Stata:使用循环将标签分配给变量范围 [英] Stata: Assign labels to range of variables with a loop

查看:235
本文介绍了Stata:使用循环将标签分配给变量范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有60个变量,没有一个具有相似的命名模式.我想为所有存储在本地的变量分配标签.例如

Let's say I have 60 variables, none with similar naming patterns. I want to assign labels to all variables, which I stored locally. So for example

local mylabels "dog cat bird"  

但是,我正在为循环的确切表达而苦苦挣扎.我是否必须全局存储变量范围,然后使用foreach?还是我使用取值?

However I am struggling with the exact expression of the loop. Do I have to store my variable range globally and then use a foreach? Or do I use forvalues?

编辑:我指的是变量标签.我设法创建了一个循环,类似于此处 http ://www.stata.com/support/faqs/programming/looping-over-parallel-lists/.但是,我遇到了一个更困难的问题:我的变量没有特定的命名模式,并且标签具有特殊字符(空格,逗号,%符号),这就是我的循环无法正常工作的地方.

I was referring to variable labels. I managed to create a loop, similar to the method used here http://www.stata.com/support/faqs/programming/looping-over-parallel-lists/. However I ran into a more difficult problem: my variables have no particular naming patterns, and the labels have special characters (spaces, commas, %-signs), and here is where my loop does not work.

一些示例数据(请排除随机性):

Some example data (excuse the randomness):

gen Apples_ts_sum = .
gen Pears_avg_1y = .
gen Bananas_max_2y = .

以及一些示例标签:

苹果的时间序列,总和,%"梨的平均,一年以上" 最大香蕉,超过2年".

"Time series of apples, sum, %" "Average of pears, over 1 year" "Maximum of bananas, over 2 years".

我遇到了Nick Cox的以下条目: http://www .stata.com/statalist/archive/2012-10/msg00285.html 并尝试应用提到的括号方法,如下所示:

I ran into this entry by Nick Cox: http://www.stata.com/statalist/archive/2012-10/msg00285.html and tried to apply the mentioned parentheses method, like so:

 local mylabels `" "Time series of apples, sum, %" "Average of pears, over 1 year" "Maximum of bananas, over 2 years" "'

但是无法使其正常工作.

But could not get it to work.

推荐答案

如果要将所有变量都标记为同一事物,例如"dog cat bird",则可以对describe命令使用varlist选项.假设您的60个变量通常可以用表达式EXP列出.然后:

If you want to label all the variables the same thing, for example "dog cat bird", Then you can use the varlist option for the describe command. Let's say your 60 variables can be generally listed with the expression EXP. Then:

    qui des EXP, varlist
    foreach variable in `r(varlist)'{
        label var `variable' "dog cat bird"
    }

以您的示例数据为例,我创建了另一个包含变量名称的局部变量.

Edited: Taking your example data, I created another local containing the variable names.

 local myvar `" "Apples_ts_sum" "Pears_avg_1y" "Bananas_max_2y" "'
 local mylabels `" "Time series of apples, sum, %" "Average of pears, over 1 year" "Maximum of bananas, over 2 years" "'
 forval n = 1/3{
    local a: word `n' of `mylabels'
    local b: word `n' of `myvar'
    di "variable `b', label `a'"
    label var `b' "`a'"
 }

请注意,我手动创建了变量列表.您可以使用des, varlist,使用上面列出的方法自动创建此列表.

Note that I manually created the list of variables. You can automatically create this list using the method I listed above, with des, varlist.

qui des , varlist
foreach var in `r(varlist)'{
    local myvar_t "`myvar_t' `var'"
}

然后您可以在上面的示例中使用本地myvar_t代替myvar.

You can then use the local myvar_t instead of myvar in the above example.

这篇关于Stata:使用循环将标签分配给变量范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆