问题
Stata has r()
macro for values that some commands return (return list
after the command).
I need similar access to x
after list x if y == 1
, but list
returns only r(N)
, not values themselves.
Is it possible to get the observations as a local or global macro to refer to it in the code?
回答1:
Try levelsof
command to get distinct values. It's the cat's pajamas.
回答2:
One way to save values of all observations (i.e. including repeated) is with a loop:
clear
set more off
*----- exmple data -----
sysuse auto
keep rep78
list
*----- what you want -----
forvalues i = 1/`=_N' {
local myvals `myvals' `=rep78[`i']'
}
display "`myvals'"
But more importantly, why do you think you need such a thing?
来源:https://stackoverflow.com/questions/27214675/how-to-get-observations-as-a-list-in-stata