Dear Statalist,
I'm doing research on hemodialysis catheters. Patients can have multiple catheters in at the same time. I want to analyse patient follow up using date ranges in which >=1 catheter is in, and date ranges in which they are catheter free
I have panel data, in a format provided in the code below. I've tried to define spells of catheters by identifying overlaps, which successfully defines the first observation in the spell. However, sometimes a catheter that is several rows below the first observation in the spell, should also be included in the spell. I'm not sure how to create a spell condition which relates a given row to all previous rows in the spell.
Specifically, I get the following data structure, but I want the spells to cover the rows I have circled in red

Any help would be most appreciated.
Kind regards,
Ben
I'm doing research on hemodialysis catheters. Patients can have multiple catheters in at the same time. I want to analyse patient follow up using date ranges in which >=1 catheter is in, and date ranges in which they are catheter free
I have panel data, in a format provided in the code below. I've tried to define spells of catheters by identifying overlaps, which successfully defines the first observation in the spell. However, sometimes a catheter that is several rows below the first observation in the spell, should also be included in the spell. I'm not sure how to create a spell condition which relates a given row to all previous rows in the spell.
Specifically, I get the following data structure, but I want the spells to cover the rows I have circled in red
Any help would be most appreciated.
Kind regards,
Ben
Code:
input ptid insertdate removaldate
1 100 199
1 300 900
1 350 400
1 450 500
1 600 800
1 900 1000
1 1100 1200
1 1210 1300
1 1400 1500
1 1600 1700
1 1800 1900
2 0 1100
2 250 500
2 500 1000
2 1010 1200
2 1300 1500
2 1700 1800
2 1900 1950
2 2000 2050
2 2055 2150
end
bysort ptid (insertdate removaldate): gen pstime = _n
tsset ptid pstime
tsspell, cond(removaldate >= insertdate[_n+1] & insertdate <= removaldate[_n+1])
by ptid: replace _spell = _spell[_n-1] if _spell==0 & _spell[_n-1] != 0 & _spell[_n-1]!=. ///
& insertdate <= removaldate[_n-1] & removaldate[_n-1]!=.
gen byte true_end = 0
by ptid: replace true_end = 1 if _end[_n-1]==1 & insertdate <= removaldate[_n-1] & removaldate[_n-1]!=.
by ptid: replace _seq = (L._seq + 1) if true_end == 1
list, sepby(ptid) noobs

Comment