Announcement

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

  • Collapsing panel dataset given an if condition

    Hello everybody,
    i'm struggling with what it seems to me a very silly question.

    I have a panel dataset in which Date is my t and ateco is my id variable.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float Date long ateco double(PDy emp gva out mkp)
    153 1                 . 1144.8 8394.2 90.4 106.9
    154 1                 . 1121.7 8455.1 90.1 107.4
    155 1 1.733790447524462 1103.1 7853.6 90.4 103.9
    156 1 1.653791042927907 1079.2 8387.1 90.2 108.3
    157 1 1.619407230835545 1084.5 8549.3   88 107.9
    end
    format %tq Date
    label values ateco ateco
    label def ateco 1 "A", modify
    Ateco has 10 different values, as you can see here

    Code:
      ATECO |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              A |         94       10.00       10.00
              B |         94       10.00       20.00
              C |         94       10.00       30.00
             DE |         94       10.00       40.00
              F |         94       10.00       50.00
            GHI |         94       10.00       60.00
              J |         94       10.00       70.00
           KLMN |         94       10.00       80.00
            OPQ |         94       10.00       90.00
            RST |         94       10.00      100.00
    ------------+-----------------------------------
          Total |        940      100.00
    I am interested in ateco C, GHI and F and I want to collapse data (by Date) for all the other ateco in one new ateco, i.e. W, so that I end up with a panel dataset where Date remains the t dimension and the id var ateco has 4 values, C - GHI - F - W.

    I've tried with a dummy variables with no success. Would you mind to help me?

  • #2
    Code:
    decode ateco, g(str_ateco)
    replace str_ateco="W" if !inlist(str_ateco, "C", "GHI", "F")
    collapse PDy emp gva out mkp, by(str_ateco Date)

    Comment


    • #3
      Thank you so much!! It works much better than creating dummies

      Comment

      Working...
      X