stata - Creating a flag using indexes -
i'm looking build flags students have repeated grade, skipped grade, or have unusual grade progression (e.g. 4th grade in 2008 , 7th grade in 2009). data unique @ student id-year-subject level , structured (albeit more variables):
id year subject tested_grade 1 2011 m 10 1 2012 m 11 1 2013 m 12 2 2011 r 4 2 2012 r 7 2 2013 r 8 3 2011 m 6 3 2013 m 8
this code i've used:
sort id year grade gen repeat_flag = . replace repeat_flag = 1 if year!=year[_n+1] & grade==grade[_n+1] /// & subject!=subject[_n+1] & id==id[_n+1] replace repeat_flag = 0 if repeat_flag==.
one problem there lot of students took test in 6 grade, didn't take 1 in 7th , took 1 in 8th grade. varies across years , school districts, school districts adopted tests in different years different grade levels. code doesn't account this.
regardless though think there must more elegant ways , side note wanted know if use of indexes appropriate problem this. thanks!
edit included sample of data looks above in response 1 of comments below. if still not clear feedback welcomed.
what may seem anomalous students progressing faster or more in tested grade passage of time imply. that's possibly 1 line grunt work:
clear input id year str1 subject tested_grade 1 2011 m 10 1 2012 m 11 1 2013 m 12 2 2011 r 4 2 2012 r 7 2 2013 r 8 3 2011 m 6 3 2013 m 8 end bysort id (year) : gen flag = (tested - tested[_n-1]) - (year - year[_n-1]) list if flag != 0 & flag < . , sepby(id) +---------------------------------------+ | id year subject tested~e flag | |---------------------------------------| 5. | 2 2012 r 7 2 | +---------------------------------------+
Comments
Post a Comment