I want to generate a new variable "risk" that takes the value 1 if nr_of_phys equals 1 on the same row and the subsequent rows until phys_quit==1 for each subject (ID). I have illustrated in the table with three different examples how it should look like. Both nr_of_phys and phys_quit can only take the values 0 or 1.
I'm not able to provide you with my dataset for confidentiality reasons hence the table instead.
ID | Year | nr_of_phys | phys_quit | risk |
1 | 1 | 0 | 0 | 0 |
1 | 2 | 1 | 0 | 1 |
1 | 3 | 1 | 0 | 0 |
1 | 4 | 1 | 1 | 0 |
2 | 1 | 1 | 0 | 0 |
2 | 2 | 1 | 0 | 0 |
2 | 3 | 0 | 0 | 0 |
2 | 4 | 1 | 0 | 1 |
2 | 5 | 1 | 1 | 0 |
3 | 1 | 1 | 0 | 0 |
3 | 2 | 1 | 0 | 0 |
3 | 3 | 1 | 0 | 0 |
3 | 4 | 0 | 0 | 0 |
3 | 5 | 0 | 0 | 0 |
Comment