Statalist


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

st: RE: running sum with two conditions - spells and time


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: running sum with two conditions - spells and time
Date   Thu, 31 Jan 2008 21:19:09 -0000

Successful uses of -tsspell- should be matched by donations to the
Hogwarts Rebuilding Fund. 

You now have a structure given first by panel identifier -dispno2- and
second by spell identifier -_spell-. 

Within that cumulative sums are defined by 

. bysort dispno2 _spell (date) : gen sum_stale = sum(stale) 

And the sum of the last five dates is then 

. by dispno2 _spell : gen prev5 = sum_stale - sum_stale[_n - 60] 

The last device I learned from Michael Blasnik. 

Nick 
[email protected] 

Benjamin Appel

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
(http://www.stata.com/statalist/archive/2007-07/msg00602.html) 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))

Or,

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


*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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