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:40:55 +0200

<> 

Paul,

I really do not understand what you think is wrong with the solution, not
even after thinking about it for 15 minutes. I was kind of waiting for
Abhimanyu to come to the list saying he ended up with the wrong -rename-d
vars b/c his initial state was not ours in that it was not in alphabetical
order:

*************
//create vars

clear*

loc mynumbers 104 59 62 53 56 65 101 107 95 68 71 74 80 77 89 83 86 92 98


foreach i of local mynumbers{
	gen var`i'=1
}
*************

But a simple -aorder- would have changed the order to the one required. 

Your complaint seems to refer to a data structure that Abhimanyu never
requested...


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