# Re: st: RE: nested loops for calculating a population 'at risk'

 From David Kantor To statalist@hsphsun2.harvard.edu Subject Re: st: RE: nested loops for calculating a population 'at risk' Date Mon, 20 Sep 2004 14:14:45 -0400

At 06:37 PM 9/20/2004 +0100, Sue Lee wrote:
Many thanks to those kind souls who responded to my initial query... i
think perhaps i was not clear enough about what i am trying to do... so
i'll try explaining again!

the dataset contains a record for each baby born on each day from 1988
to 2000.  we have a variable for birthdate and a variable for gestation
at birth in days (dgest).

i am interested in how to generate the following algorithm into
workable STATA code:

1. for each date from 1 jan 1988 to 31 dec 2000
2. calculate the number of records that have dgest > 167 and < 259 on
the current day  (these are the 'cases at risk')
3.  move forward a day
4.  increase both dgest values by 1 and repeat step 2 (i.e., calculate
the number of records on this day that have dgest > 168 and <260...
these are the cases that were 'at risk' the day before)
5. move forward a day
6.  repeat step 4 and 5 until max dgest value reached
7.  sum the number of records 'at risk' for each day

i'd be most grateful for any help or suggestions received!
If I understand correctly -- that the bounds 167 and 259 apply to 02jan1988, and 168 and 260 apply to 02jan1988, and so on, then try this: (I'm not sure if this is right, since this will lead to absurdly long gestation limits. But then you just get nobody at-risk after some point.)

gen int dgestmin = birthdate - d(01jan1988) + 167
gen int dgestmax = dgestmin + 92
gen byte atrisk = dgest > dgestmin & dgest < dgestmax

bysort birthdata: egen long numatrisk = sum(atrisk)

You can then reduce to one record per birthdate. Alternatively, the instead of the -egen-,
you can -collapse-, taking the sum of atrisk.

I hope this helps.
--David

David Kantor
Institute for Policy Studies
Johns Hopkins University
dkantor@jhu.edu
410-516-5404

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