Hi all, please consider the following example data
I want to flag consecutive observations that have open==1. Accordingly I have tried
But it appears only
is being run/read/considered, as any consecutive occurrences in open are being flagged, not just open==1. I want my flag variable to look like flag_repeat_exp where consecutive open==1 are flagged, but not sure how to work that in.
Appreciate your suggestions.
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str3 studentid float acadyr str1 postcode float(school open flag_repeat flag_repeat_exp) "123" 2010 "A" 111 1 1 1 "123" 2011 "A" 111 1 0 0 "123" 2012 "B" 111 0 1 0 "123" 2013 "C" 111 0 0 0 "123" 2014 "A" 111 1 1 1 "123" 2015 "D" 111 1 0 0 "124" 2012 "S" 111 0 0 0 "124" 2013 "C" 111 1 1 1 "124" 2015 "C" 112 1 1 1 "124" 2016 "C" 112 1 0 0 "124" 2017 "S" 111 0 0 0 "125" 2007 "A" 111 1 1 1 "125" 2008 "A" 112 1 1 1 "125" 2009 "A" 111 1 0 0 "126" 2012 "S" 111 0 1 0 "126" 2014 "B" 112 0 1 0 "126" 2015 "C" 112 0 0 0 "126" 2016 "D" 111 1 1 1 "126" 2017 "A" 112 1 1 1 "126" 2018 "A" 114 1 0 0 end
I want to flag consecutive observations that have open==1. Accordingly I have tried
Code:
sort studentid acadyr gen flag_repeat=0 by studentid: replace flag_repeat=1 if open[_n]==open[_n+1]==1
Code:
by studentid: replace flag_repeat=1 if open[_n]==open[_n+1]
Appreciate your suggestions.
Comment