Sunday, November 3, 2013

Batch Variable Rename - Stata

* Using macros it is very easy to rename any number of variables in Stata.

* Imagine you have a number of variables.

clear
set obs 10

forv i=1/100 {
  gen var`=`i'^2' = rnormal()
}

* We can rename variables by using a - mark which tells Stata to use
* the variable list.

foreach v of varlist var1-var100 {
  rename `v' `v'A
}

* We can also rename with the wildcards * and ?

foreach v of varlist var*4* {
  rename `v' four`v'
}

Formatted By Econometrics by Simulation

1 comment:

  1. As of Stata 12, the -rename- command can rename more than one variable at once. For example, -rename (var1-var100) =A- instead of the first loop. Even in Stata 11 and below, the user-written -renvars- can rename multiple variables at once.

    ReplyDelete