Help using Base SAS procedures

How do I get a count of unique Patient IDs grouped by two variables (DrugName & Year)?

Reply
Occasional Contributor
Posts: 11

How do I get a count of unique Patient IDs grouped by two variables (DrugName & Year)?

Hi,

 

I have a data set that contains Patient IDs, Drugs, and date of the prescription fill.  I am trying to identify the number of individual patients that were on a drug in a given year.  A patients typically have multiple records within a year, as each record representation a prescription fill date.  The data structure looks like this:

 

PatidDrugNameYear
100Drug 12013
200Drug 12013
200Drug 12013
200Drug 12013
200Drug 12013
300Drug 12013
100Drug 12014
300Drug 22013
100Drug 22015
400Drug 22015
400Drug 22015
400Drug 22015
300Drug 32014
300Drug 32015
100Drug 32015

 

 

In my head, the output should should look something like this:

Drugyear#Patients
Drug 120133
Drug 120141
Drug 220131
Drug 220151
Drug 320141
Drug 320152

 

 

I have tried proc summary, but that just gives me a frequency of the number of records, by drug & year.  I am thinking proc sql using count/distinct would work, but not sure how to group by drugname & year, as I am not very good at this...

 

Any assistance is greatly appreciated.


Thanks!
Chris

 

Super User
Posts: 18,586

Re: How do I get a count of unique Patient IDs grouped by two variables (DrugName & Year)?

You can do that but I'd consider a different method because you are likely to want multiple stats.

 

data have2;
set have;

by id drug;
First_Drug=0;
if first.drug then First_Drug=1;

run;

proc means data=have2 sum;
class year drug;
var first_drug;
output out=want sum(first_drug) = num_patients;
run;
Occasional Contributor
Posts: 11

Re: How do I get a count of unique Patient IDs grouped by two variables (DrugName & Year)?

Thanks.  This worked well, and matches up with another step in my analysis. 

 

-Chris

 

Super User
Posts: 5,255

Re: How do I get a count of unique Patient IDs grouped by two variables (DrugName & Year)?

PROC FREQ is pretty good at counting:

 

proc freq data=have;

tables DrugName*PatID*Year / noprint out=summarized;

run;

 

proc freq data=summarized;

tables DrugName*Year / list;

run;

 

The frequency column in the final PROC FREQ will be the patient count.  Also note, the second PROC FREQ could create an output data set if needed.

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