stata - Reducing code when creating prior year test score in opposite subject -
i have data looks , variable i'm trying create math_score
:
year id subject score math_score 2011 1 m 30 30 2011 1 r 40 30 2012 2 m 50 50 2012 2 r 60 50
my colleague , have come following way of doing this:
bys id year: egen math = mode(score) if subject=="m" id year: egen math_score = max(math)
or:
bys id year: gen math_score = score[_n-1] replace math_score = math_score[_n+1] if math_score==.
ultimately going used in lagged variable denote prior year test score in subject , in opposite subject. we're sure there's more elegant way of doing less code can't seem think of one. ideas?
it's lot easier in one:
clear input year id str1 subject score 2011 1 m 30 2011 1 r 40 2012 2 m 50 2012 2 r 60 end egen math_score = mean(score / (subject == "m")), by(id year)
documented here.
we're dividing true-or-false expression evaluated 0 or 1. if result 1, score
used is. if result 0, result missing , ignored, precisely want here.
that said, may better off different data structure:
clear input year id str1 subject score 2011 1 m 30 2011 1 r 40 2012 2 m 50 2012 2 r 60 end reshape wide score, i(id year) j(subject) string list, sepby(id) +-----------------------------+ | year id scorem scorer | |-----------------------------| 1. | 2011 1 30 40 | |-----------------------------| 2. | 2012 2 50 60 | +-----------------------------+
Comments
Post a Comment