BookmarkSubscribeRSS Feed
MarcTC
Obsidian | Level 7
My SAS code:

proc sql;
create table a1 as
select region,
sum(sales) as tot_sales
from sashelp.shoes
group by region
order by tot_sales desc
; quit;

data a2;
set a1;
rank = _n_;
run;

I want to know if it is possible to eliminate the second data step and let the first proc sql create rank variable.
6 REPLIES 6
sss
Fluorite | Level 6 sss
Fluorite | Level 6
Hi
Gud one i dont know how to do in Proc sql;

Base sas Code goes here...


DATA SHOES(KEEP =REGION TOT_SALES);

SET SHOES;
BY REGION;
IF FIRST.REGION THEN TOT_SALES=0;
TOT_SALES+SALES;
IF LAST.REGION THEN OUTPUT;
RANK=_N_;
RUN;
SPR
Quartz | Level 8 SPR
Quartz | Level 8
Hello SSS,

It is more like this:
[pre]
data Shoes (KEEP =REGION TOT_SALES rank);
set sashelp.shoes;
if FIRST.region then do; TOT_SALES=0; Rank+1; end;
TOT_SALES+SALES;
if LAST.region then output;
by region;
run;
[/pre]
Sincerely,
SPR Message was edited by: SPR
sss
Fluorite | Level 6 sss
Fluorite | Level 6
Hi SPR,

it would be more likely

PROC SORT DATA=SASHELP.SHOES OUT=SHOES;
by decending REGION;
RUN;

data Shoes (KEEP =REGION TOT_SALES rank);
set shoes;
if FIRST.region then do; TOT_SALES=0; Rank+1; end;
TOT_SALES+SALES;
if LAST.region then output;
by descending region;
run;
data_null__
Jade | Level 19
I don't know either but PROC SUMMARY will do it.

[pre]
proc sql;
create table a1 as
select region,
sum(sales) as tot_sales
from sashelp.shoes
group by region
order by tot_sales desc
; quit;
data a2;
set a1;
rank = _n_;
run;

proc print;
run;



proc summary data=sashelp.shoes nway;
class region / order=freq descending;
freq sales;
output out=ranks / levels;
run;
proc print;
run;
proc compare base=a2 compare=ranks;
var region tot_sales rank;
with region _freq_ _level_;
run;
[/pre]
art297
Opal | Level 21
Marc,

If you can use an undocumented function (that comes with the risk that the feature may not exist in future releases), you can definitely do it in a 2nd proc sql step and can probably combine the two. E.g.:
[pre]

proc sql noprint;
create table a1 as
select distinct region,
sum(sales) as tot_sales
from sashelp.shoes
group by region
order by tot_sales desc
;
create table want as
select *,
monotonic() as rank
from a1
order by tot_sales desc
;
quit;
[/pre]
HTH,
Art
LinusH
Tourmaline | Level 20
None of the above solution is a single SQL.
Question is, why is that important? Is this you real summarized data huge, and you want to avoid more than one pass of data? In that case, one could use a view approach for the SQL group by step.
And for ranking, why not use a procedure which was created to do - ranking...?
In my eyes, it is the simplest (and supported) code:


proc rank data=work.a1 out=a2 descending;
var tot_sales;
ranks rank_sales;
run;

/Linus Oh, missed the ranks statement...


Message was edited by: Linus H
Data never sleeps

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 6 replies
  • 13196 views
  • 1 like
  • 6 in conversation