Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Add leading zero before decimal in scalars

    Dear All,

    I am writing to ask a quick question on making a small adjustment to scalars.

    Code:
    sysuse auto, clear
    gen lprice = log(price)
    reg lprice mpg
    scalar b = _b[mpg]
    display %9.3f scalar(b)
    -0.033
    
    local coef: di %9.3f scalar(b)
    display `coef'
    -.033
    As shown in the codes, -local- has dismissed the leading 0, while the original -display- can display the leading 0.

    I am wondering how I could have this leading 0 back when I turn the result into local because I would like to use this -local coef- to produce a regression table, which does not look nice if the leading 0 is missing.

    Thank you very much and I look forward to hearing from you!

    Best regards,
    Long





  • #2
    1. I do not think -local- has dismissed anything, if you replace the last two lines of your code with the following, you get the same result as you got in your first block of code:

    Code:
    . scalar b = _b[mpg]
    
    . display %9.3f scalar(b)
       -0.033
    
    . local coef: di %9.3f scalar(b)
    
    . display `coef'
    -.033
    
    . display %9.3f `coef'
       -0.033
    2. I think different formats affect only how Stata shows you a result, not how Stata stores the result.

    Comment


    • #3
      Joro makes good points. Let's emphasise that display isn't always a candid guide to the contents of local macros or numeric scalars. The fact that you can supply a display format as part of the instructions is part of that story.

      But there is more to be said. You can use string scalars. They don't remember any format used to produce them, but they keep the results.

      Code:
      . sysuse auto, clear
      (1978 Automobile Data)
      
      . gen lprice = log(price)
      
      . reg lprice mpg
      
            Source |       SS           df       MS      Number of obs   =        74
      -------------+----------------------------------   F(1, 72)        =     22.87
             Model |  2.70578153         1  2.70578153   Prob > F        =    0.0000
          Residual |  8.51775155        72  .118302105   R-squared       =    0.2411
      -------------+----------------------------------   Adj R-squared   =    0.2305
             Total |  11.2235331        73  .153747029   Root MSE        =    .34395
      
      ------------------------------------------------------------------------------
            lprice |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
      -------------+----------------------------------------------------------------
               mpg |   -.033277   .0069581    -4.78   0.000    -.0471478   -.0194062
             _cons |   9.349342    .153489    60.91   0.000     9.043367    9.655317
      ------------------------------------------------------------------------------
      
      . scalar b = string(_b[mpg], "%09.3f")
      
      . scalar list
               b = -0000.033
      
      . display scalar(b)
      -0.033

      Comment

      Working...
      X