I want to count gaps between investment types in my panel data. For example, I know when a firm invested positively, negatively or was inactive. What I would like to do is to sum up number of years between negative investment and positive, and potentially another count would be between negative or inactive and positive. In other words, I want to count how many negative investments occurred before a positive investment: the counter resets each time a positive investment occurs, and similarly with inactivity and negative in the second calculation. In all cases I want to ignore missing values. Here's an example of how I would like my table to look like:
id Year invType invType GapNegPos GapAllPos
1 2000 1 Negative
1 2001 1 Negative
1 2002 1 Negative
1 2003 2 Positive 3 3
1 2004 1 Negative
1 2005 1 Negative
1 2006 2 Positive 2 2
1 2007 1 Negative
1 2008 1 Negative
1 2009 1 Negative
1 2010 3 Inactivity
1 2011 1 Negative
1 2012 1 Negative
1 2013 1 Positive 2 6
2 2000 .
2 2001 .
2 2002 .
2 2003 .
2 2004 2 Positive 0 0
2 2005 2 Positive 0 0
2 2006 .
2 2007 .
2 2008 3 Positive 0 0
2 2009 2 Inactivity
2 2010 1 Negative
2 2011 1 Negative
2 2012 2 Positive 2 3
2 2013 1 Negative
stata
tag wiki for advice to usedataex
. You should show some attempt at code.