🔒 This topic is solved and locked.
Need further help from the community? Please
sign in and ask a new question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 12-08-2019 09:59 AM
(3237 views)
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
1 ACCEPTED SOLUTION
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Jag
9 REPLIES 9
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
PROC SORT DATA = sorted ;
BY DESCENDING sum;
RUN;
DATA want;
SET sorted ;
by DESCENDING sum;
IF _n_<=2 THEN OUTPUT;
RUN;
Thanks,
Jag
Jag
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks for your reply but sorry. This does not work. In log: " ERROR: Variable SUM not found."
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Jag
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Here we are:
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Jag
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Jag
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Jag