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

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


From   Jeppe Warberg Larsen <jeppewl@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Extract variable name in order to change it, iteratively
Date   Wed, 10 Aug 2005 12:51:04 +0200

Thanks, this was exactly what I needed!
Philip Ryan's direct-reshape approach may have been a good
alternative, but I'm happy with James' solution.

Jeppe

On 8/2/05, James Muller <james.muller@internode.on.net> wrote:
> If your dates are all the same number of digits you can do some direct 
> string processing:
> 
> 
> foreach var of varlist `mylist' {
>     local len = length("`var'")
> 
>     local head = substr("`var'",1,`len'-2)
>     local tail = substr("`var'",`len'-1,`len')
> 
>     if (`tail' < 70) local date = 2000 + `tail'
>     else local date = 1900 + `tail'
> 
>     rename `var' `head'`date'
> }
> 
> 
> and then reshape as you like.
> 
> James
> 
> 
> 
> David Harrison wrote:
> 
> >Along these lines, you could also specify that the two-digit year should be
> a string variable with, e.g.
> >
> >reshape long var1_ var2_, i(id) j(year) string
> >
> >David
> >
> >-----Original Message-----
> >From: owner-statalist@hsphsun2.harvard.edu
> >[mailto:owner-statalist@hsphsun2.harvard.edu]On Behalf Of Philip Ryan
> >Sent: 02 August 2005 14:06
> >To: statalist@hsphsun2.harvard.edu
> >Subject: Re: st: Extract variable name in order to change it,
> >iteratively
> >
> >
> >You might try an alternative specification of the j index on -reshape-.
> >
> >Here is a little data set:
> >
> >. list, clean noobs
> >
> > id   var1_00  var1_01 var1_88  var1_89  var2_00 var2_01  var2_88  var2_89
> >  1        11       13      12        3        4       6       67        8
> >  2         9        6      14        5       10       2       18        3
> >  3        13        4      11        2       15       8        8        7
> >
> >
> >. reshape long var1_ var2_, i(id) j(year 00 01 88 89)
> >
> >Data                               wide   ->   long
> >-----------------------------------------------------------------------------
> >Number of obs.                        3   ->      12
> >Number of variables                   9   ->       4
> >j variable (4 values)                     ->   year
> >xij variables:
> >            var1_00 var1_01 ... var1_89   ->   var1_
> >            var2_00 var2_01 ... var2_89   ->   var2_
> >-----------------------------------------------------------------------------
> >
> >. li, clean noobs
> >
> >    id   year   var1_   var2_
> >     1      0      11       4
> >     1      1      13       6
> >     1     88      12      67
> >     1     89       3       8
> >     2      0       9      10
> >     2      1       6       2
> >     2     88      14      18
> >     2     89       5       3
> >     3      0      13      15
> >     3      1       4       8
> >     3     88      11       8
> >     3     89       2       7
> >
> >That is to say, _you_ may specify the values of the subobservation index
> rather
> >than allow Stata to try and determine them - unsuccesfully on this
> occasion, as
> >you discovered.
> >
> >Phil
> >
> >
> >Quoting Jeppe Warberg Larsen <jeppewl@gmail.com>:
> >
> >  
> >
> >>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.
> >>
> >>foreach var of varlist var1_88-var100_02{
> >>	//save name of variable in a local string
> >>	//do nessecary string manipulation (I can do that)
> >>	//rename var
> >>}
> >>
> >>My problem seems to be:
> >>1) extracting the name of the current variable
> >>2) rename
> >>
> >>Do you understand what I'm asking?
> >>I'd be glad if someone can help, and please don't tell me that it
> >>would be easier to do this manually, because I will need to do this
> >>operation many times...
> >>
> >>Jeppe W. Larsen
> >>
> >>*
> >>*   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/
>

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