Hi all,

I'm working on a fairly large hospital readmission dataset with 350 vars and 1.5M observations.
I'm not being able to code, bysort pat_id fict_data, the date difference (fict_data var) between each observation and the previous observation where the index var assumes value 1.

If a patient had only one admission, missing value is expected. If a patient had 2 admissions and both assume index==1 the number of days between the second and the first admission is expected to be returned. If a patient had, for instance 4 admissions and only the second assume index==1, a missing is expected for the first and second, the days between 3rd and 2nd is expected for the 3rd observation and the days between 4th and 2nd for the 4th observation.

Bellow I provide a sample of relevant variables and observations for illustration purposes.

Any help on this would be great,

Thanks
Bruno

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input long(pat_id adm_id) float(_temp_order index fict_data)
256039741 951368220  1 1   1
256040137 951633005  1 0   1
256040299 951431200  1 1   1
256040299 951430409  2 1  42
256040362 951414839  1 1   1
256040419 950863825  1 0   1
256040419 950863813  2 0   4
256040419 950863827  3 0  10
256040419 950863828  4 0  16
256040419 950863829  5 0  22
256040419 950863797  6 0  26
256040419 950863824  7 0  28
256040419 950863796  8 0  42
256040419 950863821  9 0  48
256040419 950863823 10 0  54
256112056 951365650  1 0   1
256112056 951365651  2 1  90
256112142 951410486  1 1   1
256112148 951367566  1 1   1
256112148 951367564  2 1  83
256112148 951367565  3 1 170
256112148 951366152  4 1 172
256136070 951367186  1 0   1
256136070 951367188  2 1  75
256136114 951437260  1 1   1
256136114 951436321  2 1  22
256136114 951442601  3 1  25
end