Hi again,
I previously wrote a question and received some great help, but I now want to make it a bit more complicated and the same script does not seem to work.
We have a dataset following patients after a surgical intervention. The patients are followed up annually, however, in reality this means annually ish. Therefore, we have decided that patients who was seen between the days 548 and 914 are counted as annual followup year two (counted as 365,25x2=730,5), by using this code:
We need to have them in different dataset divided per year.
Some patients have two observation within the time-span. To pick the one closest to the year-mark, I have used:
We have now realized that some patients have two observations during what we count as "year 1", e.g. day 361 and day 547, but none during year 2. If that's the case, we'd like to keep the left-over one (day 547) and use it as the 2nd year observation.
The issue is now that when adding these to year 2's dataset by the code below, and thereafter running the same script, it chooses the year one-observation even if there is a 2nd year observation closer to 730 days. E.g. if it had a leftover observation of 544 days and one observation at 720 days, it chooses the 544-days-observation instead of the 720 days.
I previously wrote a question and received some great help, but I now want to make it a bit more complicated and the same script does not seem to work.
We have a dataset following patients after a surgical intervention. The patients are followed up annually, however, in reality this means annually ish. Therefore, we have decided that patients who was seen between the days 548 and 914 are counted as annual followup year two (counted as 365,25x2=730,5), by using this code:
Code:
. generate daysfromintervention= datediff(INTERVENTION_DATE, FOLLOWUP_DATE, "day"> ) . generate followuptime =. . replace followuptime=1 if (daysfromintervention>=274 & daysfromintervention<548) . replace followuptime=2 if (daysfromintervention>=548 & daysfromintervention<914) . replace followuptime=3 if (daysfromintervention>=914 & daysfromintervention<1279) . save "/Volumes/USB/BIGDATASET.dta" *then using the variable followuptime to divide into different datasets so that each year is in one dataset: . use "/Volumes/USB/BIGDATASET.dta" . keep if followuptime==1 . save "/Volumes/USB/1.dta"
Some patients have two observation within the time-span. To pick the one closest to the year-mark, I have used:
Code:
. use "/Volumes/USB/1.dta" . gen delta = abs(daysfromtx - followuptime*365.25) . by ID_CODE(delta), sort: keep if _n == 1 . save "/Volumes/USB/1_closest.dta" *but also creating one dataset with the other left-over observations . use "/Volumes/USB/1.dta" . gen delta = abs(daysfromtx - followuptime*365.25) . by ID_CODE(delta), sort: drop if _n == 1 . save "/Volumes/USB/1_not closest.dta"
Code:
. use "/Volumes/USB/2.dta" . append using "/Volumes/USB/1_not closest.dta" . gen delta = abs(daysfromtx - followuptime*365.25) . by ID_CODE(delta), sort: keep if _n == 1
