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上面的键)
—}
原文地址:https://www.cnblogs.com/celine227/p/14655660.html