访问Stata中的列表元素
想象一下,您必须在 Stata
tab var1 region if var1 > 4
tab var2 region if var2 > 32
tab var3 region if var3 > 7
等中针对许多变量运行以下命令。请注意,输入到 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
}
,
上面的代码在 Stata 中不起作用。我试图了解如何定义一个包含值列表的宏并显式访问列表中的每个元素,即
thresholdList(`counter')
Imagine you have to run the following in Stata
tab var1 region if var1 > 4
tab var2 region if var2 > 32
tab var3 region if var3 > 7
and so on for many variables. Notice that the filter fed to if
depends on the variable.
I would like to do the same by iterating over a list of variables. Something like
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
}
`
Clearly, the code here above does not work in Stata. I'm trying to understand how can I define a macro including a list of values and access each element of the list explicitly, i.e.
thresholdList(`counter')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Stata可以做到这一点。您要使用的语法应如下所示:
请参阅: http://www .stata.com/support/faqs/lang/parallel.html
Stata can do this. The syntax you want to use should be something like this:
See: http://www.stata.com/support/faqs/lang/parallel.html
对您的代码的其他一些建议和更正 - 首先,我将使用 -tokenize- 迭代您的项目列表,其次使用本地宏来存储您的
thresholdList',最后使用“本地计数器” code>++counter'" 而不是 "local counter = counter+1" 来迭代计数器,因此:
A couple of other suggestions and corrections to your code - First, I'd use -tokenize- to iterate over your list of items, second use a local macro to store your
thresholdList', and finally use "local counter
++counter'" instead of "local counter = counter+1" to iterate your counter, so: