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;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1100 views
  • 2 likes
  • 3 in conversation