snadhelta
snadhelta

Reputation: 82

Stata: return a macro containing a list of all scalars stored in e()

I'm trying to write a program and would like to iterate over all scalars stored in e(). I want the list that appears on the console in response to ereturn list -- it normally includes e(N), e(r2), &c.

I can't hardcode it, because the user can add to the list using estadd.

I would love to be able to type something like:

levelsof e_scalars
return list

and see something like:

macros:
    r(escalars) : "e(N) e(r2) e(df)"

Is there any way to return that list?

Thanks.

Upvotes: 1

Views: 1553

Answers (2)

Roberto Ferrer
Roberto Ferrer

Reputation: 11102

Expanding on @Dimitriy 's answer:

clear all
set more off

sysuse auto, clear

regress price mpg
ereturn list

local groupscalars: e(scalars)

foreach element of local groupscalars {
    local e_scalars "`e_scalars' e(`element')"
}

estadd local mymacro = `"`e_scalars'"'
ereturn list

Gives you:

macros:
            e(mymacro) : "e(N) e(df_m) e(df_r) e(F) e(r2) e(rmse) e(mss) e(rss) e(r2_a) e(ll) e(ll_0).."
            e(cmdline) : "regress price mpg"
              e(title) : "Linear regression"
          e(marginsok) : "XB default"
                e(vce) : "ols"
             e(depvar) : "price"
                e(cmd) : "regress"
         e(properties) : "b V"
            e(predict) : "regres_p"
              e(model) : "ols"
          e(estat_cmd) : "regress_estat"

Upvotes: 2

dimitriy
dimitriy

Reputation: 9460

This will get you close:

sysuse auto
reg price mpg
estadd scalar mystat 42 
ereturn list

local scalars: e(scalars)
di "`scalars'"

foreach element of local scalars {
    di "e(`element') is " e(`element')
}

This is documented under "Macro extended functions for names of stored results" in the programming manual (p. 267).

Upvotes: 2

Related Questions