Dear users,

I’m currently stuck in a script and would very much appreciate some help.

I am using a dataset that includes around 20,000 patients admitted to hospital. Many of whom have been admitted several times, some transferred between hospitals and thereby have the same admission date but differ in time and hospital admitted to. For this study each patient has a reference admission on which a certain event happened.
I wish to exclude (remove) all admissions but the one directly succeeding the reference event in time.
I.e. all admissions that took place before as well as all the ones after the admission directly following the reference, keeping only two adjacent admissions.
Part of the dataset includes these variables which I hope would be enough to work this out.


Personal ID (unique to each patient, could therefore be same for different admissions)
Unique admission number
Time/date of admission
Number of admissions
Reference admission

Any help would be much appreciated

Regards
Fredric