Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

st: RE: RE: renaming variables


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: RE: renaming variables
Date   Mon, 24 May 2010 15:07:59 +0100

Paul is correct. 

If the variables are not as Abhimanyu specified, then the code needs
changing. 

I have to say that I don't regard this as a problem with my solution,
but more an illustration that a closely related but different question
will have a closely related but different answer. 

Nick 
n.j.cox@durham.ac.uk 

Seed, Paul

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
}



*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index