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   Mon, 24 May 2010 16:08:31 +0200

<> 


Just to be sure, Paul, you should blame, since Nick really only changed one
line in my proposed code at
http://www.stata.com/statalist/archive/2010-05/msg01140.html. The initial
poster was happy with the solution, btw
(http://www.stata.com/statalist/archive/2010-05/msg01170.html). 




HTH
Martin

-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von Seed, Paul
Gesendet: Montag, 24. Mai 2010 16:00
An: [email protected]
Betreff: st: RE: renaming variables

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/


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