help _variables
-------------------------------------------------------------------------------
Title
[U] 13.4 System variables (_variables)
Description
Expressions may also contain _variables (pronounced "underscore
variables"). These are built-in system variables that are created and
updated by Stata. They are called _variables because their names all
begin with the underscore (_) character.
The _variables are
[eqno]_b[varname] (synonym: [eqno]_coef[varname]) contains the value (to
machine precision) of the coefficient on varname from the most
recently fitted model (such as ANOVA, regression, Cox, logit, probit,
and multinomial logit). See [U] 13.5 Accessing coefficients and
standard errors for a description.
_cons is always equal to the number 1 when used directly and refers to
the intercept term when used indirectly, as in _b[_cons].
_n contains the number of the current observation.
_N contains the total number of observations in the dataset.
_pi contains the value of pi to machine precision.
_rc contains the value of the return code from the most recent capture
command; see [P] capture.
[eqno]_se[varname] contains the value (to machine precision) of the
standard error of the coefficient on varname from the most recently
fitted model (such as ANOVA, regression, Cox, logit, probit, and
multinomial logit). See [U] 13.5 Accessing coefficients and standard
errors for a complete description.
The _n and _N variables are useful for indexing observations or
generating sequences of numbers. _n can act as a running counter within
a by-group and _N acts as the total number within each by-group.
_rc is useful for programmers, particularly if you want to test a command
internal to a program without having the program terminate.
Examples
. display _pi
. display cos(_pi)
. range x 0 4*_pi 100
. sysuse auto, clear
. generate price2 = price[_n-1]
. regress price mpg foreign rep78
. display _b[mpg]
. mlogit rep78 gear_ratio displacement foreign
. display [5]_b[foreign]
. webuse dollhill2, clear
. by age (smokes), sort: generate wgt=pyears[_N]
Also see
Manual: [U] 13.4 System variables (_variables)
Help: [D] by, [D] generate, [U] 13.7 Explicit subscripting