Dear Statalist,
I would really appreciate if you could help me on this as I've been trying to solve this problem for hours now!
Below is what my data looks like (copied via dataex).
clear
input long(id casenum) byte(active startmonth) int startyear
1 1 0 1 2017
1 1 0 2 2017
1 1 1 3 2017
1 1 0 4 2017
1 1 0 5 2017
1 1 0 6 2017
1 1 0 7 2017
1 1 0 8 2017
1 1 0 9 2017
1 1 0 10 2017
1 1 0 11 2017
1 1 0 12 2017
1 1 0 1 2018
1 1 0 2 2018
1 1 1 3 2018
1 1 1 4 2018
1 1 1 5 2018
1 1 1 6 2018
1 1 1 7 2018
1 1 1 8 2018
1 1 1 9 2018
1 1 1 10 2018
1 1 1 11 2018
1 1 1 12 2018
1 1 1 1 2019
1 1 1 2 2019
1 1 1 3 2019
1 1 1 4 2019
1 1 1 5 2019
1 1 1 6 2019
1 1 0 7 2019
1 1 0 8 2019
1 1 0 9 2019
1 1 0 10 2019
1 1 0 11 2019
1 1 0 12 2019
1 1 0 1 2020
1 1 0 2 2020
1 1 0 3 2020
1 1 0 4 2020
1 1 0 5 2020
1 1 0 6 2020
1 1 0 7 2020
1 1 0 8 2020
1 1 0 9 2020
1 1 1 10 2020
1 1 1 11 2020
1 1 1 12 2020
1 1 1 1 2021
1 1 1 2 2021
1 1 1 3 2021
1 1 1 4 2021
1 1 1 5 2021
1 1 1 6 2021
1 1 1 7 2021
1 2 0 1 2017
1 2 0 2 2017
1 2 0 3 2017
1 2 0 4 2017
1 2 0 5 2017
1 2 0 6 2017
1 2 0 7 2017
1 2 0 8 2017
1 2 0 9 2017
1 2 0 10 2017
1 2 0 11 2017
1 2 0 12 2017
1 2 0 1 2018
1 2 0 2 2018
1 2 0 3 2018
1 2 0 4 2018
1 2 0 5 2018
1 2 0 6 2018
1 2 0 7 2018
1 2 0 8 2018
1 2 0 9 2018
1 2 0 10 2018
1 2 0 11 2018
1 2 0 12 2018
1 2 1 1 2019
1 2 1 2 2019
1 2 1 3 2019
1 2 1 4 2019
1 2 1 5 2019
1 2 1 6 2019
1 2 0 7 2019
1 2 0 8 2019
1 2 0 9 2019
1 2 0 10 2019
1 2 0 11 2019
1 2 0 12 2019
1 2 0 1 2020
1 2 0 2 2020
1 2 0 3 2020
1 2 0 4 2020
1 2 0 5 2020
1 2 0 6 2020
1 2 0 7 2020
1 2 0 8 2020
1 2 0 9 2020
end
As you can see in this example, there are two different case numbers (1 and 2) for an individual ID = 1.
I've sorted the data with "sort id casenum startyear startmonth active".
I want to assign _n for every time 'active' changes from 1 to 0 or vice versa within the unique id and case number.
Put differently, for ID =1 & casenum =1, I want to create a new variable (let's call it 'spell') that will equal 1 for the first two rows (active = 0), 2 for the third row (active = 1), 3 for rows 4 to 14, and so on.
Hope this makes sense! Please feel free to ask for any clarification & I really appreciate your help in advance!
I would really appreciate if you could help me on this as I've been trying to solve this problem for hours now!
Below is what my data looks like (copied via dataex).
clear
input long(id casenum) byte(active startmonth) int startyear
1 1 0 1 2017
1 1 0 2 2017
1 1 1 3 2017
1 1 0 4 2017
1 1 0 5 2017
1 1 0 6 2017
1 1 0 7 2017
1 1 0 8 2017
1 1 0 9 2017
1 1 0 10 2017
1 1 0 11 2017
1 1 0 12 2017
1 1 0 1 2018
1 1 0 2 2018
1 1 1 3 2018
1 1 1 4 2018
1 1 1 5 2018
1 1 1 6 2018
1 1 1 7 2018
1 1 1 8 2018
1 1 1 9 2018
1 1 1 10 2018
1 1 1 11 2018
1 1 1 12 2018
1 1 1 1 2019
1 1 1 2 2019
1 1 1 3 2019
1 1 1 4 2019
1 1 1 5 2019
1 1 1 6 2019
1 1 0 7 2019
1 1 0 8 2019
1 1 0 9 2019
1 1 0 10 2019
1 1 0 11 2019
1 1 0 12 2019
1 1 0 1 2020
1 1 0 2 2020
1 1 0 3 2020
1 1 0 4 2020
1 1 0 5 2020
1 1 0 6 2020
1 1 0 7 2020
1 1 0 8 2020
1 1 0 9 2020
1 1 1 10 2020
1 1 1 11 2020
1 1 1 12 2020
1 1 1 1 2021
1 1 1 2 2021
1 1 1 3 2021
1 1 1 4 2021
1 1 1 5 2021
1 1 1 6 2021
1 1 1 7 2021
1 2 0 1 2017
1 2 0 2 2017
1 2 0 3 2017
1 2 0 4 2017
1 2 0 5 2017
1 2 0 6 2017
1 2 0 7 2017
1 2 0 8 2017
1 2 0 9 2017
1 2 0 10 2017
1 2 0 11 2017
1 2 0 12 2017
1 2 0 1 2018
1 2 0 2 2018
1 2 0 3 2018
1 2 0 4 2018
1 2 0 5 2018
1 2 0 6 2018
1 2 0 7 2018
1 2 0 8 2018
1 2 0 9 2018
1 2 0 10 2018
1 2 0 11 2018
1 2 0 12 2018
1 2 1 1 2019
1 2 1 2 2019
1 2 1 3 2019
1 2 1 4 2019
1 2 1 5 2019
1 2 1 6 2019
1 2 0 7 2019
1 2 0 8 2019
1 2 0 9 2019
1 2 0 10 2019
1 2 0 11 2019
1 2 0 12 2019
1 2 0 1 2020
1 2 0 2 2020
1 2 0 3 2020
1 2 0 4 2020
1 2 0 5 2020
1 2 0 6 2020
1 2 0 7 2020
1 2 0 8 2020
1 2 0 9 2020
end
As you can see in this example, there are two different case numbers (1 and 2) for an individual ID = 1.
I've sorted the data with "sort id casenum startyear startmonth active".
I want to assign _n for every time 'active' changes from 1 to 0 or vice versa within the unique id and case number.
Put differently, for ID =1 & casenum =1, I want to create a new variable (let's call it 'spell') that will equal 1 for the first two rows (active = 0), 2 for the third row (active = 1), 3 for rows 4 to 14, and so on.
Hope this makes sense! Please feel free to ask for any clarification & I really appreciate your help in advance!
Comment