How to get observations as a list in Stata?

旧时模样 提交于 2019-12-13 08:35:19

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!