Sorting question

Reply
Occasional Contributor
Posts: 10

Sorting question

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

SAS Employee
Posts: 340

Re: Sorting question

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;

Valued Guide
Posts: 854

Re: Sorting question

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;

Ask a Question
Discussion stats
  • 2 replies
  • 156 views
  • 0 likes
  • 3 in conversation