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]

Re: st: Remove portion of a number at the end of a variable name


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: Remove portion of a number at the end of a variable name
Date   Thu, 20 Dec 2012 11:16:34 +0000

Although this was solved to David's satisfaction, the premise was
quite incorrect. You don't _need_ to -rename- first before you
-reshape- this kind of data. Specifying the -string- option on
-reshape- enables the suffixes such as 08, 09, 10, 11 to be carried
across as is. Presumably once you have -reshape-d, you want to
reinstate the information on years, which requires a single command.
Here is a minimal example to show the point.

clear
set obs 1

foreach sfx in 09 10 11 12 {
      gen x`sfx' = 42
}

gen id = 1

. reshape long x, i(id) string
(note: j = 09 10 11 12)

Data                               wide   ->   long
-----------------------------------------------------------------------------
Number of obs.                        1   ->       4
Number of variables                   5   ->       3
j variable (4 values)                     ->   _j
xij variables:
                        x09 x10 ... x12   ->   x
-----------------------------------------------------------------------------

. l

     +--------------+
     | id   _j    x |
     |--------------|
  1. |  1   09   42 |
  2. |  1   10   42 |
  3. |  1   11   42 |
  4. |  1   12   42 |
     +--------------+

. gen year = real("20" + _j)

. drop _j

. l

     +----------------+
     | id    x   year |
     |----------------|
  1. |  1   42   2009 |
  2. |  1   42   2010 |
  3. |  1   42   2011 |
  4. |  1   42   2012 |
     +----------------+

Nick

On Wed, Dec 19, 2012 at 11:41 PM, David Torres <[email protected]> wrote:

> I have about 260 variables with two-digit numbers (representing years) following the variable stub in the following form:
>
> 08
> 09
>
> Before reshaping the data to long format, I need to get rid of the 0 between the variable stub and the year.
>
> For instance, how can I get these variables:
>
> yrid08
> yrcampus08
> testdate08
> dob08
>
> into this form:
>
> yrid8
> yrcampus8
> testdate8
> dob8
>
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index