Hi everyone,
I have a panel data (repeated visits of patients during 2016-2017). I have unique patient IDs, visit IDs, time variable as MDYhms for admission and discharge dates. I need two new variables to show me how long (in hours and in days, separately) it took for a person to be readmitted (time difference between a current admission date - color red in the data and a previous discharge date - green). I am aware that most of the patients had only one visits, I am interested in the readmitted patients. For instance, patient ID 404 had two readmissions while patient ID 418 had 4.

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input long(person_id visit_id) str19(admit_date discharge_date)
401  1 "01/12/2017 07:25:00" "01/12/2017 12:05:00"
402  2 "05/31/2016 15:14:00" "06/01/2016 16:44:00"
403  3 "04/09/2016 12:11:00" "04/09/2016 13:45:00"
404  4 "07/06/2016 12:33:00" "07/06/2016 14:01:00"
404  5 "07/06/2016 18:50:00" "07/07/2016 00:10:00"
404  6 "08/19/2017 13:42:00" "08/21/2017 16:27:00"
405  7 "02/25/2016 20:45:00" "02/26/2016 03:35:00"
405  8 "08/02/2017 16:13:00" "08/03/2017 05:10:00"
405  9 "10/23/2017 15:29:00" "10/24/2017 07:55:00"
406 10 "06/21/2016 18:05:00" "06/22/2016 17:34:00"
407 11 "02/16/2016 11:00:00" "02/16/2016 12:06:00"
407 12 "06/09/2016 11:29:00" "06/09/2016 13:55:00"
407 13 "06/20/2016 07:54:00" "06/20/2016 08:30:00"
408 14 "01/10/2017 12:43:00" "01/12/2017 14:23:00"
409 15 "02/22/2017 21:22:00" "02/24/2017 19:45:00"
410 16 "02/22/2016 06:36:00" "02/25/2016 13:10:00"
411 17 "03/13/2016 12:43:00" "03/13/2016 15:56:00"
411 18 "08/11/2016 05:50:00" "08/12/2016 12:11:00"
411 19 "10/03/2016 08:09:00" "10/03/2016 11:24:00"
412 20 "08/11/2016 18:49:00" "08/11/2016 19:16:00"
412 21 "11/14/2016 10:08:00" "11/14/2016 11:57:00"
413 22 "08/19/2016 05:48:00" "09/15/2016 18:15:00"
413 23 "09/15/2016 18:25:00" "10/07/2016 08:00:00"
413 24 "11/02/2016 17:05:00" "11/03/2016 01:21:00"
413 25 "11/28/2016 05:00:00" "12/09/2016 18:40:00"
413 26 "09/13/2017 09:16:00" "09/19/2017 09:25:00"
414 27 "02/12/2016 05:19:00" "02/12/2016 13:52:00"
415 28 "05/19/2016 03:30:00" "05/21/2016 12:05:00"
416 29 "06/16/2017 01:14:00" "06/16/2017 05:21:00"
417 30 "12/08/2016 16:08:00" "12/08/2016 19:33:00"
418 31 "06/14/2016 22:24:00" "06/15/2016 02:49:00"
418 32 "06/18/2017 11:18:00" "06/20/2017 16:00:00"
418 33 "06/23/2017 20:42:00" "06/23/2017 23:16:00"
418 34 "08/03/2017 06:11:00" "08/03/2017 15:00:00"
418 35 "08/04/2017 09:29:00" "08/04/2017 12:35:00"
end
I was wondering if it would be possible to create new variables like this:

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input long(person_id visit_id) str19(admit_date discharge_date) float(readmission_hour readmission_day)
404  4 "07/06/2016 12:33:00" "07/06/2016 14:01:00"     4   0
404  5 "07/06/2016 18:50:00" "07/07/2016 00:10:00"  9800 409
404  6 "08/19/2017 13:42:00" "08/21/2017 16:27:00"     .   .
405  7 "02/25/2016 20:45:00" "02/26/2016 03:35:00" 12570 524
405  8 "08/02/2017 16:13:00" "08/03/2017 05:10:00"  1968  82
405  9 "10/23/2017 15:29:00" "10/24/2017 07:55:00"     .   .
407 11 "02/16/2016 11:00:00" "02/16/2016 12:06:00"  2760 115
407 12 "06/09/2016 11:29:00" "06/09/2016 13:55:00"     .   .
407 13 "06/20/2016 07:54:00" "06/20/2016 08:30:00"     .   .
411 17 "03/13/2016 12:43:00" "03/13/2016 15:56:00"     .   .
411 18 "08/11/2016 05:50:00" "08/12/2016 12:11:00"     .   .
411 19 "10/03/2016 08:09:00" "10/03/2016 11:24:00"     .   .
412 20 "08/11/2016 18:49:00" "08/11/2016 19:16:00"     .   .
412 21 "11/14/2016 10:08:00" "11/14/2016 11:57:00"     .   .
413 22 "08/19/2016 05:48:00" "09/15/2016 18:15:00"     .   .
413 23 "09/15/2016 18:25:00" "10/07/2016 08:00:00"     .   .
413 24 "11/02/2016 17:05:00" "11/03/2016 01:21:00"     .   .
413 25 "11/28/2016 05:00:00" "12/09/2016 18:40:00"     .   .
413 26 "09/13/2017 09:16:00" "09/19/2017 09:25:00"     .   .
418 31 "06/14/2016 22:24:00" "06/15/2016 02:49:00"     .   .
418 32 "06/18/2017 11:18:00" "06/20/2017 16:00:00"     .   .
418 33 "06/23/2017 20:42:00" "06/23/2017 23:16:00"     .   .
418 34 "08/03/2017 06:11:00" "08/03/2017 15:00:00"     .   .
418 35 "08/04/2017 09:29:00" "08/04/2017 12:35:00"     .   .
end
The goal is to study the potential variables associated with readmission so the focus is on the conditions prior to the readmissions, such as what medications the patients took, how was his/her condition when he/she was previously discharged, discharge diagnosis and so on. That is the reason readmission variables are matched with the previous visits instead of the visits the patients were readmitted.
Any help is highly appreciated.
Thank you