Dear Statalists
I am not entirely sure how describe my goal accurately in a few words, so sorry for the potentially misleading title. Here is what I would like to do:
I have data over a number of years, say, 2000 to 2010 that looks like the following:
My goal is to find out the highest number of consecutive 1s in each observation in the period. For "001" that would be 3, for "003" it would be 5. Additionally, I would like to have a way to systematically determine in which year the longest period of consecutive 1s started and in which it ended. For "001" that would be in 2000 (start) and 2002 (end) etc. Such that the resulting data would look like the following.
As a bonus, if there are two different periods with the same length of consecutive 1s as in "004", I would like to have the start and end years of the first and an indication that there is more than one such period (variable name here: multiple).
I am not looking for perfect code (although I would not mind it either) but rather for some conceptual advice as to how I might approach. Everything I can think of at the moment would make things very complicated, especially considering that in the full data I have a period of 50 years and over 3000 observations.
Does anyone know how I might approach the question? Any advice is much appreciated.
Thank you all.
Best wishes
Milan
I am not entirely sure how describe my goal accurately in a few words, so sorry for the potentially misleading title. Here is what I would like to do:
I have data over a number of years, say, 2000 to 2010 that looks like the following:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str3 id float(y2000 y2001 y2002 y2003 y2004 y2005 y2006 y2007 y2008 y2009 y2010) "001" 1 1 1 0 0 0 0 0 0 0 0 "002" 1 0 0 1 1 1 0 0 0 0 0 "003" 0 0 0 0 0 1 1 1 1 1 0 "004" 0 1 1 1 0 0 0 1 1 1 0 end
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str3 id float(y2000 y2001 y2002 y2003 y2004 y2005 y2006 y2007 y2008 y2009 y2010 longest_consecutive start end multiple) "001" 1 1 1 0 0 0 0 0 0 0 0 3 2000 2002 0 "002" 1 0 0 1 1 1 0 0 0 0 0 3 2003 2005 0 "003" 0 0 0 0 0 1 1 1 1 1 0 5 2005 2009 0 "004" 0 1 1 1 0 0 0 1 1 1 0 3 2001 2003 1 end
I am not looking for perfect code (although I would not mind it either) but rather for some conceptual advice as to how I might approach. Everything I can think of at the moment would make things very complicated, especially considering that in the full data I have a period of 50 years and over 3000 observations.
Does anyone know how I might approach the question? Any advice is much appreciated.
Thank you all.
Best wishes
Milan
Comment