Stefan Hansen
Stefan Hansen

Reputation: 631

Renaming variables that contain a certain string

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.

Upvotes: 0

Views: 2284

Answers (2)

vikjam
vikjam

Reputation: 550

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

Upvotes: 2

Richard Herron
Richard Herron

Reputation: 10112

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?

Upvotes: 1

Related Questions