Please put informative titles on your postings.
This problem has some similarities to that in a
current thread in which Scott Merryman and I have posted
answers. In that thread, Scott recommended use
of -tsspell- from SSC.
By and large, no looping (e.g. with -foreach-)
should be needed at all. It should all yield
to -by:-.
If you
sort id startdate
you can then go
by id : gen obsno = _n
tsset id obsno
by id : gen timesinceprevious = startdate - startdate[_n-1]
You can define spells by the condition that
the first time is greater than your threshold.
Nick
[email protected]
Markus Steiner, MSc
> I have a data set with one or more hospital admissions each.
> The admission could
> have been because of the occurance of a specific disease
> (here coded as event).
> I want to code every event as an first event (incident case),
> if the person was
> not admitted to hospital for the same reason in the previous
> 10 (or 5) years
> before.
> The idea was, to do some sort of look back, if an event is an
> incident case. I
> wanted to check for every patient separately (by id:...):
> 1. has the current obs an event? If not, continue with the
> next observation; if
> yes, step 2.
> 2. check if the next observation has an event too? If not,
> check the second next
> observation (and so on) for an event - till end of
> observations for this person.
> If an event occurs, continue with step 3.
> 3. check the time between the both events. If the time
> between the both events
> is 10(5) years or more the event (from step 1) is tagged as
> incident case. If
> the time difference is less then 10(5) years the event from
> step 1 is not
> tagged as a case and we continue the same procedure starting
> with step 2 again
> with the new event from step 3.
>
> The dataset looks like that:
>
> id startdate enddate event stay adm_total
> 001 23/06/2004 13/07/2004 1 1 7
> 001 17/01/2004 28/02/2004 1 2 7
> 001 23/10/2002 24/10/2002 0 3 7
> 001 03/05/1998 25/05/1998 1 4 7
> 001 12/01/1994 12/03/1994 0 5 7
> 001 12/04/1976 24/04/1976 0 6 7
> 001 12/04/1976 24/04/1976 1 7 7
> 002 04/02/2003 28/02/2003 0 1 2
> 002 11/10/2000 18/10/2003 0 2 2
> 003 15/03/2004 26/03/2004 1 1 6
> 003 02/04/2003 04/04/2003 0 2 6
> 003 08/11/2001 12/11/2001 0 3 6
> 003 26/05/1999 31/05/1999 0 4 6
> 003 30/01/1994 12/03/1994 0 5 6
> 003 12/06/1976 24/04/1976 0 6 6
>
> and the result should look like that with 10 years and 5
> years look back:
>
> id startdate enddate event stay adm_total case10y case5y
> casedate10y ...
> 001 23/06/2004 13/07/2004 1 1 7 0 0 .
> 001 17/01/2004 28/02/2004 1 2 7 0 1 .
> 001 23/10/2002 24/10/2002 0 3 7 0 0 .
> 001 03/05/1998 25/05/1998 1 4 7 1 1
> 03/05/1998
> 001 12/01/1994 12/03/1994 0 5 7 0 0 .
> 001 12/04/1976 24/04/1976 0 6 7 0 0 .
> 001 12/04/1976 24/04/1976 1 7 7 1 1
> 12/04/1976
> 002 04/02/2003 28/02/2003 0 1 2 0 0 .
> 002 11/10/2000 18/10/2003 0 2 2 0 0 .
> 003 15/03/2004 26/03/2004 1 1 6 1 1
> 15/03/2004
> 003 02/04/2003 04/04/2003 0 2 6 0 0 .
> 003 08/11/2001 12/11/2001 0 3 6 0 0 .
> 003 26/05/1999 31/05/1999 0 4 6 0 0 .
> 003 30/01/1994 12/03/1994 0 5 6 0 0 .
> 003 12/06/1976 24/04/1976 0 6 6 0 0 .
>
> I have tried foreach but first you can not combine it with
> by...: and second I
> could not use variables (to account that for each person I
> has a different
> number of admissions) with it (something like: foreach x =
> stay/adm_total).
> It might be easier to reduce the data set to these only with
> an event first, but
> this might reduce the application in a general manner (e.g.
> stay and adm_total
> have to be recalculated for evey new sort of event, if realy needed).
>
> id startdate enddate event stay adm_total
> 001 23/06/2004 13/07/2004 1 1 7
> 001 17/01/2004 28/02/2004 1 2 7
> 001 03/05/1998 25/05/1998 1 4 7
> 001 12/04/1976 24/04/1976 1 7 7
> 003 15/03/2004 26/03/2004 1 1 6
>
> Does anyone had to write a similar program in Stata and a
> solution for this
> problem ready or any idea how I can get my look back via
> looping through all
> remaining observations of one person to work?
*
* 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/