Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: generating a moving sum


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: generating a moving sum
Date   Sun, 23 Nov 2003 17:31:02 -0000

I hate to say it, but a loop is one way out. 

levels hospid, local(H) 
levels doadmit, local(D) 
gen nprev90 = . 

foreach h of local H { 
	foreach d of local D { 
		qui count if hospid == `h' & doa < `d' & doa >= (`d' - 90) 
		replace nprev90 = r(N) if hospid == `h' & doa == `d' 
	} 
} 

Nick 
[email protected] 

Alexander Tsai
 
> I have a dataset of hospital admissions: admissions indexed by
> "admitid", patients indexed by "ptid", hospitals indexed by 
> "hospid".
> 
>        +-------------------------------------------+
>        |   doadmit   admitid         ptid   hospid |
>        |-------------------------------------------|
>     1. | 21 Jun 91       160       007336        1 |
>     2. | 23 Jun 91       197       007695        1 |
>     3. | 28 Jun 91       419       126888        1 |
>     4. | 30 Jun 91       550       046134        1 |
>     5. | 02 Jul 91       696       015142        1 |
>        |-------------------------------------------|
>     6. | 03 Jul 91       771       035194        1 |
>     7. | 05 Jul 91       906       006762        1 |
>     8. | 06 Jul 91       989       114879        1 |
>     9. | 07 Jul 91      1040       010445        1 |
>    10. | 11 Jul 91      1328       035194        1 |
>        |-------------------------------------------|
>    11. | 12 Jul 91      1403       016055        1 |
>    12. | 12 Jul 91      1402       001565        1 |
>    13. | 15 Jul 91      1609       007721        1 |
>    14. | 16 Jul 91      1694       015450        1 |
>    15. | 18 Jul 91      1856       005970        1 |
>        |-------------------------------------------|
> 	 |			etc...			   |
> 
> What I would like to do is this: for each 
> patient-admission, calculate
> the total number of admissions at that hospital in the 
> prior 90 days.
> 
> On Sep 3 '03, Kristien Verheyen posted a message to the listserv
> (subject: "Generating 'moving sum' variable") that had to do with
> calculating moving sums, but my data are a little more 
> complicated. My
> first thought was to expand the data with dummy dates of 
> admission and
> generate moving sums by the prior 90 rows, but then I run into the
> problem that some hospitals have multiple admissions on one day.
> 

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