list - 访问Stata中的列表元素

标签 list foreach 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')

最佳答案

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'

}

参见:http://www.stata.com/support/faqs/lang/parallel.html

关于list - 访问Stata中的列表元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7666551/

相关文章:

python - 在 Python 中查找列表和多个子列表之间的匹配索引

list - 合并rhandsontable中的多个单元格

r - 重复执行矩阵乘法的有效方法

c# - 如何将项目添加到数组以替换变量

c++ - 如何在 for_each 中使用 lambda?

stata - Twoway estpost tabstat-esttab : retain variable labels

list - 无法推断参数类型

r - 从 R 调用 Stata 函数

stata - 通过在 Stata 中将变量除以 IQR 创建变量

java - 输入帐号查找银行账户