使用标签值

时间:2018-06-13 15:50:46

标签: rename stata stata-macros

我想自动重命名Stata数据集中的所有变量及其标签值。

知道如何做到这一点?

示例数据:

input str13 usa str9 v338 str13(fra gbr)
"1.443888e+05"  "0" "7.402382e+04"  "2.017132e+05"
"1.540046e+05"  "0" "2.091502e+05"  "1.863581e+05"
"1.339439e+05"  "0" "1.898640e+04"  "1.540931e+05"
"1.340120e+05"  "0" "6.982494e+04"  "1.554574e+05"
"1.339604e+05"  "0" "5.265134e+04"  "1.593273e+05"
"1.436713e+05"  "0" "2.477454e+05"  "2.010521e+05"
"1.339533e+05"  "0" "2.244940e+04"  "1.542236e+05"
"1.339438e+05"  "0" "1.898623e+04"  "1.540923e+05"
"3.089005e+05"  "0" "2.489533e+05"  "1.941806e+05"
"1.387412e+05"  "0" "2.759644e+04"  "1.538440e+05"

修改

您是否知道从第二次出现的本地宏countries开始的方法? 我想从本地宏列表中排除第一列。

1 个答案:

答案 0 :(得分:1)

这是如何做到的:

clear 

input str13 usa str9 v338 str13(fra gbr) 
"1.443888e+05" "0" "7.402382e+04" "2.017132e+05"
"1.364555e+05" "0" "3.683586e+05" "1.988450e+05"
"2.577159e+05" "0" "3.995106e+05" "2.113975e+05"
"1.351904e+05" "0" "2.586979e+05" "1.905737e+05"
"1.339386e+05" "0" "1.895385e+04" "1.540146e+05"
"1.517621e+05" "0" "2.021519e+04" "1.769611e+05"
"1.540046e+05" "0" "2.091502e+05" "1.863581e+05"
"1.339439e+05" "0" "1.898640e+04" "1.540931e+05"
"1.340120e+05" "0" "6.982494" "1.554574e+05" 
end 

label variable usa USA
label variable v338 Unknown1
label variable fra France
label variable gbr Britain

ds
local countries `r(varlist)'

foreach var of local countries {
    rename `var' `: variable label `var''
}

list

     +-------------------------------------------------------+
     |          USA   Unknown1         France        Britain |
     |-------------------------------------------------------|
  1. | 1.443888e+05          0   7.402382e+04   2.017132e+05 |
  2. | 1.364555e+05          0   3.683586e+05   1.988450e+05 |
  3. | 2.577159e+05          0   3.995106e+05   2.113975e+05 |
  4. | 1.351904e+05          0   2.586979e+05   1.905737e+05 |
  5. | 1.339386e+05          0   1.895385e+04   1.540146e+05 |
     |-------------------------------------------------------|
  6. | 1.517621e+05          0   2.021519e+04   1.769611e+05 |
  7. | 1.540046e+05          0   2.091502e+05   1.863581e+05 |
  8. | 1.339439e+05          0   1.898640e+04   1.540931e+05 |
  9. | 1.340120e+05          0       6.982494   1.554574e+05 |
     +-------------------------------------------------------+

我使用了一些玩具标签作为示例,并使用命令ds来获取本地宏中数据集中变量的所有名称。

修改

键入ds usa, not而不是ds