Announcement

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

  • Creating duration of event dummies in panel data

    Hello Everyone,

    I have an unbalanced panel dataset organized by firm id, year, age, and event. The event is a dummy if the event (=1) happened in that year. Events can happen more than once for each firm throughout the sample period.

    For instance,
    1. For id 1, the first time an event occurred was in the year 2007 and lasted for one year. The second time an event occurred was in the year 2011 and lasted to the year 2012. Thus, the duration of the second event lasted two years.
    2. For id 2, the first time an event occurred was in the year 2007 and lasted to the year 2012, thus duration is six years.
    3. For id 3, the first time an event occurred was in the year 2005 and lasted to the year 2008. Thus the duration of the first event lasted four years. The second time an event occurred was in the year 2011 and lasted to the year 2012. Thus, the duration of the second event lasted two years.

    I'm trying to create multiple event dummy variable(s) so that I can sum the duration of events when an event (=1) occurred the first time, a second time, etc. by firm id.

    What I want to do:
    1. Create an event1 dummy variable in which only the duration of event1==1 happens and pre/post events ==0.
    2. Create an event2 dummy variable in which only the duration of event2==1 happens and pre/post events ==0.
    2. Create an event3 dummy variable in which only the duration of event3==1 happens and pre/post events ==0. And so forth…

    . dataex id yr age event

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id yr age event)
    1 2006  1 0
    1 2007  2 1
    1 2008  3 0
    1 2009  4 0
    1 2010  5 0
    1 2011  6 1
    1 2012  7 1
    2 2006  1 0
    2 2007  2 1
    2 2008  3 1
    2 2009  4 1
    2 2010  5 1
    2 2011  6 1
    2 2012  7 1
    3 2003  1 0
    3 2004  2 0
    3 2005  3 1
    3 2006  4 1
    3 2007  5 1
    3 2008  6 1
    3 2009  7 0
    3 2010  8 0
    3 2011  9 1
    3 2012 10 1
    end
    ------------------ copy up to and including the previous line ------------------

    Listed 24 out of 24 observations


    I would really appreciate it if you could help me here. Many thanks in advance!

  • #2
    Welcome to Statalist. And thank you for posting a question that follows the FAQ advice. It was very easy to understand and work on.

    Code:
    clear
    input float(id yr age event)
    1 2006  1 0
    1 2007  2 1
    1 2008  3 0
    1 2009  4 0
    1 2010  5 0
    1 2011  6 1
    1 2012  7 1
    2 2006  1 0
    2 2007  2 1
    2 2008  3 1
    2 2009  4 1
    2 2010  5 1
    2 2011  6 1
    2 2012  7 1
    3 2003  1 0
    3 2004  2 0
    3 2005  3 1
    3 2006  4 1
    3 2007  5 1
    3 2008  6 1
    3 2009  7 0
    3 2010  8 0
    3 2011  9 1
    3 2012 10 1
    end
    
    tsset id yr
    tsspell event if event == 1
    
    summarize _spell, detail
    
    forvalues x = 1/`=r(max)'{
        gen event_`x' = (_spell == `x')
    }
    
    drop _*
    
    list, sep(0)
    Results:

    Code:
         +---------------------------------------------+
         | id     yr   age   event   event_1   event_2 |
         |---------------------------------------------|
      1. |  1   2006     1       0         0         0 |
      2. |  1   2007     2       1         1         0 |
      3. |  1   2008     3       0         0         0 |
      4. |  1   2009     4       0         0         0 |
      5. |  1   2010     5       0         0         0 |
      6. |  1   2011     6       1         0         1 |
      7. |  1   2012     7       1         0         1 |
      8. |  2   2006     1       0         0         0 |
      9. |  2   2007     2       1         1         0 |
     10. |  2   2008     3       1         1         0 |
     11. |  2   2009     4       1         1         0 |
     12. |  2   2010     5       1         1         0 |
     13. |  2   2011     6       1         1         0 |
     14. |  2   2012     7       1         1         0 |
     15. |  3   2003     1       0         0         0 |
     16. |  3   2004     2       0         0         0 |
     17. |  3   2005     3       1         1         0 |
     18. |  3   2006     4       1         1         0 |
     19. |  3   2007     5       1         1         0 |
     20. |  3   2008     6       1         1         0 |
     21. |  3   2009     7       0         0         0 |
     22. |  3   2010     8       0         0         0 |
     23. |  3   2011     9       1         0         1 |
     24. |  3   2012    10       1         0         1 |
         +---------------------------------------------+

    Comment


    • #3
      tsspell is from SSC and must be installed using ssc install tsspell before you can use it.

      Comment


      • #4
        This is brilliant; the codes worked exactly the way I had imagined. Thank you very much Ken and Nick for introducing me to tsspell command.

        Comment

        Working...
        X