问题
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