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
Name | Date | Rank | Status |
BOB | 1/2/23 | 1 | P1 |
BOB | 1/2/22 | 2 | M1 |
Want
Name | Date | Rank | Status |
BOB | 1/2/23 | 1 | P1 |
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;
If your selection rule is simply picking the top rank then try this:
Data Want;
set Have;
where Rank='1' and Status='P1';
run;
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.
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.