Search code examples
stata

Extension of consecutive sum using Stata


I have a panel. Each ID is a person.

lose_A is 1 if a person's A status changes from 1 to 0.

I only have ID, year, A, lose_A, and A_upgrade.

I want to make length and length2.

length is just number of consecutive 1s of A.

length2 is similar but just one difference: It goes back to 1 right after the year in which A_upgrade is 1.

ID year  A  lose_A  A_upgrade   length  length2
1  3     1    1       0           1        1
1  4     0    0       0           0        0
1  5     1    0       0           1        1
1  6     1    0       1           2        2
1  7     1    0       0           3        1
2  4     0    0       0           0        0
2  5     1    0       0           1        1
2  6     1    0       0           2        2
2  7     0    1       0           0        0

I did

bysort ID (year): gen sumA=sum(A)

as a stepping stone for length and length2. But I don't know what to do next.


Solution

  • Thanks for the clear question and data example.

    The main idea here is to think that you have spells or runs of your main variable being 1. So you divide the problem into (1) creating markers for the start of each kind of spell and (2) counting upwards within each spell.

    There is much more discussion within http://www.stata-journal.com/sjpdf.html?articlenum=dm0029

    dm0029 and tsspell (a program on SSC) are otherwise unpredictable search terms to find many related posts on Statalist.

    clear 
    input ID year  A  lose_A  A_upgrade   length  length2
    1  3     1    1       0           1        1
    1  4     0    0       0           0        0
    1  5     1    0       0           1        1
    1  6     1    0       1           2        2
    1  7     1    0       0           3        1
    2  4     0    0       0           0        0
    2  5     1    0       0           1        1
    2  6     1    0       0           2        2
    2  7     0    1       0           0        0
    end 
    
    * start of each spell is 1, others 0 
    bysort ID (year) : gen Length = A == 1 & (_n == 1 | A[_n-1] == 0) 
    
    * count 2, 3, ... within spells 
    by ID : replace Length = Length[_n-1] + 1 if A == 1 & Length == 0 
    
    * second variable is a twist on the first 
    bysort ID (year) : gen Length2 = A == 1 & (_n == 1 | A[_n-1] == 0 | A_upgrade[_n-1] == 1) 
    by ID : replace Length2 = Length2[_n-1] + 1 if A == 1 & Length2 == 0 
    
    list, sepby(ID) 
    
         +-------------------------------------------------------------------------+
         | ID   year   A   lose_A   A_upgr~e   length   length2   Length   Length2 |
         |-------------------------------------------------------------------------|
      1. |  1      3   1        1          0        1         1        1         1 |
      2. |  1      4   0        0          0        0         0        0         0 |
      3. |  1      5   1        0          0        1         1        1         1 |
      4. |  1      6   1        0          1        2         2        2         2 |
      5. |  1      7   1        0          0        3         1        3         1 |
         |-------------------------------------------------------------------------|
      6. |  2      4   0        0          0        0         0        0         0 |
      7. |  2      5   1        0          0        1         1        1         1 |
      8. |  2      6   1        0          0        2         2        2         2 |
      9. |  2      7   0        1          0        0         0        0         0 |
         +-------------------------------------------------------------------------+