Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Reshape wide to long

    Hi

    I have a question using the code -reshape-.
    For each year denoted by alphabet 'a' to 'd'. Ex) 2000 stands for year 'a' and 2004 stands for year 'd', I have two variables *sts and *strc.
    I would like to convert the wide data into long version.

    I tried
    Code:
    reshape *sts *strc, i(id) j(year)
    but I think Stata is not recognizing the j because I am using a string not numeric.
    Do I need to convert the variable names first? Is there a restriction in using -reshape-?

    Thank you so much!

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str3 id byte(asts astrc bsts bstrc csts cstrc dsts dstrc)
    "003"  14   1  14   1  12   1  -8  -8
    "005"  12   1  -8  -8  -8  -8  -8  -8
    "006"   .   .  37   4  -8  -8  26   3
    "007"   .   .  -8  -8  -8  -8   .   .
    "008"  13   1   .   .   .   .   .   .
    "009"  24   3   .   .   .   .   .   .
    "010"  14   1  17   2  15   1  15   1
    "011"  -8  -8  -8  -8   .   .   .   .
    "012" -10 -10 -10 -10   .   .   .   .
    "013" -10 -10 -10 -10   .   .   .   .
    "014"  17   2  14   1  18   2  15   1
    "015"  15   1  13   1  25   3  28   3
    "016"  12   1  14   1  13   1  15   1
    "018"  21   2  16   2  25   3  26   3
    "019"  14   1  13   1  15   1  12   1
    end

  • #2
    I did fix the reshape problem renaming the variable into sts"year" and strc"year" but I was wondering if there is another method. Thank you!

    Comment


    • #3
      Code:
      reshape long @sts @strc, i(id) j(year) string
      Code:
      . list if id=="003", clean
      
              id   year   sts   strc  
        1.   003      a    14      1  
        2.   003      b    14      1  
        3.   003      c    12      1  
        4.   003      d    -8     -8
      The key is in the output of help reshape where it tells us
      and where stubnames are variable names (long->wide), or stubs of variable names (wide->long), and either way, may contain @, denoting where j appears or is to appear in the name.
      So you are not limited to having the j() come from the end of the stub.

      Comment


      • #4
        Thank you so much!!

        Comment

        Working...
        X