The code and example result are below. How can I get the sum in descending order?
Thanks for your help.
data disc;
set sales (keep=sub_category sales order_date);
saleyear=year(order_date);
run;
proc means data=disc sum ;
class sub_category;
var sales;
where saleyear in (2012,2013,2014);
run;
Means Procedure
Sub_category N obs sum
a 20 20006.56
b 39 114309.658
c 200 12657.98
d 98 876000.765
e 256 9860
proc means data=disc nway ;
class sub_category;
var sales;
where saleyear in (2012,2013,2014);
output out=temp sum=sum n=nobs;
run;
proc sort data=temp;
by descending sum;
run;
HI @meetagupta You would need to write your results to an output dataset and sort by descedning. You could use ods output or output out to write your results to output dataset and then a proc sort
But SUM is not a variable in the dataset DISC. How will I sort it?
proc means data=disc nway ;
class sub_category;
var sales;
where saleyear in (2012,2013,2014);
output out=temp sum=sum n=nobs;
run;
proc sort data=temp;
by descending sum;
run;
Or you could switch to SQL to do all in one and easy
proc sql;
create table want as
select sub_category,sum(sales)as sum
from disc
where saleyear in (2012,2013,2014)
group by sub_category
order by sum desc;
quit;
Thanks. It worked but now its giving me result as below:
sub_category sum saleyear
a 64573 2012
a 64573 2012
a 64573 2014
a 64573 2013
b 4590 2012
b 4590 2012 and so on...
I want only 1 row for a and 1 row for b and so on. I can drop the saleyear as I dont need it anymore in the result table.
Yes That's the reason I didn't include the saleyear in the SELECT clause in the example above. Did you code differently?
Ya I did include saleyear. But I removed it now and I am getting correct answer. Thank you very much for your help
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.