I have a dataset like this,
clear
input byte (id gr state)
1 1 0
1 2 0
1 2 1
1 3 0
1 4 0
1 4 1
1 4 1
1 4 1
1 5 0
1 6 0
2 1 0
2 2 0
2 3 0
2 4 0
2 4 1
2 5 .
2 5 1
2 6 0
3 1 0
3 . .
3 3 0
3 4 0
3 . .
3 6 0
4 1 0
4 1 1
4 2 0
4 3 0
4 4 0
4 5 0
4 6 0
end
I want to create a new variable called "GR", indicating if a student was ever retained in grades.
The rule to create "GR" is as follows,
Within id, as long as there is a 1 in the "State" variable, then GR==1
within id, when all values of state==0, then GR==0
Within id, as long as the cells of the "state" consist of "0" and "missing values", then GR==missing value.
Can someone help me with the Stata code to achieve this goal?
Thank you!
Comment