How to create locals on the fly in Stata?

夙愿已清 提交于 2019-12-11 05:19:22

问题


I am trying to create locals on the fly and check them by assigning values to a new variable

gen sampleVar =.

foreach i in AK AL  AR  AZ {
 su income if (year==2012 & state_nsw == "`i'"), meanonly
local val_`i' = r(mean)
display "`val_`i''"
}

 // check the local
 recode sampleVar .= "`val_AL'" 
 // this is what I get:
 5242.57421875
 .....
 5352.66796875
 . invalid name
 r(198);

 // check 2 the local
 recode sampleVar .= `val_AL'  // error

ANSWER: My problem was that I tried

 recode sampleVar .= `val_AL' + `val_AZ'
// this is inappropriate.
//the correct way is:
local try = `val_AL' + `val_AZ'
recode sampleVar .= `try'

回答1:


Be clear that your locals must be in the same name space, i.e. interactive session, do-file, do-file editor, program. To debug, type

macro li 

before the recode statement to see what macros are visible.



来源:https://stackoverflow.com/questions/17266287/how-to-create-locals-on-the-fly-in-stata

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