访问Stata中的列表元素

发布于 2024-12-08 12:23:05 字数 577 浏览 3 评论 0原文

想象一下,您必须在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江南烟雨〆相思醉 2024-12-15 12:23:05

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

Stata can do this. The syntax you want to use should be something like this:

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'

}

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

云淡月浅 2024-12-15 12:23:05

对您的代码的其他一些建议和更正 - 首先,我将使用 -tokenize- 迭代您的项目列表,其次使用本地宏来存储您的 thresholdList',最后使用“本地计数器” code>++counter'" 而不是 "local counter = counter+1" 来迭代计数器,因此:

clear
set obs 200
forval n = 1/3 {
    g var`n' = ceil(runiform()*10)
    }
g region = 1


loc thresholdList  "4 32 7 " //here's your list
token `"`thresholdList'"'
**notice how tokenize stores these values:
di "`1'"
di "`2'"
**now you'll iterate i to reference the token locations:
loc i = 1
foreach myvar in var1 var2 var3 { //use 'of' not 'in'
     tab `myvar' region if `myvar' > ``i''
    loc i `++i'  //iterates `i'

}

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:

clear
set obs 200
forval n = 1/3 {
    g var`n' = ceil(runiform()*10)
    }
g region = 1


loc thresholdList  "4 32 7 " //here's your list
token `"`thresholdList'"'
**notice how tokenize stores these values:
di "`1'"
di "`2'"
**now you'll iterate i to reference the token locations:
loc i = 1
foreach myvar in var1 var2 var3 { //use 'of' not 'in'
     tab `myvar' region if `myvar' > ``i''
    loc i `++i'  //iterates `i'

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文