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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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