use*_*032 2 foreach list stata
假设您必须在 Stata 中运行以下命令
tab var1 region if var1 > 4
tab var2 region if var2 > 32
tab var3 region if var3 > 7
Run Code Online (Sandbox Code Playgroud)
对于许多变量依此类推。请注意,输入的过滤器取决于if变量。
我想通过迭代变量列表来执行相同的操作。就像是
thresholdList = "4 32 7 ..." /// don't know if this works
foreach myvar of var1 var2 var3 ... {
tab `myvar' region if `myvar' > thresholdList(`counter')
local counter = `counter' + 1
}
Run Code Online (Sandbox Code Playgroud)
`
显然,上面的代码在 Stata 中不起作用。我试图了解如何定义一个包含值列表的宏并显式访问列表中的每个元素,即
thresholdList(`counter')
Run Code Online (Sandbox Code Playgroud)
Stata可以做到这一点。您想要使用的语法应该是这样的:
local thresholdlist "4 32 7"
local varlist "var1 var2 var3"
local numitems = wordcount("`thresholdlist'")
forv i=1/`numitems' {
local thisthreshold : word `i' of `thresholdlist'
local thisvar : word `i' of `varlist'
di "variable: `thisvar', threshold: `thisthreshold'"
tab `thisvar' region if `thisvar' > `thisthreshold'
}
Run Code Online (Sandbox Code Playgroud)
请参阅: http: //www.stata.com/support/faqs/lang/parallel.html