有些数据集带有全小写值标签,最后我会得到图表和表格,显示"埃及","约旦"和"沙特阿拉伯"的结果,而不是大写的国家名称.
我想proper()字符串函数可以为我做一些事情,但我找不到正确的方法来编写Stata 11的代码,它将为给定变量的所有值标签大写.
我基本上需要proper()在变量的所有值标签上运行该函数,然后将它们分配给变量.这可能foreach在Stata中使用循环和宏吗?
是.首先,让我们创建一些带有标签的样本数据进行测试:
clear
drawnorm x, n(10)
gen byte v = int(4+x)
drop x
label define types 0 "zero" 1 "one" 2 "two" 3 "three" 4 "four" 5 "five" 6 "six"
label list types
label values v types
Run Code Online (Sandbox Code Playgroud)
这是一个宏来大写与变量"v"相关的值:
local varname v
local sLabelName: value label `varname'
di "`sLabelName'"
levelsof `varname', local(xValues)
foreach x of local xValues {
local sLabel: label (`varname') `x', strict
local sLabelNew =proper("`sLabel'")
noi di "`x': `sLabel' ==> `sLabelNew'"
label define `sLabelName' `x' "`sLabelNew'", modify
}
Run Code Online (Sandbox Code Playgroud)
运行后,检查结果:
label list types
Run Code Online (Sandbox Code Playgroud)