Hi PG, I examined the data on 20 patients and the code does its job!
Thanks a lot!
If I understood what you mean.
data have; input Patient_ID $ Medication_name $ Service_date : mmddyy10.; format Service_date mmddyy10.; cards; 0000120 QVar 08/07/2007 0000120 SPIRIVA 08/14/2007 0000120 SPIRIVA 11/14/2007 0000120 SPIRIVA 01/16/2008 0000120 QVar 12/05/2008 0000120 SPIRIVA2 08/13/2007 0000120 SPIRIVA2 11/13/2007 0000120 SPIRIVA2 01/15/2008 0000120 SPIRIVA3 08/15/2007 0000120 SPIRIVA3 11/15/2007 0000120 SPIRIVA3 01/17/2008 0000120 SPIRIVA4 01/11/2008 ; run; data temp; if _n_ eq 1 then do; if 0 then set have; length rank 8; declare hash ha(); ha.definekey('Medication_name'); ha.definedata('rank'); ha.definedone(); end; set have; by Patient_ID; if ha.find() ne 0 then do;r+1;rank=r;ha.add();end; if last.Patient_ID then do;ha.clear(); r=0;end; drop r; run; proc sort data=temp ; by Patient_ID rank Service_date;run; data x; set temp; by Patient_ID rank; if first.rank then n=0; n+1; lag_Service_date= lag(Service_date) ; month=intck('month',lag_Service_date,Service_date,'c'); if n=2 and month le 6; keep Patient_ID Medication_name lag_Service_date; format lag_Service_date mmddyy10.; run; data want; set x; by Patient_ID; if first.Patient_ID; run;
Xia Keshan
Message was edited by: xia keshan
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.