bysort hhid mid (surveyyear) : gen dropped = inschool[1] == 1 & inschool[2] == 0
The commentary is longer than the code:
Within blocks of observations with the same
hhid
andmid
, sort bysurveyyear
.You want students who were
inschool
in year 3 but not in year 4. So,inschool
is 1 in the first observation and 0 in the second.Here subscripting
[1]
and[2]
refers to order within blocks of observations defined by theby:
statement.
If further detail is needed see e.g. this article. Note that contrary to one tag, no loop is needed (or, if you wish, that the loop over possibilities is built in to the by:
framework).