Announcement

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

  • stack/unstack?

    Dear All, Suppose that I use -stack- command in the following.
    Code:
     webuse stackxmpl, clear
    
    . list
    
         +---------------+
         | a   b   c   d |
         |---------------|
      1. | 1   2   3   4 |
      2. | 5   6   7   8 |
         +---------------+
    
    .         
    . stack a b c d, into(e f) clear
    
    . list
    
         +----------------+
         | _stack   e   f |
         |----------------|
      1. |      1   1   2 |
      2. |      1   5   6 |
      3. |      2   3   4 |
      4. |      2   7   8 |
         +----------------+
    My question is: how can go from the bottom table to the top table? Thanks.
    Ho-Chuan (River) Huang
    Stata 17.0, MP(4)

  • #2
    Code:
    webuse stackxmpl, clear
    stack a b c d, into(e f) clear
    bysort _stack: gen i = _n
    reshape wide e f, i(i) j(_stack)
    list
    
         +-----------------------+
         | i   e1   f1   e2   f2 |
         |-----------------------|
      1. | 1    1    2    3    4 |
      2. | 2    5    6    7    8 |
         +-----------------------+
    
    drop i
    rename * (a b c d)
    list
    
         +---------------+
         | a   b   c   d |
         |---------------|
      1. | 1   2   3   4 |
      2. | 5   6   7   8 |
         +---------------+

    Comment


    • #3
      Dear Wouter, Many thanks for this helpful suggestion.
      Ho-Chuan (River) Huang
      Stata 17.0, MP(4)

      Comment

      Working...
      X