My question is to identify the buyer with the smallest and largest total number of item purchase. First I need to sum up the total unit purchase for each same buyer name as the name is repeated. I decided to use proc means but unfortunately, I can't get the output. So what should I do so that I can sum up and get the highest and lowest no of purchase? REMARK: I'm using SAS 9.4
I decided to get the result like this. But the sample syntax in the following picture does not work.
Hi @SOOKHUI99
You can simply modify the PROC SQL as follows:
proc sql;
select "Smallest", *
from want
having sum_purchase= min(sum_purchase)
union
select "Largest", *
from want
having sum_purchase= max(sum_purchase);
quit;
Best,
Hi @SOOKHUI99
Please supply data in a usable form (DATALINES in a DATA step).
Could you please try the following code and see whether it does meet your needs?
The MEANS Procedure output a dataset names 'Want' from the input dataset 'Have' : it contains one observation per buyer with the total price (= sum price*units)
The PROC SQL output a report which select the buyer with the minimal amount and the buyer with the maximal amount.
best,
proc means data=have noprint nway;
class Buyer_Name;
var price;
weight unit;
output out=want (drop=_:) sum=sum_purchase;
run;
proc sql;
select *
from want
having sum_purchase= min(sum_purchase) or sum_purchase=max(sum_purchase);
quit;
Hi! thanks a lot!! Finally I get the answer. But I got modified a bit. One more thing, want can i do if i want to add the word 'largest' and 'smallest' beside the buyer name or sum purchase. thanks again!!!
data have;
set foodsalesreport6;
run ;
proc means data=have noprint nway;
class Buyer_Name;
var unit;
output out=want (drop=_:) sum=sum_purchase;
run;
proc sql;
select *
from want
having sum_purchase= min(sum_purchase) or sum_purchase=max(sum_purchase);
quit;
Hi @SOOKHUI99
You can simply modify the PROC SQL as follows:
proc sql;
select "Smallest", *
from want
having sum_purchase= min(sum_purchase)
union
select "Largest", *
from want
having sum_purchase= max(sum_purchase);
quit;
Best,
Hi @SOOKHUI99
You're very welcome ! 😊
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.