Dear Statalisters,
first of all, thumbs up and an honest "thank you" for all the great contributions in this forum! A while ago I started working with Stata and since then I came accross this forum a dozen of times and it really helped me out with some issues.
However, I am now dealing with an issue I can't really grasp. I am working with panel data on country-year level in which the countries are exposed to a certain spell (e.g. policy measure XY). For further analysis, I want to calculate the following time trends:
Here my artificial data:
Any suggestions are highly appreciated. I don't really have an idea how to tackle this problem. For instance about the column "YearsBefore" - how do I tell Stata to stop counting backwards if the next spell starts (Spell==1) ?
Best,
MH (who uses Stata 15 btw)
first of all, thumbs up and an honest "thank you" for all the great contributions in this forum! A while ago I started working with Stata and since then I came accross this forum a dozen of times and it really helped me out with some issues.
However, I am now dealing with an issue I can't really grasp. I am working with panel data on country-year level in which the countries are exposed to a certain spell (e.g. policy measure XY). For further analysis, I want to calculate the following time trends:
- Duration of a spell (already solved with: "tsspell Spell, cond(Spell==1)")
- YearsAfter: Years after a spell (= years that passed since completion of last spell until time t or until the next spell is completed)
- YearsBefore: Years before spell (= X years until next program starts)
Code:
// use tsspell which creates following columns: _seq _spell _end ssc install tsspell tsspell Spell, cond(Spell==1)
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str1 Country int Year byte(Spell YearsAfter YearsBefore) "A" 2000 0 0 3 "A" 2001 0 0 2 "A" 2002 0 0 1 "A" 2003 1 0 0 "A" 2004 1 0 0 "A" 2005 0 1 1 "A" 2006 1 0 0 "A" 2007 1 0 0 "A" 2008 0 1 4 "A" 2009 0 2 3 "A" 2010 0 3 2 "A" 2011 0 4 1 "A" 2012 1 5 0 "A" 2013 1 6 0 "A" 2014 1 7 0 "A" 2015 0 1 0 "B" 2000 1 0 0 "B" 2001 1 0 0 "B" 2002 0 1 5 "B" 2003 0 2 4 "B" 2004 0 3 3 "B" 2005 0 4 2 "B" 2006 0 5 1 "B" 2007 1 0 0 "B" 2008 1 0 0 "B" 2009 1 0 0 "B" 2010 0 1 4 "B" 2011 0 2 3 "B" 2012 0 3 2 "B" 2013 0 4 1 "B" 2014 1 5 0 "B" 2015 1 6 0 "C" 2000 0 1 1 "C" 2001 1 2 0 "C" 2002 1 3 0 "C" 2003 0 1 5 "C" 2004 0 2 4 "C" 2005 0 3 3 "C" 2006 0 4 2 "C" 2007 0 5 1 "C" 2008 1 6 0 "C" 2009 1 7 0 "C" 2010 1 8 0 "C" 2011 0 1 3 "C" 2012 0 2 2 "C" 2013 0 3 1 "C" 2014 1 4 0 "C" 2015 1 5 0 end format %ty Year
Any suggestions are highly appreciated. I don't really have an idea how to tackle this problem. For instance about the column "YearsBefore" - how do I tell Stata to stop counting backwards if the next spell starts (Spell==1) ?
Best,
MH (who uses Stata 15 btw)
Comment