Announcement

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

  • stack-related problem?

    Dear All, I found this question here. The raw data is
    Code:
    * Example generated by -dataex-For more info, type help dataex
    clear
    input str3 id float(year A B)
    "A" 2005  1  2
    "B" 2005  3  4
    "C" 2005  5  6
    "A" 2006  7  8
    "B" 2006  9 10
    "C" 2006 11 12
    end
    The desired result is something like
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str3 id float(year A B year2 A2 B2)
    "A" 2005 1 2 2006  7  8
    "B" 2005 3 4 2006  9 10
    "C" 2005 5 6 2006 11 12
    end
    Any suggestions are appreciated.
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str3 id float(year A B)
    "A" 2005  1  2
    "B" 2005  3  4
    "C" 2005  5  6
    "A" 2006  7  8
    "B" 2006  9 10
    "C" 2006 11 12
    end
    
    bys id (year): gen which=_n
    reshape wide year A B, i(id) j(which)
    Res.:

    Code:
    . l
    
         +----------------------------------------+
         | id   year1   A1   B1   year2   A2   B2 |
         |----------------------------------------|
      1. |  A    2005    1    2    2006    7    8 |
      2. |  B    2005    3    4    2006    9   10 |
      3. |  C    2005    5    6    2006   11   12 |
         +----------------------------------------+

    Comment


    • #3
      Dear Andrew Musau, Thanks for this helpful suggestion. I have tried -reshape- comand but missed the `which' part.
      Ho-Chuan (River) Huang
      Stata 19.0, MP(4)

      Comment

      Working...
      X