BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
pharaohjb
Calcite | Level 5

I have a dataset that is set up as so:

Var1     Var2     Var3

1            A          1 January 2021

1            A          1 January 2021

1           A           5 January 2021

1            A          5 January 2021

1          B            31 Oct 2020

1           B            2 Nov 2020

My question is how can I ONLY keep the rows associated with the most recent values of Var2?  That is, I want all the rows associated with 5 Jan 2021 for Var2=A, and all the rows associated with 2 Nov 2020 for Var2=B, etc.

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

A classic use-case for REMERGE and HAVING clause combine to subset-

 

proc sql;
  create table want as
  select *
  from have
  group by var1, var2
  having max(var3)=var3;
quit;

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20

A classic use-case for REMERGE and HAVING clause combine to subset-

 

proc sql;
  create table want as
  select *
  from have
  group by var1, var2
  having max(var3)=var3;
quit;
Ksharp
Super User
data have;
input Var1     Var2   $  Var3 $40.;
cards;
1            A          1 January 2021
1            A          1 January 2021
1           A           5 January 2021
1            A          5 January 2021
1          B            31 Oct 2020
1           B            2 Nov 2020
;
data want;
 do until(last.var3);
  set have;
  by var1 var2 var3 notsorted;
  if last.var2 then last=1;
 end;
 do until(last.var3);
  set have;
  by var1 var2 var3  notsorted;
  if last then output;
 end;
 drop last;
 run;

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