Announcement

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

  • Reshaping data

    Hi all,

    I'm working with some panel data that includes different companies (ID) and the number of times in a period (countP1 countP2 countP3 countP4) they release a new product in a different class (class1). Right now, if a company produces an item in class 105 in period 1 and again in period 2, the data appears in separate rows (see dataex below), however, I'd like to reshape so that the classes are not repeating (see table below). I tried to use the reshape command but couldn't quite get it to work. Does anyone have any suggestions?
    ID class1 P1 P2 P3 P4
    1002 33 0 0 0 1
    1002 42 0 1 0 0
    1002 89 0 2 0 0
    1002 102 2 0 0 1
    1002 105 0 0 2 2




    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long ID int class1 float(countP1 countP2 countP3 countP4)
    1002  33  . . .  1
    1002  42  . 1 .  .
    1002  89  . 2 .  .
    1002 102  2 . .  .
    1002 102  . . .  1
    1002 105  . . 2  .
    1002 105  . . .  2
    1002 244  . . 1  .
    1002 244  . . .  1
    1002 250  1 . .  .
    1002 307  . . 1  .
    1002 324  . . 1  .
    1002 327  . 1 .  .
    1002 331  . 1 .  .
    1002 342  . . 1  .
    1002 342  . . .  1
    1002 356  . 4 .  .
    1002 367  1 . .  .
    1002 367  . 1 .  .
    1002 367  . . 1  .
    1002 392  2 . .  .
    1002 434  4 . .  .
    1002 434  . 1 .  .
    1002 434  . . 1  .
    1002 604  1 . .  .
    1002 623  1 . .  .
    1002 623  . 1 .  .
    1002 701  . . .  1
    1002 702  . . .  1
    1002 704  . . .  1
    1002 708  . . .  1
    1004  15  2 . .  .
    1004  15  . 2 .  .
    1004  29  . . .  1
    1004 108  . . 1  .
    1004 108  . . .  1
    1004 180  2 . .  .
    1004 193  . 1 .  .
    1004 220  1 . .  .
    1004 376  1 . .  .
    1004 410  1 . .  .
    1004 410  . . 1  .
    1006 442  1 . .  .
    1010 105 10 . .  .
    1010 105  . 3 .  .
    1010 137  1 . .  .
    1010 188  1 . .  .
    1010 222  2 . .  .
    1010 251  1 . .  .
    1010 406  2 . .  .
    1010 410  4 . .  .
    1012  29  . . .  1
    1012  55  . . 1  .
    1012  55  . . .  2
    1012  95  . . .  1
    1012 174  . . .  2
    1012 210  2 . .  .
    1012 264  . . 2  .
    1012 277  1 . .  .
    1012 277  . . 1  .
    1012 385  . 1 .  .
    1012 414  1 . .  .
    1012 414  . 1 .  .
    1012 422  1 . .  .
    1012 422  . 1 .  .
    1012 439  . 1 .  .
    1012 451  . . 1  .
    1012 501  . 1 .  .
    1013  16  . . 2  .
    1013  29  2 . .  .
    1013  29  . . 1  .
    1013  29  . . .  8
    1013  40  . . 1  .
    1013  52  . . .  2
    1013  57  . . .  1
    1013  60  . . 1  .
    1013 118  . . 1  .
    1013 165  . . .  3
    1013 174  . . 9  .
    1013 174  . . . 30
    1013 200  1 . .  .
    1013 200  . . 2  .
    1013 200  . . .  9
    1013 211  . . 9  .
    1013 211  . . .  5
    1013 229  . . .  1
    1013 235  1 . .  .
    1013 235  . . .  1
    1013 242  . . 2  .
    1013 248  2 . .  .
    1013 248  . . 5  .
    1013 248  . . .  9
    1013 257  . . 1  .
    1013 264  . . .  1
    1013 307  . . 1  .
    1013 312  . . 1  .
    1013 312  . . .  6
    1013 320  . . 1  .
    1013 323  . . 1  .
    1013 324  . 1 .  .
    end

  • #2
    Do you mean this?

    Code:
    collapse (sum) countP*, by(ID class1)

    Comment


    • #3
      Yes! That works perfectly. Thank you

      Comment

      Working...
      X