This is a tricky one, that I have been having issues getting the desired result. Below I have employee data where there are start dates, termination dates, 2 different review record types, and employee ids. In the data below, I need to create a unique Id to match each B review with only one A review. The matching criteria are the following: 1) The B review must fall on or between the A review dates (same employee as well). 2) When the B review matches more than one A review, then the B review must match to the A review with the latest termination date. So if it matches 2 or more A reviews, I need it to match the one with the latest termination date (see my example). This is where I'm running into problems. 3) The A reviews do not always have to match a B review. What is important is that the B reviews are correctly mapped to an A review. In my example, the unmatached A reviews have a null value o the unique Id flag. 4) A unique id flag to show the correct mapping of the B review back to the A review that fulfills criteria 1 and 2 above. data have; input employee_id rvw_type $ s_date :yymmdd10. t_Date :yymmdd10.; format s_date yymmddd10. t_date yymmdd10.; datalines; 1 A 2019-04-23 2019-04-25 1 A 2019-04-23 2019-04-27 1 A 2019-11-20 2019-11-24 1 A 2019-11-20 2019-11-26 1 B 2019-04-25 2019-04-25 1 B 2019-04-23 2019-04-23 1 B 2019-11-22 2019-11-22 2 A 2019-01-05 2019-01-06 2 A 2019-01-05 2019-01-08 2 B 2019-01-05 2019-01-05 2 B 2019-01-05 2019-01-06 ; run; data want; input _employee_id Rvw_type $ s_date :yymmdd10. t_Date :yymmdd10. Unique_match_flag; format s_date yymmddd10. t_date yymmdd10.; datalines; 1 A 2019-04-23 2019-04-25 . 1 A 2019-04-23 2019-04-27 1 1 A 2019-11-20 2019-11-24 . 1 A 2019-11-20 2019-11-26 2 1 B 2019-04-25 2019-04-25 1 1 B 2019-04-23 2019-04-23 1 1 B 2019-11-22 2019-11-22 2 2 A 2019-01-05 2019-01-06 . 2 A 2019-01-05 2019-01-08 3 2 B 2019-01-05 2019-01-05 3 2 B 2019-01-05 2019-01-06 3 ; run;
... View more