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
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.