Hello,
I have a small dataset as follows, id is student id, and gr represents grade. If a student failed grade, then the student will repeat grades and the new variable "GR" should be equal to 1, otherwise 0.
If the gr value is missing value, then "GR" is also equal to missing value.

clear
input byte (id gr)
1 1
1 2
1 2
1 3
1 4
1 4
1 4
1 4
1 5
1 6
2 1
2 2
2 3
2 4
2 4
2 5
2 5
2 6
3 1
3 .
3 3
3 4
3 .
3 6
4 1
4 1
4 2
4 3
4 4
4 5
4 6
end

I ever asked this question at https://www.statalist.org/forums/forum/general-stata-discussion/general/1654328-ask-for-help-to-create-multiple-sell-and-multiple-state-data-using-statat
However, I forgot the issue of missing value. So, today I asked here.
Can someone help me with the Stata code?
Thank you!