************************** 一、foreach 命令: 列表循环 ************************** ** 1. local —— 局部宏 local grains "rice wheat corn rye barley oats" foreach x of local grains { display"`x'" } // 等效命令 foreach x in rice wheat corn rye barley oats { display"`x'" }
** 2. global —— 全局宏 global money "Franc Dollar Lira Pound" foreach y of global money { display"`y'" } // 等效命令 foreach y in Franc Dollar Lira Pound { display"`y'" } // global指代特定文本 webuse womenwage, clear global Xlist "age age2 nev_mar rural school tenure wage wagecat" corr$Xlist collin $Xlist
** 3. 三种列表 ** 3.1 varlist - 变量列表 foreach v of varlist age* rural-wage { summ`v' } // 等效命令 foreach v in age* rural-wage { summ`v' }
** 3.2 newlist - 新变量列表 foreach v of newlist z1-z20 { gen`v' = . } // 等效命令 forvaluesn = 1/20 { gen z`n' = . }