Hi, First sort datasets by Member_ID and Service_Date. The read them with a set/by statement (interleaving). When reading from the history dataset, store the needed variables in a hash object. Key will be the Prod_Group. If key already exist, just replace, since the dataset is sorted by time, and you need only the latest. Also store the needed variables globally (not per group). For simplicity, my code stores it in the same hash object, with key=0. When reading from qualified dataset, retrieve the needed values from the hash object. Of course, first try with key=Prod_Group. If no success, or entry is too old, try with key=0. All this should be done for each Memeber_ID. I am simply clearing the hash object at the beginning of a new group(Member_ID). Please test it with more meaningful data. data qualified; input Member_ID Transaction_ID Service_Date:yymmdd10. Prod_Group; format Service_Date yymmdd10. ; cards; 1 123 2014-01-05 1 1 124 2014-01-08 3 2 126 2014-01-07 2 3 128 2014-01-26 5 ; run; data history; input Member_ID Transaction_ID Service_Date:yymmdd10. Prod_Group Product_ID; format Service_Date yymmdd10. ; cards; 1 123 2013-02-05 1 3 1 124 2013-05-08 3 4 2 126 2013-07-07 2 6 3 128 2014-01-26 5 6 1 123 2013-01-05 1 7 1 124 2013-01-08 3 8 2 126 2014-01-07 2 8 3 128 2014-01-26 5 8 ; run; /*Sort by ID and date*/ proc sort data=qualified; by Member_ID Service_Date; run; proc sort data=history; by Member_ID Service_Date; run; data want; if _n_=1 then do; dcl hash lastGroup(); lastGroup.defineKey('H_Prod_Group'); lastGroup.defineData('H_Transaction_ID'); lastGroup.defineData('H_Service_Date'); lastGroup.defineData('H_Prod_Group'); lastGroup.defineData('H_Product_ID'); lastGroup.defineDone(); /*The following line is just a trick to define H_: variables in the PDV*/ if 0 then set history(rename=(Transaction_ID=H_Transaction_ID Service_Date=H_Service_Date Prod_Group=H_Prod_Group Product_ID=H_Product_ID)); end; set qualified(in=inQ) history(in=inH); by Member_ID Service_Date; if first.Member_ID then do; lastGroup.clear(); end; if inH then do;/*Storing data about the last transaction per group, and also globally (key:0)*/ rc=lastGroup.add (key:Prod_Group,data:Transaction_ID,data:Service_Date,data:Prod_Group,data:Product_ID); rc=lastGroup.replace(key:Prod_Group,data:Transaction_ID,data:Service_Date,data:Prod_Group,data:Product_ID); rc=lastGroup.add (key:0 ,data:Transaction_ID,data:Service_Date,data:Prod_Group,data:Product_ID); rc=lastGroup.replace(key:0 ,data:Transaction_ID,data:Service_Date,data:Prod_Group,data:Product_ID); end; if inQ then do; rc=lastGroup.find(key:Prod_Group);/*fetch group specific*/ putlog rc=; if rc~=0 or H_Service_Date<=intnx('day',Service_Date,-365,'s') then do;/*not found or old entry*/ call missing(of H_:); rc=lastGroup.find(key:0);/*fetch global*/ if rc~=0 or H_Service_Date<=intnx('day',Service_Date,-365,'s') then do;/*not found or old entry*/ call missing(of H_:); end; end; output; end; run;
... View more