Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Re: Extract variable name in order to change it, iteratively


From   Jeppe Warberg Larsen <[email protected]>
To   [email protected]
Subject   Re: st: Re: Extract variable name in order to change it, iteratively
Date   Wed, 3 Aug 2005 19:59:17 +0200

Thanks to all who have responded on my question!! I am on vacation
now, so I shall try out your suggestions when I'm back, in a week.

On 8/3/05, n j cox <[email protected]> wrote:
> Jeppe Warberg Larsen asked (minor editing here)
> 
>  > I have a wide data structure. Each observation consists of a unique
>  > identifier (say, -id-) and some series of annual variables with a
>  > two-digit suffix (running in different intervals). Like this:
>  >
>  > id var1_88 var1_89 ... var1_02 var2_92 var2_93 ... var2_02
>  > var3_...................................
> 
>  > Now I want to -reshape- it to long, but Stata fails to read the
>  > two-digit values after the turn of century (00 01 02), so I need to
>  > -rename- all varibles to a four-digit year.
> 
> and then later admitted
> 
>  > the variable names are not actually var1_ var2_ var3_ ... but words:
>  > something_ somethingelse_
> 
> Jeppe got various good advice from Rafa De Hoyos, David Harrison, Philip
> Ryan and James Muller. Leaving on one side a direct -reshape- solution,
> interesting in its own right, but not the main issue here,
> the emphasis was on how to set up a -foreach- loop. Something like
> this is indicated (this code owes most to David Harrison's solution,
> but replaces an -if-/-else- construct by a call to -cond()-):
> 
> foreach v of varlist *_?? {
>        local yy = substr("`v'",-2,2)
>        local cc = cond(`yy' < 50, 20, 19)
>        local new = substr("`v'",1,length("`v'")-2) + "`cc'`yy'"
>        rename `v' `new'
> }
> 
> I was also interested to see whether -renvars- from STB-61, which
> purports to offer canned solutions to many renaming problems, was
> up to the job.
> 
> There may be a simpler way, but a somewhat grotesque one-line solution
> is possible with -renvars-, here split for display.
> 
> renvars *_??, map(substr("@",1,length("@")-2) +                 cond(substr("@",-2,2)
> < "50", "20" + substr("@",-2,2),
> "19" + substr("@",-2,2)))
> 
> A detail of note is the use of -cond()- in both solutions. In the
> first case, it is optional but replaces up to 6 lines with one.
> In the second case, it is essential as no -if-/-else- construct
> can be put inside the -map()- option of -renvars-.
> 
> The next issue of the Stata Journal will carry a tutorial on -cond()-
> by David Kantor and myself, as it often seems to be overlooked
> in problems like these.
> 
> Personally, I prefer the -foreach- route here. Brevity is good,
> but clarity is more important. I find it easier to follow the
> -foreach- logic step by step than to decode a complicated all-in-one
> solution.
> 
> Nick
> [email protected]
> 
> 
> *
> *   For searches and help try:
> *   http://www.stata.com/support/faqs/res/findit.html
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index