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1 0
8 4 3 0 1 0
8 5 4 0 1 0
8 6 5 0 1 0
8 7 6 0 1 0
8 8 7 0 1 0
8 9 . 0 1 0
8 10 7 0 1 0
end
I want to correctly select the observation from ID1 to ID6. The selection rule is as follows,
All variables that start with k are equal==0 & there is a repeated number on the variable "gr" within id only once.
For example, for the student with id==1,2,3, the student is retained grades in 9th grade, respectively.
for the student with id==4, the student is retained grades in 10th grade.
for the student with id==5, the student is retained grades in 4th grade.
for the student with id==6, the student is retained grades in 6th grade.
Because the real data has so many data observations, I just used a faked one for illustration.
Thank you for your code!
clear
input byte (id year gr kg5 k68 k912)
1 1 0 0 0 0
1 2 1 0 0 0
1 3 2 0 0 0
1 4 3 0 0 0
1 5 4 0 0 0
1 6 5 0 0 0
1 7 6 0 0 0
1 8 7 0 0 0
1 9 8 0 0 0
1 10 9 0 0 0
1 11 . 0 0 0
1 12 9 0 0 0
1 13 10 0 0 0
2 1 0 0 0 0
2 2 1 0 0 0
2 3 2 0 0 0
2 4 3 0 0 0
2 5 4 0 0 0
2 6 5 0 0 0
2 7 6 0 0 0
2 8 7 0 0 0
2 9 8 0 0 0
2 10 9 0 0 0
2 11 10 0 0 0
2 12 . 0 0 0
2 13 9 0 0 0
3 1 0 0 0 0
3 2 . 0 0 0
3 3 . 0 0 0
3 4 . 0 0 0
3 5 . 0 0 0
3 6 . 0 0 0
3 7 . 0 0 0
3 8 . 0 0 0
3 9 . 0 0 0
3 10 9 0 0 0
3 11 . 0 0 0
3 12 . 0 0 0
3 13 9 0 0 0
4 1 0 0 0 0
4 2 . 0 0 0
4 3 . 0 0 0
4 4 . 0 0 0
4 5 . 0 0 0
4 6 . 0 0 0
4 7 . 0 0 0
4 8 . 0 0 0
4 9 8 0 0 0
4 10 . 0 0 0
4 11 10 0 0 0
4 12 9 0 0 0
4 13 10 0 0 0
5 1 0 0 0 0
5 2 1 0 0 0
5 3 2 0 0 0
5 4 3 0 0 0
5 5 4 0 0 0
5 6 . 0 0 0
5 7 4 0 0 0
6 1 0 0 0 0
6 2 1 0 0 0
6 3 2 0 0 0
6 4 3 0 0 0
6 5 4 0 0 0
6 6 5 0 0 0
6 7 6 0 0 0
6 8 . 0 0 0
6 9 . 0 0 0
6 10 . 0 0 0
6 11 6 0 0 0
7 1 0 0 0 0
7 2 1 0 0 0
7 3 2 0 0 0
7 4 3 0 0 0
7 5 4 0 0 0
7 6 5 0 0 0
7 7 6 0 0 0
7 8 7 0 0 0
7 9 . 0 0 0
7 10 9 0 0 0
8 1 0 0 1 0
8 2 1 0 1 0
8 3 2 0 1 0
8 4 3 0 1 0
8 5 4 0 1 0
8 6 5 0 1 0
8 7 6 0 1 0
8 8 7 0 1 0
8 9 . 0 1 0
8 10 7 0 1 0
end
I want to correctly select the observation from ID1 to ID6. The selection rule is as follows,
All variables that start with k are equal==0 & there is a repeated number on the variable "gr" within id only once.
For example, for the student with id==1,2,3, the student is retained grades in 9th grade, respectively.
for the student with id==4, the student is retained grades in 10th grade.
for the student with id==5, the student is retained grades in 4th grade.
for the student with id==6, the student is retained grades in 6th grade.
Because the real data has so many data observations, I just used a faked one for illustration.
Thank you for your code!

Comment