Announcement

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

  • Create new variable that counts changes

    Hello Floks, I am using a panel data (8 yeras, 2012-2020) where the variabler employment (yes=1, No=0) denotes whether a respondent was employed in a specific survey year. I wish to see how many times there were changes in employment i.e. from no to yes or vice versa for each individual.
    Could you please help me with how create such a variable say "transition" that counts total changes for the entire timeframe. Many thanks in advance

  • #2
    Such data are often discussed in terms of runs or spells of the same value. For the main points, see https://www.stata-journal.com/articl...article=dm0029 or search here for mentions of dm0029.

    You may wish to subtract 1 from this variable.

    Code:
    clear
    set obs 18
    egen id = seq(), block(9)
    egen time = seq(), from(2012) to(2020)
    
    gen unemploy = real(word("0 0 0 1 1 1 1 1 0", _n)) in 1/9
    replace unemploy = real(word("1 1 0 0 0 0 1 1 1", _n-9)) in 10/18
    
    bysort id : gen change = sum(unemploy != unemploy[_n-1])
    
    list, sepby(id change)
    
         +-------------------------------+
         | id   time   unemploy   change |
         |-------------------------------|
      1. |  1   2012          0        1 |
      2. |  1   2013          0        1 |
      3. |  1   2014          0        1 |
         |-------------------------------|
      4. |  1   2015          1        2 |
      5. |  1   2016          1        2 |
      6. |  1   2017          1        2 |
      7. |  1   2018          1        2 |
      8. |  1   2019          1        2 |
         |-------------------------------|
      9. |  1   2020          0        3 |
         |-------------------------------|
     10. |  2   2012          1        1 |
     11. |  2   2013          1        1 |
         |-------------------------------|
     12. |  2   2014          0        2 |
     13. |  2   2015          0        2 |
     14. |  2   2016          0        2 |
     15. |  2   2017          0        2 |
         |-------------------------------|
     16. |  2   2018          1        3 |
     17. |  2   2019          1        3 |
     18. |  2   2020          1        3 |
         +-------------------------------+
    
    .

    Comment

    Working...
    X