Markus,
It can be solved, of course. While I was working on this, Nick pointed to some general principles. However, I think you should see my solution.
My brain works best with events in a forward sequence, so I resorted your data.
-stay2- really was not used; I just put it in.
-edate- is the most recent event date for this person.
-edate2- is created if there was no prior event for this person, or if the most recent event was > 10 years (3652.5 days) ago.
Was this what you were looking for?
Svend
. use events.dta , clear
. sort id startdate
. bysort id: gen stay2=_n
.
. gen edate=startdate if event==1
(10 missing values generated)
. replace edate=edate[_n-1] if id == id[_n-1] & edate==.
(2 real changes made)
. gen edate2=edate if id != id[_n-1]
(15 missing values generated)
. replace edate2=edate if edate-edate[_n-1] > 3652.5
(3 real changes made)
. format edate edate2 %d
.
. slist
id startdate enddate event stay adm_total stay2 edate edate2
1. 001 12apr1976 24apr1976 0 6 7 1 . .
2. 001 12apr1976 24apr1976 1 7 7 2 12apr1976 12apr1976
3. 001 12jan1994 12mar1994 0 5 7 3 12apr1976 .
4. 001 03may1998 25may1998 1 4 7 4 03may1998 03may1998
5. 001 23oct2002 24oct2002 0 3 7 5 03may1998 .
6. 001 17jan2004 28feb2004 1 2 7 6 17jan2004 .
7. 001 23jun2004 13jul2004 1 1 7 7 23jun2004 .
8. 002 11oct2000 18oct2003 0 2 2 1 . .
9. 002 04feb2003 28feb2003 0 1 2 2 . .
10. 003 12jun1976 24apr1976 0 6 6 1 . .
11. 003 30jan1994 12mar1994 0 5 6 2 . .
12. 003 26may1999 31may1999 0 4 6 3 . .
13. 003 08nov2001 12nov2001 0 3 6 4 . .
14. 003 02apr2003 04apr2003 0 2 6 5 . .
15. 003 15mar2004 26mar2004 1 1 6 6 15mar2004 15mar2004
________________________________________________________
Svend Juul
Institut for Folkesundhed, Afdeling for Epidemiologi
(Institute of Public Health, Department of Epidemiology)
Vennelyst Boulevard 6
DK-8000 Aarhus C, Denmark
Phone, work: +45 8942 6090
Phone, home: +45 8693 7796
Fax: +45 8613 1580
E-mail: [email protected]
_________________________________________________________
*
* 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/