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.