Renaming variables that contain a certain string

六月ゝ 毕业季﹏ 提交于 2019-12-25 06:39:20

问题


I have a list of variables all containing the same string "test". How do I rename all of these variables to for example var1-var20, where 20 is the number of variables. The order is not important here. I tried installing the package "renvars", and did the following

renvars *test* \ var1-var20

but this does not work. Any help is appreciated.


回答1:


To be any more help we'll need the error and how it fails. *test* should be a valid varlist and if there are the same numbers of variables in each varlist (left and right of \), the it should work.

The following works for me.

* generate some variables that fit the description
clear
local i = 0
foreach pre in ho ak {
    forvalues j = 1/10 {
        local ++i
        generate `pre'_icd`i' = ""
    }
}

* rename variables that match pattern
renvars *icd* \ var1-var20

Maybe more variables match *icd* than you expect?




回答2:


If you're using Stata 12, I think you should be able to just do:

rename (*test*) var#, addnumber

Check out this link (in particular Rule #18): http://www.stata.com/help.cgi?rename+group



来源:https://stackoverflow.com/questions/13191408/renaming-variables-that-contain-a-certain-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!