BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Romeo1
Calcite | Level 5
proc sql;
create table a as 
select rank,      
              pos_id,       
              c_estado,     
              d_estado,     
              c_cartao_sup, 
              c_ndc_sup,    
              c_orgao_resp 
from (select row_number() over (partition by pos_id order by c_estado asc, c_cartao_sup desc) as rank, 
                            pos_id,      
              c_estado,     
              d_estado,     
              c_cartao_sup, 
              c_ndc_sup,    
              c_orgao_resp 
            from  f_agreg_mens_pos)x
where rank=1;
run;

Editor's Note: The title was changed to more generally reflect the goal. 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Try this:

proc sort data=f_agreg_mens_pos;
by pos_id c_estado descending c_cartao_sup;
run;

data want;
rank = 1;
set f_agreg_mens_pos;
by pos_id;
if first.pos_id;
keep
  rank    
  pos_id
  c_estado
  d_estado
  c_cartao_sup
  c_ndc_sup
  c_orgao_resp
;
run;

If I read that code correctly, you want the top observation according to the sort order.

View solution in original post

3 REPLIES 3
Romeo1
Calcite | Level 5

Hi, I can not translate by converting to subqueries, it's confused. There is no way to do row_number() over (partition by ) in SAS Studio?

Kurt_Bremser
Super User

Try this:

proc sort data=f_agreg_mens_pos;
by pos_id c_estado descending c_cartao_sup;
run;

data want;
rank = 1;
set f_agreg_mens_pos;
by pos_id;
if first.pos_id;
keep
  rank    
  pos_id
  c_estado
  d_estado
  c_cartao_sup
  c_ndc_sup
  c_orgao_resp
;
run;

If I read that code correctly, you want the top observation according to the sort order.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 14808 views
  • 0 likes
  • 3 in conversation