Stored Results

r-class and e-class commands

Results of calculations are stored by Stata commands so that they can be accessed in other commands and calculations later.

There are five classes of Stata commands:
r-class  general commands that store results in r()
e-class  estimation commands that store results in e()
s-class  parsing commands that store results in s() used by programmers
n-class  commands that do not store in r(), e(), or s()
c-class  system parameters and settings that store in c()

Commands producing statistical results are either r-class or e-class: e-class for estimation results and r-class otherwise.

Following the r-class or e-class commands, we can obtain all the stored results of a command by typing return list or ereturn list respectively.

Results will be replaced the next time you execute another command of the same class. If we need to store the returned results, we need to use a macro. We will see an example below.

Types of results

There are four types of results:
Scalars: numbers
Macros: strings
Matrices: e(b) coefficient vector and e(V) variance–covariance matrix of the estimates (VCE)
Functions: the only function existing is e(sample), which evaluates to 1 (true) if the observation was used in the previous estimation and to 0 (false) otherwise.

To see what the result lists actually look like, try typing
. sysuse auto
. regress price mpg
. ereturn list
which gives us the full lists of the results that the command regress offers.

Let’s review some examples below where we use the stored results for various purposes.

centering on the mean

. sysuse auto
. sum mpg
. gen mpg_c = mpg-r(mean)

The results will be lost the next time you run another r-class command. In order to be able to use the mean of mpg later, we can:
. sum mpg
. local mpg_mean = `r(mean)'
. gen mpg_c = mpg-`mpg_mean'

using saved results in graphs

. sysuse auto
. reg mpg weight
. ereturn list
. local r2 = e(r2)
. twoway (lfitci mpg weight) (scatter mpg weight), note(R-squared =`r2')

adding stats from postestimation in tables

. sysuse auto
. reg mpg weight foreign
. test foreign weight
. outreg2 using myfile, adds(F-test, r(F), Prob > F, r(p)) replace

predicted probabilities on samples used in the previous estimation

. sysuse auto
. reg mpg weight if foreign & rep78 <=4
. predict fv if e(sample)

Storing results in a matrix

To list the matrices of stored results, we can
. sysuse auto
. reg price mpg
. matrix list e(b)
. matrix list e(V)

To use the matrices later, we need to first store the matrices:
. matrix b = e(b)
. local increment = b[1,1]
. display "increment `increment'"

System variables

We can use Stata system variables to obtain coefficients and standard errors:
_b[var] coefficient
_se[var] standard error

For instance,
. sysuse auto
. reg price weight
. dis _b[weight]
. dis _se[weight]


In loops, levelsof is useful to store the values of a variable in a local macro for later use by specifying the local() option.
. levelsof writing_level, local(wlevel)
. foreach x of local w1evel{
.   ttest writing_gpa if writing_level == `x', by(gender)
. }

Outside loops, levelsof can display the unique values of a variable.
. levelsof writing_level
. display “`r(levels)'”

More on using stored results
UCLA: Statistical Consulting Group, How Can I Access Information Stored after I Run a Command in Stata (Returned Results)? | Stata FAQ


tempvar assigns names to the specified local macro that may be used as temporary variables in a program.

What we did below is creating a temporary variable temp that stored the residuals from regressions within each group.

gen residuals = .
tempvar temp
qui levelsof group, local(g)
foreach x of local g{
	qui reg return a b c d if group == `x'
	predict `temp', residual
	replace residuals = `temp' if group == `x'
	drop `temp'


What we did below is using word to store the locations of each string in local macros, merged the files and kept the variables vertically. We merged the master file with the 1st file a1 and kept the 1st variable b1; we then merged the last file with the 2nd file a2 and kept the 2nd variable b2.

use masterfile

local files a1 a2
local vars b1 b2

forvalues i = 1/2 {
	local f : word `i' of `files'
	local v : word `i' of `vars'
	merge 1:1 id sector using `f', keepusing(`v')
	drop _merge

A case

We have an example where we use several techniques introduced earlier for automation: loops, levelsof, and using matrices to store results.

//create score_diff variables by sector*year
qui levelsof group, local(g)
gen score_diff = .
foreach x of local g{
	qui estpost ttest score if group == `x', by(rank)
	matrix b = e(b)
	replace score_diff = b[1,1] if group == `x'

Author: Yun Dai, 2018