Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: renaming variables


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: renaming variables
Date   Fri, 21 May 2010 13:26:20 +0100

-renvars- was written by Jeroen Weesie and myself. Good practice as
often requested on the list is to say where user-written stuff comes
from, which here is the Stata Journal site. 

As you say, your code was incorrect.

1. -postsub()- is an option of -renvars- and therefore must follow a
comma. 

2. -postsub()- does not use comma as separator but a space. 

3. Your code would not change "var" to "year". 

4. You need at most one loop simultaneously over 53(3)107 and 1990/2008
(with 19 iterations) but your two nested loops would produce 19 * 19
iterations. 

In principle, you seem to be trying to call -renvars- once for each
renaming. If you are going to do that you might as well use -rename-. 

-renvars- can be used to solve your problem -- in one line -- but I
think the solution is too messy to be interesting. After all, you want
to change the prefix systematically and the postfix from an
idiosyncratic number to a more systematic number, all at once, and no
one-line solution for that could look nice. 

Any way, this is a solution: 

renvars var*, map("year" + string(1989 + (real(substr("@",4,.)) - 50) /
3))

Here all the looping is inside -renvars-. 

Even as an author of -renvars- I would much prefer going back to first
principles and very much agree with Martin Weiss's strategy. 

His code can be very slightly simplified: 

clear
set obs 1

forv i=53(3)107{
	gen var`i'=1
}

//-rename-
loc start 1990

foreach var of varlist var*{
	ren `var' year`start'
	loc ++start
}

Nick 
[email protected] 

Abhimanyu Arora

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.
Is there any other way?

PS-my incorrect way follows


forvalues j =53(3)107{
forvalues i =1990/2008{
renvars postsub(`i',`j')    /*the arguments should be strings, I guess,
though*/
}
}

*
*   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index