Statalist


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

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


From   "Benjamin Appel" <[email protected]>
To   <[email protected]>
Subject   Re: st: RE: running sum with two conditions - spells and time
Date   Thu, 31 Jan 2008 17:25:10 -0500

Thanks, Nick. I now understand adding the second structure. However, I still have problems creating the five year running sum. I get missing values for all variables after I try your code. 

I include below a small sample of my data below to help clarify the issue.  Is the problem related to the seemingly random and very unbalanced dates in the data. Or is %tm wrong for my date variable. The goal is to get prev5 to include a count of  all stalemates (in each spell) only in the past five years rather than the cummulative sum as in the sum_stale variable.

Again, thank you for your assistance and I apologize for my ambiguity in my previous email. Thanks,Ben.

PS fresh out of Galleons at this time...but Ill see what I can do (especially if I can figure this out after spending the past several hours on it). 

tsset dispno2 date
tsspell, cond(!missing(stale))
bysort dispno2 _spell (date) : gen sum_stale = sum(stale)
by dispno2 _spell : gen prev5 = sum_stale - sum_stale[_n - 60]
(1141 missing values generated)

dispno2	date	stale	sum_stale	prev5
10	1976m10	  1	     1	
10	1978m6	   1	      2 	
10	1980m4		       0	
10	1981m12	  1	     1	
10	1982m10	  1	     2	
10	1983m10	  1	     3	
10	1984m10	  1	     4	
10	1985m10	  1	     5	
10	1986m10	  1	     6	
10	1987m10	  1	     7	- would like this to be 6 
10	1988m10	  1	     8	  ""   6 again
10	1989m10	  1 	     9	  "" 6, etc.
10	1990m10	  1	    10	
10	1991m10	  1	    11	
10	1992m10	  1	    12	
10	1993m10	  1	    13	
10	1994m10	  1	    14	
10	1995m10	  1	    15	


Benjamin Appel
Ph.D. Student, GVPT at UMD

>>> "Nick Cox" <[email protected]> 01/31/08 4:19 PM >>>
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/


*
*   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