Announcement

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

  • generate variable considering interuptions

    Hello
    I am currently working with Panel Data. I have one variable which tells me, if a person lives in poverty (dummy variable). Since I am interessted in long-term poverty, I need a new variable, which tells me how long the poverty spell lastet (or the longest if several poverty spells) (without interuption).

    I tried:

    by idpers, sort: egen firsttime = min(cond(poverty==1, year, .))
    by idpers: egen lasttime = max(cond(poverty==1, year, .))



    but with this, it doesn't consider interuptions, but rather givs me the first and last time poverty occurred to the same person.

    Thank you very much for the help.

  • #2
    Here's one way:
    Code:
    * ignore this top bit; it is just used to simulate some data
    clear
    set obs 20
    set seed 111
    gen byte idpers = ceil(_n/10)
    bys idpers: gen int year = 2000 + _n
    gen poverty = runiformint(0,1)
    
    * start your code from here
    sort idpers year
    by idpers: gen byte spell_start = poverty == 1 & (_n == 1 | poverty[_n-1] == 0)
    by idpers: gen spell_id = cond(poverty == 1, sum(spell_start), .)
    bys idpers spell_id: egen spell_length = count(spell_id)
    by idpers: egen wanted = max(spell_length)
    drop spell_start spell_id spell_length
    sort idpers year
    which produces:
    Code:
    . li, sepby(idpers) noobs
      +----------------------------------+
      | idpers   year   poverty   wanted |
      |----------------------------------|
      |      1   2001         1        6 |
      |      1   2002         1        6 |
      |      1   2003         1        6 |
      |      1   2004         1        6 |
      |      1   2005         1        6 |
      |      1   2006         1        6 |
      |      1   2007         0        6 |
      |      1   2008         0        6 |
      |      1   2009         0        6 |
      |      1   2010         1        6 |
      |----------------------------------|
      |      2   2001         0        3 |
      |      2   2002         1        3 |
      |      2   2003         1        3 |
      |      2   2004         1        3 |
      |      2   2005         0        3 |
      |      2   2006         1        3 |
      |      2   2007         1        3 |
      |      2   2008         0        3 |
      |      2   2009         0        3 |
      |      2   2010         1        3 |
      +----------------------------------+
    Last edited by Hemanshu Kumar; 07 Dec 2022, 08:36.

    Comment


    • #3
      There is a Stata literature and there are programs to help. On the latter, see e.g. tsspell from SSC.


      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float(id year poverty)
      1 2018 0
      1 2019 0
      1 2020 1
      1 2021 1
      1 2022 1
      2 2018 1
      2 2019 1
      2 2020 0
      2 2021 0
      2 2022 1
      end
      
      tsset id year 
      
      tsspell, cond(poverty == 1)
      by id : egen longest = max(_seq)
      by id : egen nspells = total(_end)
      
      list, sepby(id) 
      
      
           +----------------------------------------------------------------+
           | id   year   poverty   _seq   _spell   _end   longest   nspells |
           |----------------------------------------------------------------|
        1. |  1   2018         0      0        0      0         3         1 |
        2. |  1   2019         0      0        0      0         3         1 |
        3. |  1   2020         1      1        1      0         3         1 |
        4. |  1   2021         1      2        1      0         3         1 |
        5. |  1   2022         1      3        1      1         3         1 |
           |----------------------------------------------------------------|
        6. |  2   2018         1      1        1      0         2         2 |
        7. |  2   2019         1      2        1      1         2         2 |
        8. |  2   2020         0      0        0      0         2         2 |
        9. |  2   2021         0      0        0      0         2         2 |
       10. |  2   2022         1      1        2      1         2         2 |
            +----------------------------------------------------------------+
      On the former, here are some suggestions.

      SJ-18-1 dm0078_3 . . . . . . . . . . . . . . . . Software update for newspell
      (help newspell if installed) . . . . . . . . . . . . . . . H. Kroger
      Q1/18 SJ 18(1):290
      newspell combine now combines overlapping spells correctly

      SJ-17-2 dm0078_2 . . . . . . . . . . . . . . . . Software update for newspell
      (help newspell if installed) . . . . . . . . . . . . . . . H. Kroger
      Q2/17 SJ 17(2):515--516
      newspell fillin now fills in gaps correctly; newspell gap bug
      fixed for finding gaps

      SJ-16-1 dm0078_1 . . . . . . . . . . . . . . . . Software update for newspell
      (help newspell if installed) . . . . . . . . . . . . . . . H. Kroger
      Q1/16 SJ 16(1):244
      bug fixed for combining two types of spells and dataset is now
      sorted as specified in sort()

      SJ-15-1 dm0078 . . . . . . . newspell: Easy management of complex spell data
      (help newspell if installed) . . . . . . . . . . . . . . . H. Kroger
      Q1/15 SJ 15(1):155--172
      introduces a command for merging or cutting off spells,
      finding gaps in the data, filling those gaps, transforming
      spell data to wide or long format, and creating completely
      new states from overlapping spells

      SJ-15-1 dm0079 . . . . . . . . . . . . . . . Stata tip 123: Spell boundaries
      . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
      Q1/15 SJ 15(1):319--323 (no commands)
      shows how to identify spells


      SJ-7-2 dm0029 . . . . . . . . . . . . . . Speaking Stata: Identifying spells
      . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
      Q2/07 SJ 7(2):249--265 (no commands)
      shows how to handle spells with complete control over
      spell specification




      Comment


      • #4
        Thank you both! I got it and it work perfectly

        Comment

        Working...
        X