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
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
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
PROC RANK will do this as well.
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
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.