I want to fill in values between date ranges.
For example, someone is good until they are bad. And then once they are bad, they remain bad until they switch to good.
Here is my example:
And the solution would look like this:
Where the goodpath/badpath variables take on the trailing values until there is a switch from good to bad or vice versa. To be clear, I am looking for a way to create the goodpath and badpath variables.
I can do this if there is only 1 switch (from good to bad or vice versa). If only one switch, you find first occurrence of good and first occurrence of bad.
But I get tripped up when there are multiple switches (good --> bad --> good, etc). Don't know how to deal with the third, fourth, etc switches.
I could code one line for every possible scenario, but that seems inefficient.
Thanks for your help in advance
For example, someone is good until they are bad. And then once they are bad, they remain bad until they switch to good.
Here is my example:
Code:
clear input year int good int bad 2000 1 0 2001 0 0 2002 0 1 2003 0 0 2004 0 0 2005 0 0 2006 1 0 2007 0 0 2008 0 1 end
Code:
clear input year int good int bad goodpath badpath 2000 1 0 1 0 2001 0 0 1 0 2002 0 1 0 1 2003 0 0 0 1 2004 0 0 0 1 2005 0 0 0 1 2006 1 0 1 0 2007 0 0 1 0 2008 0 1 0 1 end
I can do this if there is only 1 switch (from good to bad or vice versa). If only one switch, you find first occurrence of good and first occurrence of bad.
Code:
egen first_yr_good = min(cond(good == 1, year, .)) egen first_yr_bad = min(cond(bad == 1, year, .)) gen goodpath=1 if year>=first_yr_good replace goodpath=0 if year>=first_yr_bad & first_yr_good < first_yr_bad
I could code one line for every possible scenario, but that seems inefficient.
Thanks for your help in advance
Comment