Dear All, The question is from here (in Chinese). Suppose the data set is
Code:
* Example generated by -dataex-. For more info, type help dataex
clear
input str12 stkcd int year byte multi
"000002" 2009 0
"000002" 2010 0
"000002" 2011 0
"000002" 2012 0
"000002" 2013 0
"000002" 2014 1
"000002" 2015 1
"000002" 2016 1
"000002" 2017 1
"000002" 2018 1
"000002" 2019 1
"000004" 2011 0
"000004" 2012 0
"000004" 2013 0
"000004" 2014 0
"000004" 2015 1
"000004" 2016 1
"000004" 2017 0
"000004" 2018 0
"000004" 2019 0
"000005" 2009 0
"000005" 2010 0
"000005" 2011 0
"000005" 2012 0
"000005" 2013 0
"000005" 2014 0
"000005" 2015 0
"000005" 2016 0
"000005" 2017 0
"000005" 2018 0
"000005" 2019 0
"000006" 2009 0
"000006" 2010 1
"000006" 2011 1
"000006" 2012 1
"000006" 2013 1
"000006" 2014 0
"000006" 2015 0
"000006" 2016 0
"000006" 2017 0
"000006" 2018 0
"000006" 2019 0
"000007" 2012 0
"000007" 2013 0
"000007" 2014 0
"000007" 2015 0
"000007" 2016 1
"000008" 2013 0
"000008" 2014 0
"000008" 2015 0
"000008" 2016 0
"000008" 2017 0
"000008" 2018 1
"000008" 2019 1
"000009" 2011 0
"000009" 2012 0
"000009" 2013 0
"000009" 2014 0
"000009" 2015 0
"000009" 2016 0
"000009" 2017 0
"000009" 2018 0
"000009" 2019 0
"000010" 2013 1
"000010" 2014 1
"000010" 2015 0
"000010" 2016 0
"000010" 2017 0
"000010" 2018 0
"000011" 2009 0
"000011" 2010 0
"000011" 2011 0
"000011" 2012 0
"000011" 2013 0
"000011" 2014 0
"000011" 2015 0
"000011" 2016 0
"000011" 2017 0
"000011" 2018 0
"000011" 2019 0
"000012" 2015 0
"000012" 2016 0
"000012" 2017 0
"000012" 2018 0
"000012" 2019 0
"000014" 2009 0
"000014" 2010 0
"000014" 2011 0
"000014" 2012 0
"000014" 2013 0
"000014" 2014 0
"000014" 2015 1
"000014" 2016 1
"000014" 2017 0
"000014" 2018 0
"000014" 2019 0
"000016" 2009 0
"000016" 2010 0
"000016" 2011 0
"000016" 2012 0
end
The purpose is to generate two variables, v1 and v2.
  1. For each `stkcd', if a value 0 of `multi' is (imediately) followed by 1, then v1=1 for this `stkcd', 0 otherwise.
  2. In contrast, for each `stkcd', if a value 1 of `multi' is (imediately) followed by 0, then v2=1 for this `stkcd', 0 otherwise.
Any suggestions are highly appreciated.