BookmarkSubscribeRSS Feed
nickspencer
Obsidian | Level 7
Hi,
I Have a table that has record with multiple dates. I need to create a dataset with each record with maximum date. The source table looks as below.

ID STATE eff_date
123 CA 10/18/2017
123 CA 10/18/2017
123 NY 3/25/2018
123 NY 3/25/2018
234 AZ 1/1/2018
234 AZ 7/18/2018
234 ? 7/18/2018
I need to pick the ID and state with maximum effective date. I need to pick just one id and a state even though they have duplicates.
Any logic for this ?

Thanks
5 REPLIES 5
novinosrin
Tourmaline | Level 20

data have;
input ID STATE $ eff_date : mmddyy10.;
format eff_date mmddyy10.;
cards;
123 CA 10/18/2017
123 CA 10/18/2017
123 NY 3/25/2018
123 NY 3/25/2018
234 AZ 1/1/2018
234 AZ 7/18/2018
;
proc sort data=have out=_have;
by id state descending eff_date;
run;


data want;
set _have;
by id state descending eff_date;
if first.state;
run;

or

 


proc sort data=have out=_have;
by id state  eff_date;
run;


data want;
set _have;
by id state  eff_date;
if last.state;
run;

 or

 

proc sql;
create table want as
select distinct *
from have
group by id,state
having eff_date=max(eff_date);
quit;
PGStats
Opal | Level 21

Which record do you want when the latest date is duplicated?

PG
nickspencer
Obsidian | Level 7
@PGStats I see the city values are same when eff date is duplicated while some have null values.
novinosrin
Tourmaline | Level 20

Please provide us a better and comprehensive sample of what you have and your required output for the sample

PGStats
Opal | Level 21

Assuming you means STATE and not CITY and that you prefer a record with a non missing STATE when there there is one but a missing STATE when it's the only one on the LAST eff_date :

 

data test;
input ID STATE $ eff_date :mmddyy.;
format eff_date yymmdd10.;
datalines;
123 CA 10/18/2017
123 CA 10/18/2017
123 NY 3/25/2018
123 NY 3/25/2018
234 AZ 1/1/2018
234 AZ 7/18/2018
234 . 7/18/2018
;

proc sort data=test out=last; 
by descending eff_date descending state; run;

data want; set last(obs=1); run;

proc print; run;
PG

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 Update

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
  • 5 replies
  • 2834 views
  • 1 like
  • 3 in conversation