Announcement

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

  • Generate many variables after reshape wide

    Hello,

    After applying this code:

    Code:
    reshape wide stub, i(i) j(j)
    Assume j = 1, 2, 3, 4 and there are several stubs.

    Is there an easy way to generate new variables for j = 5, instead of having to gen stub5 = . for all stubs?


  • #2
    The following example may point you in a useful direction.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(i j a b)
    101 1 45 16
    101 2 35 97
    101 3 18 89
    101 4  4 88
    102 1 45 59
    102 2 10  9
    102 3 42 44
    102 4 72 93
    103 1 27  6
    103 2 75 96
    103 3 48 64
    103 4 51 92
    end
    set obs `= _N+1'
    replace j = 5 in l
    list in l
    fillin i j
    drop _fillin
    drop if i==.
    list if i==101
    reshape wide a b, i(i) j(j)
    list if i==101
    Code:
    . set obs `= _N+1'
    number of observations (_N) was 12, now 13
    
    . replace j = 5 in l
    (1 real change made)
    
    . list in l
    
         +---------------+
         | i   j   a   b |
         |---------------|
     13. | .   5   .   . |
         +---------------+
    
    . fillin i j
    
    . drop _fillin
    
    . drop if i==.
    (5 observations deleted)
    
    . list if i==101
    
         +-------------------+
         |   i   j    a    b |
         |-------------------|
      1. | 101   1   45   16 |
      2. | 101   2   35   97 |
      3. | 101   3   18   89 |
      4. | 101   4    4   88 |
      5. | 101   5    .    . |
         +-------------------+
    
    . reshape wide a b, i(i) j(j)
    (note: j = 1 2 3 4 5)
    
    Data                               long   ->   wide
    -----------------------------------------------------------------------------
    Number of obs.                       15   ->       3
    Number of variables                   4   ->      11
    j variable (5 values)                 j   ->   (dropped)
    xij variables:
                                          a   ->   a1 a2 ... a5
                                          b   ->   b1 b2 ... b5
    -----------------------------------------------------------------------------
    
    . list if i==101
    
         +-------------------------------------------------------+
         |   i   a1   b1   a2   b2   a3   b3   a4   b4   a5   b5 |
         |-------------------------------------------------------|
      1. | 101   45   16   35   97   18   89    4   88    .    . |
         +-------------------------------------------------------+

    Comment


    • #3
      Hello, thank you very much. This was extremely helpful!

      Comment


      • #4
        Below code, which seems more concise in coding and typing, but not in calculating, is ... just for a tricky fun.
        Code:
        reshape wide a b, i(i) j(j)
        gen a5=.
        reshape long
        reshape wide

        Comment

        Working...
        X