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

I have big database obtained from store data. I used the folowing code to identify which month has the highest profit

proc sort data=Superstore_Orders1; by order_month  descending profit ; run;
proc means data=Superstore_Orders1 sum ;
output out=sorted (drop= _TYPE_ _FREQ_); 
var   profit ; 
class  order_month ;  run;

I got the following  in the results but I want to get the 2 months with highest profit in the output (I think through sorting sum in descending order). Any advice will be graetly appreciated

image.png

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

Sure please try the below code

 

proc sort data=Superstore_Orders1; 
by order_month  descending profit ;
 run;

proc means data=Superstore_Orders1 sum ;
output out=sorted (drop= _TYPE_ _FREQ_) sum=sum; 
var   profit ; 
class  order_month ;  
run;

proc sort data=sorted ; by descending sum; run;

data want;
set sorted ;
by descending sum;
if _n_<=2 then output;
run;

Thanks,
Jag

View solution in original post

9 REPLIES 9
Jagadishkatam
Amethyst | Level 16

 

 

 

PROC SORT DATA = sorted ;
    BY DESCENDING sum;
RUN;

DATA want;
    SET sorted ;
    by DESCENDING sum;
    IF _n_<=2 THEN OUTPUT;
RUN;
Thanks,
Jag
mrahouma
Obsidian | Level 7
Thanks for your reply but sorry. This does not work. In log: " ERROR: Variable SUM not found."
Jagadishkatam
Amethyst | Level 16
ok could you please post the screenshot of the sorted dataset generated from proc means, I want to know the name of the sum variable
Thanks,
Jag
mrahouma
Obsidian | Level 7

Here we are:

image.png

Jagadishkatam
Amethyst | Level 16

Please try below code , here I have subset only the SUM row and considered the first 2 highest sum

 

 

PROC SORT DATA = sorted ;
    BY DESCENDING profit;
where _stat_='SUM';
RUN;

DATA want;
    SET sorted ;
    by DESCENDING profit;
    IF _n_<=2 THEN OUTPUT;
RUN;
Thanks,
Jag
mrahouma
Obsidian | Level 7
in _STAT_, there is no "sum" and it gives me empty dataset upon running your code. Is there a way to fix my original code to get sum in the _STAT_ column so I can use your code
Jagadishkatam
Amethyst | Level 16

Sure please try the below code

 

proc sort data=Superstore_Orders1; 
by order_month  descending profit ;
 run;

proc means data=Superstore_Orders1 sum ;
output out=sorted (drop= _TYPE_ _FREQ_) sum=sum; 
var   profit ; 
class  order_month ;  
run;

proc sort data=sorted ; by descending sum; run;

data want;
set sorted ;
by descending sum;
if _n_<=2 then output;
run;

Thanks,
Jag
mrahouma
Obsidian | Level 7

Thanks it works. However it gives me 2 rows with the 1st row represents sum of all profit irrespective of month.

I accept the prior post as a solution and would appreciate if you could edit it further.

Jagadishkatam
Amethyst | Level 16

Sure, please try below code, I included where order_month ne .;

 

proc sort data=Superstore_Orders1; 
by order_month  descending profit ;
 run;

proc means data=Superstore_Orders1 sum ;
output out=sorted (drop= _TYPE_ _FREQ_) sum=sum; 
var   profit ; 
class  order_month ;  
run;

proc sort data=sorted ; 
by  descending sum;
where order_month ne .;
 run;


data want;
set sorted ;
by  descending sum;
if _n_<=2 then output;
run;
Thanks,
Jag

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 3739 views
  • 1 like
  • 2 in conversation