Dear users,
I have put together a code in Stata which identefies overlaps between dates in my dataset (see below). Unfortanately the code doesnt run for all observation. Can someone guide me on how to change my codes so that it workf for all patients.
Many thanks,
Ivy
bysort id: gen nnum=_n
bysort id: gen nnum2=_N
format adm %d
format dis %d
gen curadm=adm
format curadm %d
gen curdis=dis
format curdis %d
forvalues i=2/`=nnum2' {
local lastobs=`i'-1
*if id[`i']!=id[`i'-1] {
* replace curadm=adm in `i'
* replace curdis=dis in `i'
*replace curadm=curadm[`i'-1] in `lastobs'
*replace curdis=curdis[`i'-1] in `lastobs'
*}
//if the ith discharge date is less than the previous discharge date
//then replace the ith discharge date with the previous discharge date
else {
if dis[`i']<curdis[`i'-1] {
replace curdis=curdis[`i'-1] in `i'
}
// if this is not the case then keep the current discharge
else {
replace curdis=dis in `i'
}
}
// if the admission date for the ith discharge is less than or
// equal to the previous discharge date
//then replaces the ith admission date with the previous admission date
//replace all discharge dates with a missing value except the final discharge
//in the overlap set
if adm[`i']<=curdis[`i'-1] {
replace curadm=curadm[`i'-1] in `i'
replace curdis=. in `lastobs'
// if this is not true then keep the current admission date and
//current discharge
else{
replace curadm=curadm in `i'
replace curdis=curdis in `i'
}
}
}
I have put together a code in Stata which identefies overlaps between dates in my dataset (see below). Unfortanately the code doesnt run for all observation. Can someone guide me on how to change my codes so that it workf for all patients.
Many thanks,
Ivy
bysort id: gen nnum=_n
bysort id: gen nnum2=_N
format adm %d
format dis %d
gen curadm=adm
format curadm %d
gen curdis=dis
format curdis %d
forvalues i=2/`=nnum2' {
local lastobs=`i'-1
*if id[`i']!=id[`i'-1] {
* replace curadm=adm in `i'
* replace curdis=dis in `i'
*replace curadm=curadm[`i'-1] in `lastobs'
*replace curdis=curdis[`i'-1] in `lastobs'
*}
//if the ith discharge date is less than the previous discharge date
//then replace the ith discharge date with the previous discharge date
else {
if dis[`i']<curdis[`i'-1] {
replace curdis=curdis[`i'-1] in `i'
}
// if this is not the case then keep the current discharge
else {
replace curdis=dis in `i'
}
}
// if the admission date for the ith discharge is less than or
// equal to the previous discharge date
//then replaces the ith admission date with the previous admission date
//replace all discharge dates with a missing value except the final discharge
//in the overlap set
if adm[`i']<=curdis[`i'-1] {
replace curadm=curadm[`i'-1] in `i'
replace curdis=. in `lastobs'
// if this is not true then keep the current admission date and
//current discharge
else{
replace curadm=curadm in `i'
replace curdis=curdis in `i'
}
}
}
This is what I get | ||||||
id | adm | dis | nnum | nnum2 | curadm | curdis |
x | 01/01/2012 | 03/02/2012 | 1 | 4 | 01/01/2012 | 03/02/2012 |
x | 02/01/2012 | 18/03/2012 | 2 | 4 | 01/01/2012 | 03/02/2012 |
x | 09/01/2012 | 25/03/2012 | 3 | 4 | 09/01/2012 | 03/02/2012 |
x | 28/03/2012 | 03/04/2012 | 4 | 4 | ||
y | 01/03/2012 | 03/03/2012 | 1 | 7 | ||
y | 03/03/2012 | 06/03/2012 | 2 | 7 | ||
y | 06/03/2012 | 08/03/2012 | 3 | 7 | ||
y | 10/03/2012 | 13/03/2012 | 4 | 7 | ||
y | 02/04/2012 | 03/04/2012 | 5 | 7 | ||
y | 03/04/2012 | 05/04/2012 | 6 | 7 | ||
y | 04/04/2012 | 04/04/2012 | 7 | 7 | ||
this is what I want | ||||||
id | adm | dis | nnum | nnum2 | curadm | curdis |
x | 01/01/2012 | 03/02/2012 | 1 | 4 | 01/01/2012 | 25/01/2012 |
x | 02/01/2012 | 18/03/2012 | 2 | 4 | - | - |
x | 09/01/2012 | 25/03/2012 | 3 | 4 | - | - |
x | 28/03/2012 | 03/04/2012 | 4 | 4 | 28/03/2012 | 03/04/2012 |
y | 01/03/2012 | 03/03/2012 | 1 | 7 | 01/03/2012 | 08/03/2012 |
y | 03/03/2012 | 06/03/2012 | 2 | 7 | - | - |
y | 06/03/2012 | 08/03/2012 | 3 | 7 | - | - |
y | 10/03/2012 | 13/03/2012 | 4 | 7 | 10/03/2012 | 13/03/2012 |
y | 02/04/2012 | 03/04/2012 | 5 | 7 | 02/04/2012 | 05/04/2012 |
y | 03/04/2012 | 05/04/2012 | 6 | 7 | - | - |
y | 04/04/2012 | 04/04/2012 | 7 | 7 | - | - |
Comment