I want to sort prescription records by individual drug and the first service date and assign numbers to each drug based on sorting result but have no idea how to do that. An example is like this:
Patient ID Drug name Service date
0001 Drug A 01/01/2007
0001 Drug B 01/10/2007
0001 Drug C 01/20/2007
0001 Drug A 02/01/2007
0001 Drug B 02/10/2007
0001 Drug C 02/20/2007
0001 Drug A 03/01/2007
0001 Drug B 03/10/2007
0001 Drug C 03/20/2007
The desired outcome is like this,
Patient ID Drug name Service date Drug number
0001 Drug A 01/01/2007 1
0001 Drug A 02/01/2007 1
0001 Drug A 03/01/2007 1
0001 Drug B 01/10/2007 2
0001 Drug B 02/10/2007 2
0001 Drug B 03/10/2007 2
0001 Drug C 01/20/2007 3
0001 Drug C 02/20/2007 3
0001 Drug C 03/20/2007 3
Thanks!!!
proc sort data=have out=temp;
by Patient_ID Drug_name Service_date;
run;
data want;
set temp;
by Patient_ID Drug_name;
if first.Drug_name then do;
Drug_number+1;
end;
run;
Here you go:
data have;
infile cards dsd;
informat service_date mmddyy10.;
format service_date mmddyy10.;
input Patient_ID $ Drug_name $ Service_date;
cards;
0001,Drug A,01/01/2007
0001,Drug B,01/10/2007
0001,Drug C,01/20/2007
0001,Drug A,02/01/2007
0001,Drug B,02/10/2007
0001,Drug C,02/20/2007
0001,Drug A,03/01/2007
0001,Drug B,03/10/2007
0001,Drug C,03/20/2007
;
proc sort data=have;by Patient_ID Drug_name;
data want;
set have;
by Patient_ID drug_name;
if first.Drug_name then drug_number +1;
run;
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 16. 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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: