简体   繁体   中英

Calculate AVERAGE in SAS

I have been using proc means to get an average copay out of my dataset for a particular month, but I also need to calculate average copay by pharmacy .

It doesn't seem like I can do this with proc means , also I prefer it as a dataset I can query off of, not a proc print of 200 pharmacies. I tried the below code but I'm not getting the same numbers as I would with proc means :

This is how I sum data by Pharmacy, is there a way to make this an average by pharmacy instead of summing it?:

    %pjsum(add_ram,  RAM      pme_id pharmacy month, patient_benefit APPR, sum);

Assuming you have a patient_benefit numeric column and pharmacy and month columns, you can use proc means by specifying groups in class and numeric column in var clauses. Be sure to specify the mean statistic. Below outputs a dataset named AggData with Avg_Patient_Benefit as the aggregated column.

proc means data=myData nway ;
  class pharmacy month;
  var patient_benefit;
  output out=AggData mean=Avg_Patient_Benefit;  
run;

Alternatively you can run an aggregate SQL query with proc sql :

proc sql;
   create table AggData as
   select pharmacy, month, mean(patient_benefit)
   from myData
   group by pharmacy, month;
quit;

You can even run conditional aggregation for various month columns:

proc sql;
   create table AggData as
   select pharmacy, 
          mean(case when month = "July" then patient_benefit else . end) as avg_july_month,
          mean(case when month = "August" then patient_benefit else . end) as avg_aug_month,
          mean(case when month = "September" then patient_benefit else . end) as avg_sep_month
          ...
   from myData
   group by pharmacy;
quit;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM