Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | "Seed, Paul" <paul.seed@kcl.ac.uk> |
To | "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |
Subject | st: RE: renaming variables |
Date | Mon, 24 May 2010 15:00:28 +0100 |
I'm afraid there is a problem with Nick Cox's elegant solutions to Abhimanyu Arora's problem. Abhimanyu asked: "I would like to rename my variables var53....var108 such that var53, var56, var59 ......var107 etc...are renamed as year1990, year1991......year2008 repectively. I tried to use renvars(by Jeroen Weesie) and a forvalues loop, but am not able to succeed. And Nick suggested //-rename- loc start 1990 foreach var of varlist var* { ren `var' year`start' loc ++start } However, the use of var* assumes a very unusual data set, in which the only "varX" variables are the year variables that need renaming, and they are spaced 3-apart with nothing in the gaps. This is the case in the artificial example Nick produces: clear set obs 1 forv i=53(1)107{ gen var`i'=1 } Consider perhaps a more realistic data set: clear sysuse auto forv i=40(1)107{ gen var`i'=1 } Now Nick's solution needs to be adapted slightly. //-rename2- loc start 1990 forv i=53(3)107{ ren `var'`i' year`start' loc ++start } It can also be extended to other variables. I assume that year is in each case the first (rather than the second or last) of a cluster of 3 variables: //-rename3- loc start 1990 forv i=53(3)107{ ren `var'`i' year`start' local j = `i' + 1 ren `var'`j' thing_one_`start' local j = `i' + 2 ren `var'`j' thing_two_`start' loc ++start } (with acknowledgements to Dr Seuss) http://en.wikipedia.org/wiki/The_Cat_in_the_Hat ********************************************************************* Paul T Seed King's College London, Division of Reproduction and Endocrinology St Thomas' Hospital, Westminster Bridge Road, London SE1 7EH ********************************************************************* * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/