BookmarkSubscribeRSS Feed
rebelde52
Fluorite | Level 6

Hello, 

 

I have multiple observations with the same names with ranks and dates. I basically want to exclude names where if their date is less than their other date with their rank. For example Bob has multiple dates with his name, however, for one date (1/2/23) and he is ranked 1 with a status of P1. There is another row/date for Bob (1/2/22) where he is ranked 2 with a status of M1. The Rank 1 and P1 are my conditions for a top rank.  Therefore, because Bob's date is 1/2/22 in one of the rows and he is rank 2 with a status of M1 then I want to exclude that row. Below are 2 tables of what I have and what I want along with a code. Please let me know whats the best way to approach this as im still learning. Thank you. 

 

Data Want;
set Have;

*just how im thinking but dont know how to code it;

If date is < date where Rank=1 and Status=P1
Run;

Have

NameDateRankStatus 
BOB1/2/231P1
BOB1/2/222M1

 

Want

NameDateRankStatus 
BOB1/2/231P1
2 REPLIES 2
Reeza
Super User

Sort data first using name and descending date, then select the first.

 

proc sort data=have;
by name descending date;
run;

data want;
set have;
by name descending date;
if first.name and rank=1 and status='P1';
run;
SASKiwi
PROC Star

If your selection rule is simply picking the top rank then try this:

Data Want;
set Have;
where Rank='1' and Status='P1';
run;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 768 views
  • 2 likes
  • 3 in conversation