stata: for循环


foreach循环:跟的对象可以是宏、变量名和文件名等

 Braces must be specified with foreach, and 1. the open brace must appear on the same line as the foreach; 2. nothing may follow the open brace except, of course, comments; the first command to be executed must appear on a new line; 3. the close brace must appear on a line by itself.

gen x1 = rnormal()
gen x2 = rnormal()
gen x3 = rnormal()
gen x4 = rnormal()
gen x5 = rnormal()

foreach var in x1 x2 x3 x4 x5{
    disp ""
    disp ""
    disp "T-test: the mean of variable `var' is 0"
    ttest `var' = 0
}
sysuse auto, clear

foreach v in mpg weight length{
    summarize `v'
}

如果选择所有变量,可以写成:

foreach var of varlist_all{
    command
}

或

foreach var of varlist * {
    command
}

forvalues

forvalues i= 10(-3)1 {
—display `i'   //注意暂元怎么写`(左边的半引号是tab上面的键)
—}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM