Loops 使用Stata中的循环重命名长变量列表
我正试图用Stata编写一个代码。 我目前正在使用以家庭为观察单位的数据集。每个家庭成员也有变量,例如家庭中第一个人的hv101_01,家庭中第二个人的hv101_02,以及最多hv101_39,所有这些变量都包含相同的标签。这是许多变量的情况 我想将所有变量的名称更改为标签名称。我可以通过以下方式来理解这一点:Loops 使用Stata中的循环重命名长变量列表,loops,foreach,stata,stata-macros,Loops,Foreach,Stata,Stata Macros,我正试图用Stata编写一个代码。 我目前正在使用以家庭为观察单位的数据集。每个家庭成员也有变量,例如家庭中第一个人的hv101_01,家庭中第二个人的hv101_02,以及最多hv101_39,所有这些变量都包含相同的标签。这是许多变量的情况 我想将所有变量的名称更改为标签名称。我可以通过以下方式来理解这一点: foreach v of var * { local lbl : var label `v' local lbl = strtoname("`lbl'") rename `v' `lb
foreach v of var * {
local lbl : var label `v'
local lbl = strtoname("`lbl'")
rename `v' `lbl'
label variable `lbl' "`v'"
}
但是当它到达第二个家庭成员的变量时,例如hv101_02,Stata说变量名已经定义好了。我知道这是因为hv101_01已经使用了该标签名称
当变量更改为标签名称时,我想在变量后面添加_02或任何数字。
有人能帮我写一个代码吗
感谢您的预期响应。根据您的变量名考虑以下玩具示例:
clear
set obs 1
forvalues i = 11 / 15 {
generate hv101_`i' = rnormal()
label variable hv101_`i' ExampleVarLabel
}
describe, fullnames
Contains data
obs: 1
vars: 5
size: 20
-------------------------------------------------------------------------------------------------------------------------------------
storage display value
variable name type format label variable label
-------------------------------------------------------------------------------------------------------------------------------------
hv101_11 float %9.0g ExampleVarLabel
hv101_12 float %9.0g ExampleVarLabel
hv101_13 float %9.0g ExampleVarLabel
hv101_14 float %9.0g ExampleVarLabel
hv101_15 float %9.0g ExampleVarLabel
-------------------------------------------------------------------------------------------------------------------------------------
Sorted by:
Note: Dataset has changed since last saved.
以下内容对我很有用:
foreach v of var * {
local lbl : variable label `v'
rename `v' `lbl'`=substr("`v'", strpos("`v'", "_"), .)'
label variable `lbl'`=substr("`v'", strpos("`v'", "_"), .)' `v'
}
describe, fullnames
Contains data
obs: 1
vars: 5
size: 20
-------------------------------------------------------------------------------------------------------------------------------------
storage display value
variable name type format label variable label
-------------------------------------------------------------------------------------------------------------------------------------
ExampleVarLabel_11
float %9.0g hv101_11
ExampleVarLabel_12
float %9.0g hv101_12
ExampleVarLabel_13
float %9.0g hv101_13
ExampleVarLabel_14
float %9.0g hv101_14
ExampleVarLabel_15
float %9.0g hv101_15
-------------------------------------------------------------------------------------------------------------------------------------
Sorted by:
Note: Dataset has changed since last saved.
编辑:
如果标签相同且变量编号连续,则甚至不需要循环:
local lbl : variable label hv101_11
rename hv101_* `lbl'_#, renumber(11)
考虑以下基于变量名的玩具示例:
clear
set obs 1
forvalues i = 11 / 15 {
generate hv101_`i' = rnormal()
label variable hv101_`i' ExampleVarLabel
}
describe, fullnames
Contains data
obs: 1
vars: 5
size: 20
-------------------------------------------------------------------------------------------------------------------------------------
storage display value
variable name type format label variable label
-------------------------------------------------------------------------------------------------------------------------------------
hv101_11 float %9.0g ExampleVarLabel
hv101_12 float %9.0g ExampleVarLabel
hv101_13 float %9.0g ExampleVarLabel
hv101_14 float %9.0g ExampleVarLabel
hv101_15 float %9.0g ExampleVarLabel
-------------------------------------------------------------------------------------------------------------------------------------
Sorted by:
Note: Dataset has changed since last saved.
以下内容对我很有用:
foreach v of var * {
local lbl : variable label `v'
rename `v' `lbl'`=substr("`v'", strpos("`v'", "_"), .)'
label variable `lbl'`=substr("`v'", strpos("`v'", "_"), .)' `v'
}
describe, fullnames
Contains data
obs: 1
vars: 5
size: 20
-------------------------------------------------------------------------------------------------------------------------------------
storage display value
variable name type format label variable label
-------------------------------------------------------------------------------------------------------------------------------------
ExampleVarLabel_11
float %9.0g hv101_11
ExampleVarLabel_12
float %9.0g hv101_12
ExampleVarLabel_13
float %9.0g hv101_13
ExampleVarLabel_14
float %9.0g hv101_14
ExampleVarLabel_15
float %9.0g hv101_15
-------------------------------------------------------------------------------------------------------------------------------------
Sorted by:
Note: Dataset has changed since last saved.
编辑:
如果标签相同且变量编号连续,则甚至不需要循环:
local lbl : variable label hv101_11
rename hv101_* `lbl'_#, renumber(11)
这是Andrew Musau在中提供的正确答案
这是Andrew Musau在中提供的正确答案
Cross-posted at Cross-posted at这与我的解决方案完全相同,只是考虑了变量标签中的额外空格,而您在问题中从未出现过这些空格。您所要做的就是添加
local lbl=subinstr(“`lbl'”,“,”,”)
在重命名
命令之前。这与我的解决方案完全相同,只是考虑了变量标签中的额外空格,而您在问题中从未出现过这些空格。您所要做的就是在重命名
命令之前添加local lbl=subinstr(“`lbl'”,”)
。