Hi I have a dataset that looks something like this: data test; input EP_ID $ SOC :mmddyy10. ROC :mmddyy10. ASSMT_DT :mmddyy10. ASSMT_RSN $; format SOC ROC ASSMT_DT date9.; datalines; 1 08/10/2016 . 08/10/2016 01 1 08/10/2016 . 08/18/2016 09 1 08/10/2016 . 10/08/2016 04 1 08/10/2016 . 11/25/2016 06 1 08/10/2016 . 12/06/2016 08 2 01/05/2017 . 01/05/2017 01 2 01/05/2017 . 01/15/2017 06 2 01/05/2017 01/19/2017 01/19/2017 03 2 01/05/2017 01/19/2017 01/30/2017 09 3 02/15/2017 . 02/15/2017 01 3 02/15/2017 . 02/25/2017 07 4 03/14/2017 . 03/14/2017 01 4 03/14/2017 . 03/18/2017 04 4 03/14/2017 . 03/26/2017 06 4 03/14/2017 04/02/2017 04/02/2017 03 4 03/14/2017 04/02/2017 04/12/2017 05 4 03/14/2017 04/02/2017 04/22/2017 06 4 03/14/2017 04/02/2017 04/28/2017 08 ; In my data set, I want to create a start and stop indicator using values of assmt_rsn. If assmt_rsn takes the values '06', '07', '08' or 09 then I want to stop at that line and count that date as the end of stay within that episode. Every stop of episode , should have a start denoted by assmt_rsn taking the value 01 or 03. In some cases when a 01 or 03 is missing before the stop, a 04 or 05 is considered as a start of stay. So, I finally want to achieve something like this: ep_id ep_stay_id SOC ROC ep_stay_strt_dt ep_stay_start_rsn ep_stay_end_dt ep_stay_end_rsn 1 1 08/10/2016 . 08/10/2016 01 08/18/2016 09 1 2 08/10/2016 . 10/08/2016 04 11/25/2016 06 1 3 08/10/2016 . . . 12/06/2016 08 2 1 01/05/2017 . 01/05/2017 01 01/15/2017 06 2 2 01/05/2017 01/19/2017 01/19/2017 03 01/30/2017 09 3 1 02/15/2017 . 02/15/2017 01 02/25/2017 07 4 1 03/14/2017 . 03/14/2017 01 03/26/2017 06 4 2 03/14/2017 04/02/2017 04/02/2017 03 04/22/2017 06 4 3 03/14/2017 . . . 04/28/2017 08 How can I achieve this?
... View more