I have a dataset where each row is identified by a hospitalization id. Each row contains information on the hospitalization id, hospital id where it took place as well as date of admission and date of discharge of the given hospitalization.
I would like to know, for each hospitalization, the id of all other hospitalizations concluded in the 30 days before the beginning of the given hospitalization in the given hospital where it took place.
Below is a simple example of 6 hospitalizations taking place in 2 hospitals. I also created the date range encompassing the 30-day interval prior to the beginning of each given hospitalization.
I am trying to write a code that adds a column with all the hospitalizations that took place in the 30-day interval range before the given hospitalization started (and in sample hospital). I managed to write a code that tabulates the information I am looking for.
The output of this code is copied below
I would like to add this information into the dataset into a new column. The command -rangestat- informs how many hospitalizations fall into the given row's time interval. If more than 1 hospitalization fall into the given row's interval, the entire row should be repeated so as to inform all of them. I managed to do this below:
Now I would like to add the information I managed to tabulate into a new column in this dataset. It would do somehow a similar job as -rangestat- but instead of computing a given statistic using the rows that fall in the given interval it would inform which these rows are.
I would like to know, for each hospitalization, the id of all other hospitalizations concluded in the 30 days before the beginning of the given hospitalization in the given hospital where it took place.
Below is a simple example of 6 hospitalizations taking place in 2 hospitals. I also created the date range encompassing the 30-day interval prior to the beginning of each given hospitalization.
Code:
clear set obs 6 gen hospitalization_id = _n gen hospital_id = "A" if _n<4 replace hospital_id = "B" if _n>3 gen date_start = mdy(1,1,2000) if _n==1 replace date_start = mdy(1,12,2000) if _n==2 replace date_start = mdy(1,20,2000) if _n==3 replace date_start = mdy(2,10,2000) if _n==4 replace date_start = mdy(2,12,2000) if _n==5 replace date_start = mdy(2,12,2000) if _n==6 gen date_end = mdy(1,3,2000) if _n==1 replace date_end = mdy(1,18,2000) if _n==2 replace date_end = mdy(1,22,2000) if _n==3 replace date_end = mdy(2,11,2000) if _n==4 replace date_end = mdy(2,14,2000) if _n==5 replace date_end = mdy(2,17,2000) if _n==6 format date* %td gen date_range1 = date_start - 31 gen date_range2 = date_start - 1 format date* %td
Code:
levelsof hospitalization_id, local(ids)
foreach i of local ids {
display "hospitalization `i': list of hospitalizations in 30-day period before given hospitalization started, in the same hospital"
tab hospitalization_id if inrange(date_end,date_range1[`i'],date_range2[`i']) & hospital_id == hospital_id[`i']
}
Code:
hospitalization 1: list of hospitalizations in 30-day period before given hospitalization started, in the sa
> me hospital
no observations
hospitalization 2: list of hospitalizations in 30-day period before given hospitalization started, in the sa
> me hospital
hospitaliza |
tion_id | Freq. Percent Cum.
------------+-----------------------------------
1 | 1 100.00 100.00
------------+-----------------------------------
Total | 1 100.00
hospitalization 3: list of hospitalizations in 30-day period before given hospitalization started, in the sa
> me hospital
hospitaliza |
tion_id | Freq. Percent Cum.
------------+-----------------------------------
1 | 1 50.00 50.00
2 | 1 50.00 100.00
------------+-----------------------------------
Total | 2 100.00
hospitalization 4: list of hospitalizations in 30-day period before given hospitalization started, in the sa
> me hospital
no observations
hospitalization 5: list of hospitalizations in 30-day period before given hospitalization started, in the sa
> me hospital
hospitaliza |
tion_id | Freq. Percent Cum.
------------+-----------------------------------
4 | 1 100.00 100.00
------------+-----------------------------------
Total | 1 100.00
hospitalization 6: list of hospitalizations in 30-day period before given hospitalization started, in the sa
> me hospital
hospitaliza |
tion_id | Freq. Percent Cum.
------------+-----------------------------------
4 | 1 100.00 100.00
------------+-----------------------------------
Total | 1 100.00
.
Code:
. rangestat (count) nhosp=hospitalization_id, interval(date_end date_range1 date_range2) by(hospital_id)
. replace nhosp=0 if nhosp==.
(2 real changes made)
. expand nhosp
(2 zero counts ignored; observations not deleted)
(1 observation created)
. sort hospitalization_id
. list, sepby(hospital_id)
+-----------------------------------------------------------------------------+
| hos~n_id hos~l_id date_st~t date_end date_ra~1 date_ra~2 nhosp |
|-----------------------------------------------------------------------------|
1. | 1 A 01jan2000 03jan2000 01dec1999 31dec1999 0 |
2. | 2 A 12jan2000 18jan2000 12dec1999 11jan2000 1 |
3. | 3 A 20jan2000 22jan2000 20dec1999 19jan2000 2 |
4. | 3 A 20jan2000 22jan2000 20dec1999 19jan2000 2 |
|-----------------------------------------------------------------------------|
5. | 4 B 10feb2000 11feb2000 10jan2000 09feb2000 0 |
6. | 5 B 12feb2000 14feb2000 12jan2000 11feb2000 1 |
7. | 6 B 12feb2000 17feb2000 12jan2000 11feb2000 1 |
+-----------------------------------------------------------------------------+

Comment