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]
AW: st: AW: RE: renaming variables
From
"Martin Weiss" <[email protected]>
To
<[email protected]>
Subject
AW: st: AW: RE: renaming variables
Date
Mon, 24 May 2010 18:33:53 +0200
<>
True, so the ultimate telescoped solution for Abhimanyu is:
*************
clear*
forv i=53/108{
gen var`i'=.
}
loc start 1990
forv i=53(3)108{
ren var`i' year`start++'
}
*************
Abhimanyu can read up on "macro increments" via "++" in [U], section 18.3.7.
HTH
Martin
-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von Nick Cox
Gesendet: Montag, 24. Mai 2010 18:22
An: [email protected]
Betreff: RE: st: AW: RE: renaming variables
Note that the code
local start 1990
forv i=53(3)108{
loc myvars `myvars' var`i'
}
foreach var of local myvars{
ren `var' year`start'
loc ++start
}
can be condensed to
local start 1990
forv i=53(3)108{
ren var`i' year`start'
loc ++start
}
and in turn into
local start 1990
forv i=53(3)108{
ren var`i' year`start++'
}
Nick
[email protected]
Martin Weiss
" Yes indeed , I should have mentioned while thanking,
that I had to drop the var54+3.. and var55+3... before applying
Martin's solution to get exectly what I wanted..."
Well, if you had said it, I would have replied that you can gather the
relevant -varname-s in a -local- and loop over it:
*************
//create vars
clear*
forv i=53/108{
gen var`i'=1
}
//-rename-
loc start 1990
//get vars into -local-
forv i=53(3)108{
loc myvars `myvars' var`i'
}
foreach var of local myvars{
ren `var' year`start'
loc ++start
}
*************
Abhimanyu Arora
Hi Martin Nick Paul
Thank you Paul. Yes indeed , I should have mentioned while thanking,
that I had to drop the var54+3.. and var55+3... before applying
Martin's solution to get exectly what I wanted...
Best regards
Abhimanyu
On Mon, May 24, 2010 at 4:40 PM, Martin Weiss <[email protected]> wrote:
>
> <>
>
> 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
>
*
* 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/