SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Rakeon
Quartz | Level 8

Hi,

I need to convert this Oracle Code in SAS :

 

row_number() over (partition by Var1, Var2, Var3 
order by Var 4, Var5, Var6)

I'm trying with proc rank, but it requires just one variable in rank parameter.
I would like to know if is there any chance to implement it with proc rank.

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@Rakeon wrote:

Hi,

I need to convert this Oracle Code in SAS :

 

row_number() over (partition by Var1, Var2, Var3 
order by Var 4, Var5, Var6)

I believe below should return the same result.

proc sort data=have out=want;
  by Var1 Var2 Var3 Var4 Var5 Var6;
run;
  
data want;
  set want;
  by Var1 Var2 Var3;
  if first.var3 then row_num=1;
  else row_num+1;
run;

 NB: In your subject line you mention Proc Rank BUT in the Oracle SQL code you're using row_number()

If you need anything that can deal with ties (multiple rows with identical values for variables var1 to var6) then you would need to use Oracle functions RANK() or RANK_DENSE() ...and in doing so the SAS code would also need to look a bit differently.

View solution in original post

2 REPLIES 2
Reeza
Super User

No, but data step + proc sort would probably work. 

 

Not sure this is quite right, but the idea is there

data want;
set have;
by var1 var2 var3;
if first.var then count=0;
else count+1;
run;

proc sort data=want;
by var4 var5 var6;
run;
Patrick
Opal | Level 21

@Rakeon wrote:

Hi,

I need to convert this Oracle Code in SAS :

 

row_number() over (partition by Var1, Var2, Var3 
order by Var 4, Var5, Var6)

I believe below should return the same result.

proc sort data=have out=want;
  by Var1 Var2 Var3 Var4 Var5 Var6;
run;
  
data want;
  set want;
  by Var1 Var2 Var3;
  if first.var3 then row_num=1;
  else row_num+1;
run;

 NB: In your subject line you mention Proc Rank BUT in the Oracle SQL code you're using row_number()

If you need anything that can deal with ties (multiple rows with identical values for variables var1 to var6) then you would need to use Oracle functions RANK() or RANK_DENSE() ...and in doing so the SAS code would also need to look a bit differently.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 2238 views
  • 4 likes
  • 3 in conversation