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

I would like to use the Rank function in Proc sql

Getting error, when I use this code

RANK() OVER(ORDER BY t.ID DESC) as rank,


Output like this,

ID New col
10 1
10 2
10 3
23 1
25 1
45 1
50 1
50 2
65 1

 

1 ACCEPTED SOLUTION

Accepted Solutions
kiranv_
Rhodochrosite | Level 12

Rank over and others are known as Order analytical functions and  are not available in Proc SQL. But you can use proc sort and use first.variable concept to give rank

data have;
input id;
datalines;
10 
10 
10 
23 
25 
45 
50 
50 
65 
;

proc sort data =  have;
by id;
run;

data want;
set have;
by id;
if first.id then rank =1;
else rank+1;
run;

and then use first.ID to get the desired result in sas datastep

 

View solution in original post

4 REPLIES 4
kiranv_
Rhodochrosite | Level 12

Rank over and others are known as Order analytical functions and  are not available in Proc SQL. But you can use proc sort and use first.variable concept to give rank

data have;
input id;
datalines;
10 
10 
10 
23 
25 
45 
50 
50 
65 
;

proc sort data =  have;
by id;
run;

data want;
set have;
by id;
if first.id then rank =1;
else rank+1;
run;

and then use first.ID to get the desired result in sas datastep

 

Reeza
Super User

PROC RANK will do this as well.

Kalai2008
Pyrite | Level 9
Thank you! I already did this in SAS, but trying to do in Proc SQL.
art297
Opal | Level 21

The following will work using proc sql:

proc sql;
  create table want as
  select *,
   (select count(distinct b.weight)
     from sashelp.class b
       where b.weight <= a.weight
         and a.sex eq b.sex) as rank
           from sashelp.class a
  ;
quit;

Art, CEO, AnalystFinder.com

 

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!

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
  • 4 replies
  • 29519 views
  • 4 likes
  • 4 in conversation