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: AW: RE: renaming variables


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   st: AW: RE: renaming variables
Date   Fri, 21 May 2010 14:35:47 +0200

<> 

" His code can be very slightly simplified:"




So in case you did not know where Nick lives/works, it is the European
country where "understatement" is more popular than anywhere else...

-ren `var' year`start'- is of course a substantial improvement on my code,
although pedagogically mine may make a more lasting impact on readers. If
you want an even shorter version still, throw out -set obs 1- as well: to
show changes in variable names, we do not need actual data, do we?




HTH
Martin

-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von Nick Cox
Gesendet: Freitag, 21. Mai 2010 14:26
An: [email protected]
Betreff: st: RE: renaming variables

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


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