[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: running sum with two conditions - spells and time

From   "Benjamin Appel" <[email protected]>
To   <[email protected]>
Subject   st: running sum with two conditions - spells and time
Date   Thu, 31 Jan 2008 15:34:16 -0500

Hi All:

I have a unbalanced panel data set with monthly observations. The panel id is: dispno2 and the time id is: date (formatted %tm). The variable of interest is called: stale. It is a dummy variable (0/1) that measures stalemates in negotiations. I would like to generate a running sum, but with two conditions. First, the sum restarts after stale is 0 and the sum only counts consecutive stalemates in the past 60 months. 

More clearly, I would like to generate a variable that counts the consecutive number of stalemates (stale=1) in the past five years.

Similar questions have been discussed at length on the Statalist, but I haven't been able to figure it out. Based on the thread at ( I was able to use the user written tool tsspell to solve the problem of restarting after every zero (or missing value based on the thread), but I can't quite figure out how to create a five year running sum within each spell. My completely copied code is below. Any help would be greatly appreciated. Thank you very much!

tsset dispno2 date
tsspell, cond(!missing(stale))


gen stalecon=.
bysort dispno2 (date): replace stalecon=cond(missing(stale[_n-1]), stale, stale+stalecon[_n-1])

*replaced stale=0 with missing to follow code on Statalist

Benjamin Appel
Ph.D. Student, GVPT at UMD

*   For searches and help try:

© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index