hi all,

I ve been trying to perform this task for a week now with no luck. I have a data set (each row corresponds to a hospitalization) with various patient id's and their admission and discharge dates . Also included is each hospitalizations discharge status (whether someone was transferred to another hospital or discharged home and is coded with the transfer variable where 1= transfer and 0 = discharged home)

in our study, when a patient is admitted to hospital and then subsequently transferred to another hospital (transfer==1), the transfer is to be considered as a continuation of the previous hospitalization. We want to add up the length of stays (LOS) and cost for a hospitalization and its subsequent transfers, if any. I do not know how to do this.

For example, in the provided dataset example, patient id=1 is admitted on day 2998 and on day 3014 is transferred to another hospital (transfer==1) 16 days later. This patient is then transferred to another hospital 30days later (transfer==1 and los==30) on day 3044. Again, this same patient is transferred to yet another hospital on day 3084. Finally, on day 3134, the patient is discharged home (transfer==0). All of this will constitute one hospitalization with a total LOS=16+30+40+50=136 days. Ideally, I would like to group each such hospitalization (with their transfers) and have a variable, total_LOS, associated with each of these hospitalizations. Some hospitalizations are single entities in that they are stand alone with no transfers, some have 1 or more transfers like in the example provided. We would also like to add up the cost for each of these hospitalization groupings. In the above example, the cost = 4.5877+4.1481+10.9536+6.1439=25.8333.


ideal data set would look like this where the first 4 rows constitute one hospitalization with transfers, and the 5th row is a single entity hospitalization:

patid los transfer admitday_dad dischday_dad cost hospitalization total_LOS total_cost
1 16 1 2998 3014 4.5877 1 136 25.83
1 30 1 3014 3044 4.1481 1 136 25.83
1 40 1 3044 3084 10.9536 1 136 25.83
1 50 0 3084 3134 6.1439 1 136 25.83
1 21 0 3436 3457 1.5273 2 21 1.52

any feedback would be greatly appreciated. Let me know if any clarifications are needed.


here is the dataex sample:

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input int patid float(los transfer admitday_dad dischday_dad) double cost
 1   9 0 2221 2230             1.5544
 1  13 0 2262 2275              2.483
 1  16 1 2998 3014             4.5877
 1  30 1 3014 3044             4.1481
 1  40 1 3044 3084            10.9536
 1  50 0 3084 3134             6.1439
 1  21 0 3436 3457             1.5273
 1   5 0 3463 3468              .7539
 1   6 0 3498 3504              1.078
 1   6 0 3539 3545             1.1104
 1   9 0 4377 4386             2.5251
 1   1 0 4631 4632             2.9441
 1   6 0 4690 4696              1.627
 1   9 0 4803 4812               .927
 1  17 0 4873 4890             2.6519
 2  26 0 3821 3847             6.9626
 2   1 0 4035 4036 .09620000000000001
 2   1 0 4052 4053              .8907
 2   5 0 5340 5345  .8019000000000001
 2  14 0 5395 5409             2.8347
 2   3 0 5435 5438  .7303000000000001
 2   5 0 5664 5669             1.6708
 2   4 0 7014 7018               .736
 3   3 0 3344 3347 .38370000000000004
 3   2 0 3688 3690              .7345
 3   6 0 4755 4761             2.9441
 3   7 0 4849 4856             2.2435
 3  60 0 5971 6031             7.1798
 3  80 0 6237 6317            10.7454
 4  13 1 1375 1388 2.0698000000000003
 4   2 1 1388 1390             1.2862
 4  14 0 1390 1404             2.4866
 4  12 0 2133 2145 2.8000000000000003
 4   8 0 2858 2866             2.0234
 4   7 0 3149 3156              .6584
 4  82 0 3472 3554             9.7291
 4  11 0 3566 3577             2.4878
 4  15 0 3650 3665 1.7530000000000001
 4  13 0 3758 3771 2.0281000000000002
 4  24 1 3832 3856             2.9849
 4  61 0 3856 3917  5.328600000000001
 4  81 0 4116 4197             7.8562
 4  86 0 4432 4518             9.7103
 4   1 0 4738 4739              .1625
 5   2 0 1802 1804  .5941000000000001
 5   8 0 2939 2947              .6584
 5  10 0 3921 3931             1.3788
 5  17 0 4040 4057 2.1087000000000002
 5  17 0 4139 4156             2.1758
 5  23 0 4426 4449             3.0194
 5  27 0 5159 5186 1.8638000000000001
 5  13 0 5275 5288             1.6845
 6   9 1 4238 4247 1.7246000000000001
 6  11 0 4247 4258             2.2109
 6  10 0 4301 4311 1.0068000000000001
 6  15 0 4475 4490 3.5399000000000003
 6   6 0 4546 4552 1.0068000000000001
 6   1 0 4820 4821  .8965000000000001
 6  12 0 5180 5192 1.4120000000000001
 6   0 0 5349 5349              .5161
 6   6 0 5788 5794  .8965000000000001
 6   9 0 5799 5808             2.8571
 6  11 0 5880 5891             2.2083
 6   7 0 6108 6115              .9331
 6  10 0 6159 6169             2.7448
 6   5 0 6238 6243 1.9444000000000001
 6   8 0 6348 6356 1.3795000000000002
 6   6 0 6472 6478 1.3795000000000002
 6   2 0 6651 6653               .382
 7   2 0  964  966 .47490000000000004
 7   4 0 4020 4024             1.0266
 7  25 1 4829 4854  9.074300000000001
 7  36 0 4854 4890             4.6931
 7   2 0 5426 5428 .33890000000000003
 7  21 1 5876 5897             4.0625
 7  51 0 5897 5948  9.882900000000001
 7   7 0 6015 6022             1.4732
 8   1 0 3740 3741 .32480000000000003
 8  11 0 3958 3969             2.3516
 8   1 0 4213 4214 1.0411000000000001
 8 269 0 4339 4608            38.7944
 8 102 0 5017 5119            12.5373
 8  17 0 5303 5320             3.4799
 9  12 0 3681 3693               3.52
 9  35 0 3695 3730             4.2714
 9   7 0 3894 3901             1.3124
 9   8 0 3917 3925              .8114
 9  28 0 3928 3956             4.3414
10   7 0 1831 1838             1.1104
10   6 0 4313 4319             1.3465
10  10 0 4488 4498             1.3826
10  12 0 4509 4521              .6039
10   8 0 5037 5045               .736
10  16 0 5098 5114 4.3508000000000004
11   4 0 4559 4563              .7637
11   0 0 5036 5036              .8907
11   1 0 5291 5292              .7637
11   9 0 5363 5372              2.899
12  14 0 4918 4932 6.8527000000000005
12   0 0 4948 4948              .4791
end