Announcement

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

  • Generating consecutive variables with panel data in wide form (using tsspell)

    Hi Stata Users,

    I'm struggling with constructing variables using panel data (I'll eventually use sequence analysis). I know that I've to use tsspell from ssc, but I nevertheless got stuck.

    My data reflect individual labor market careers in the wide format. For each spell, I've a variable indicating (1) the number of the start month of the spell, (2) the number of the end month of the spell, and (3) the status reflecting the spell. The variable name carries the chronological order of the spell (1 thru a maximum of 62). The month number could've been any, mine happens to be organized from #717 thru #1352 (more than half a century of monthly observations).

    For my particular analysis, I'm not just interested in the order of the spells, but also in the timing of the careers: month-by-month. So, I'm looking to construct as many variables as there are months in my dataset (717 thru 1352), each reflecting the status in that particular month (instead of spell). I therefore somehow have to tell Stata to create variables that pull each number between "startmonth1" and "endmonth1" and fill this cell with whatever is in "status1". I can then repeat this for any other spell. I realize this will lead to a huge datamatrix (in wide form), but it's the one I need for the sequence analysis.

    Any idea of how to code this (using tsspell)?

    Best,

    Dirk

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id startmonth1 endmonth1 status1 startmonth2 endmonth2 status2)
    1  717  726 1  726  750 7
    2 1287 1293 1 1293 1323 1
    3  745  750 1  750  764 1
    4  618  755 1  755  911 3
    5  871  892 1  892  906 6
    6 1208 1256 1 1256 1285 7
    end
    label values status1 estatverF
    label values status2 estatverF
    label def estatverF 1 "employed any", modify
    label def estatverF 3 "farmer", modify
    label def estatverF 6 "parental leave", modify
    label def estatverF 7 "studying", modify



  • #2
    Welcome to Statalist, Dirk.

    The following code seems to do what you want - construct a wide dataset with one status variable for each month - but does not make use of tsspell.

    You will see that I had to make one correction. In your data, startmonth2 is the same as endmonth1. Then the question becomes, what is the status for that month - status2 or status1? I chose to start the second spell one month after the end of the first spell.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id startmonth1 endmonth1 status1 startmonth2 endmonth2 status2)
    1  717  726 1  726  750 7
    2 1287 1293 1 1293 1323 1
    3  745  750 1  750  764 1
    4  618  755 1  755  911 3
    5  871  892 1  892  906 6
    6 1208 1256 1 1256 1285 7
    end
    label values status1 estatverF
    label values status2 estatverF
    label def estatverF 1 "employed any", modify
    label def estatverF 3 "farmer", modify
    label def estatverF 6 "parental leave", modify
    label def estatverF 7 "studying", modify
    // correct overlapping spells
    forvalues s=2/2 { /* replace with 2/62 on real data */
        local r = `s'-1
        replace startmonth`s' = endmonth`r' + 1 if startmonth`s' <= endmonth`r'
        }
    // reshape to long
    reshape long startmonth endmonth status, i(id) j(spell)
    // replicate observations
    expand endmonth-startmonth+1
    // assign to a month
    by id startmonth, sort: generate month = startmonth + (_n-1)
    // back to wide
    drop startmonth endmonth spell
    rename status st
    reshape wide st , i(id) j(month)
    // look at an example
    list id st715-st755 if id==1
    Code:
    . list id st715-st755 if id==1
    
         +-------------------------------------------------------------------+
      1. | id | st715 | st716 |        st717 |        st718  |        st719  |
         |  1 |     . |     . | employed any | employed any  | employed any  |
         |-------------------------------------------------------------------|
         |         st720  |         st721  |         st722  |         st723  |
         |  employed any  |  employed any  |  employed any  |  employed any  |
         |-------------------------------------------------------------------|
         |        st724 |        st725 |        st726 |    st727 |    st728  |
         | employed any | employed any | employed any | studying | studying  |
         |-------------------------------------------------------+-----------|
         |    st729 |    st730 |    st731 |    st732 |    st733  |    st734  |
         | studying | studying | studying | studying | studying  | studying  |
         |----------+----------+----------+----------+-----------+-----------|
         |    st735 |    st736 |    st737 |    st738 |    st739  |    st740  |
         | studying | studying | studying | studying | studying  | studying  |
         |----------+----------+----------+----------+-----------+-----------|
         |    st741 |    st742 |    st743 |    st744 |    st745  |    st746  |
         | studying | studying | studying | studying | studying  | studying  |
         |----------+----------+----------+----------+-----------------------|
         |    st747 |    st748 |    st749 |    st750 | st751 | st752 | st753 |
         | studying | studying | studying | studying |     . |     . |     . |
         |-------------------------------------------------------------------|
         |              st754              |              st755              |
         |                  .              |                  .              |
         +-------------------------------------------------------------------+

    Comment


    • #3
      To add a note to William's very helpful suggestions:

      tsspell (SSC) requires tsset or xtset data and therefore long layout (structure or format, if you will). It's quite incompatible with this data layout therefore.

      Comment


      • #4
        Thank you so much, William and Nick, for your code and insights. It works perfectly!
        That's right, there's an overlap; the last month of each spell is also the first month of the next one. The data should actually be organized in the way you suggested. Thank you for catching that.

        Comment

        Working...
        X