BookmarkSubscribeRSS Feed
buszhangsy
Calcite | Level 5

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!!!

2 REPLIES 2
gergely_batho
SAS Employee

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;

Steelers_In_DC
Barite | Level 11

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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 538 views
  • 0 likes
  • 3 in conversation