BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
meetagupta
Fluorite | Level 6

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

 

 

 
 
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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;

View solution in original post

9 REPLIES 9
novinosrin
Tourmaline | Level 20

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

meetagupta
Fluorite | Level 6

But SUM is not a variable in the dataset DISC. How will I sort it? 

novinosrin
Tourmaline | Level 20

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;
novinosrin
Tourmaline | Level 20

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;
meetagupta
Fluorite | Level 6

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.

 

novinosrin
Tourmaline | Level 20

Yes That's the reason I didn't include the saleyear in the SELECT clause in the example above. Did you code differently?

meetagupta
Fluorite | Level 6

Ya I did include saleyear. But I removed it now and I am getting correct answer. Thank you very much for your help

Reeza
Super User
Please mark this question as solved then, by marking one of novinosrin's solution as the correct response.
BrahmanandaRao
Lapis Lazuli | Level 10
please provide sales dataset

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 774 views
  • 2 likes
  • 4 in conversation