Announcement

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

  • How do we use -- e.g. loop over, or save -- all the scalar names in a command's return list without knowing the names in advance?

    I'd like to be able to access all of the scalar names from a command's return list without knowing what they will be in advance. Is there a way to do this?


    As a simple example (not my use case), we know that the return list differs between the following two commands. The first has 7 scalars, the second has 19 scalars. I'd like to either: (i) collect all the names in a local or global string, (ii) have them assigned as row/column names in a matrix, or (iii) save their values in a .dta file with variables assigned their names.


    Command 1:
    Code:
    sysuse auto
    
    *
    qui: sum mpg
    return list
    
    
    scalars:
                      r(N) =  74
                  r(sum_w) =  74
                   r(mean) =  21.2972972972973
                    r(Var) =  33.47204738985561
                     r(sd) =  5.785503209735141
                    r(min) =  12
                    r(max) =  41
                    r(sum) =  1576
    Command 2:
    Code:
    qui: sum mpg, detail
    return list
    
    
    scalars:
                      r(N) =  74
                  r(sum_w) =  74
                   r(mean) =  21.2972972972973
                    r(Var) =  33.47204738985561
                     r(sd) =  5.785503209735141
               r(skewness) =  .9487175964588155
               r(kurtosis) =  3.97500459645325
                    r(sum) =  1576
                    r(min) =  12
                    r(max) =  41
                     r(p1) =  12
                     r(p5) =  14
                    r(p10) =  14
                    r(p25) =  18
                    r(p50) =  20
                    r(p75) =  25
                    r(p90) =  29
                    r(p95) =  34
                    r(p99) =  41
    Last edited by Joshua B. Miller; 01 Jun 2022, 01:02.

  • #2
    There are extended macro functions for that, see help macro. These differentiate between r, e, and s returned results and scalars, macros, matrices, and functions.

    Taking your example, you could type:

    Code:
    . sysuse auto
    (1978 automobile data)
    
    . sum mpg
    
        Variable |        Obs        Mean    Std. dev.       Min        Max
    -------------+---------------------------------------------------------
             mpg |         74     21.2973    5.785503         12         41
    
    . local rscalars : r(scalars)
    
    . di " `rscalars' "
     N sum_w mean Var sd min max sum
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Nice, thank you! I did look at help macro before posting but was unable to locate anything useful. Checking again I see that the word "extended" does not appear. But now I see this quote there:

      Macro functions for names of stored results

      e(scalars | macros | matrices | functions)

      r(scalars | macros | matrices | functions)
      Interesting that the following commands return nothing, and only the final one works.

      Code:
      . clear
      
      . sysuse auto
      (1978 automobile data)
      
      . sum mpg
      
          Variable |        Obs        Mean    Std. dev.       Min        Max
      -------------+---------------------------------------------------------
               mpg |         74     21.2973    5.785503         12         41
      
      . disp r(scalars)
      .
      
      . disp `r(scalars)'
      
      
      . local rscalars = r(scalars)
      
      
      . disp "`rscalars'"
      
      . local rscalars = `r(scalars)'
      invalid syntax
      r(198);
      
      . local rscalars = "`r(scalars)'"
      
      . disp "`rscalars'"
      
      
      . local rscalars: r(scalars)
      
      . disp "`rscalars'"
      N sum_w mean Var sd min max sum
      I just searched and I see Nick Cox chimed in here, though in this case the documentation above doesn't refer to r(scalar) as an extended macro function, it just says macro function.

      Comment


      • #4
        Macro functions were in previous versions called extended macro functions and had their own help file. The habit just struck. As to why your first examples did not work, that is explained by the Nick's posts that you linked to: you need the colon not the equal sign, as r(scalars) is a macro function not a function. If you want to evaluate a macro function "on the fly" you can do so by including the colon, e.g.

        Code:
        . di " `:r(scalars)' "
         N sum_w mean Var sd min max sum
        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment


        • #5
          Thanks so much Maarten. Clearly there is a gap in my knowledge of basic STATA principles.

          `:r is my new emoji!

          Comment

          Working...
          X