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

Dear all,

 

For the following sample dataset I need to retrieve the last value of DATE and TIME within each group (variable ID).

data have;

 

IDDATETIME
50000110/08/2018141110
50000121/08/2018104404
50000122/08/2018140100
50000212/06/2018103810
50000210/08/2018153104
50000214/08/2018125737
50000304/08/2018094524
50000306/08/2018134535
50000308/08/2018115742
50000310/08/2018164127
50000312/08/2018092203
50000314/08/2018105922

;

run;

 

Therefore the output shall be:

 

IDDATETIME
50000122/08/2018140100
50000214/08/2018125737
50000314/08/2018105922

 

Thank you all.

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data have;
input ID $	DATE  :ddmmyy10.	TIME;
format date ddmmyy10.  ;
cards;
500001	10/08/2018	141110
500001	21/08/2018	104404
500001	22/08/2018	140100
500002	12/06/2018	103810
500002	10/08/2018	153104
500002	14/08/2018	125737
500003	04/08/2018	094524
500003	06/08/2018	134535
500003	08/08/2018	115742
500003	10/08/2018	164127
500003	12/08/2018	092203
500003	14/08/2018	105922
;

data want;
if 0 then set have;
call missing(dt);
do until(last.id);
set have;
by id;
dt=dt<>dhms(date,0,0,time);
end;
drop dt;
run;

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20
data have;
input ID $	DATE  :ddmmyy10.	TIME;
format date ddmmyy10.  ;
cards;
500001	10/08/2018	141110
500001	21/08/2018	104404
500001	22/08/2018	140100
500002	12/06/2018	103810
500002	10/08/2018	153104
500002	14/08/2018	125737
500003	04/08/2018	094524
500003	06/08/2018	134535
500003	08/08/2018	115742
500003	10/08/2018	164127
500003	12/08/2018	092203
500003	14/08/2018	105922
;

proc sql;
create table want(drop=dt) as
select *,dhms(date,0,0,time) as dt
from have
group by id
having dt=max(dt);
quit;
novinosrin
Tourmaline | Level 20
data have;
input ID $	DATE  :ddmmyy10.	TIME;
format date ddmmyy10.  ;
cards;
500001	10/08/2018	141110
500001	21/08/2018	104404
500001	22/08/2018	140100
500002	12/06/2018	103810
500002	10/08/2018	153104
500002	14/08/2018	125737
500003	04/08/2018	094524
500003	06/08/2018	134535
500003	08/08/2018	115742
500003	10/08/2018	164127
500003	12/08/2018	092203
500003	14/08/2018	105922
;

data want;
if 0 then set have;
call missing(dt);
do until(last.id);
set have;
by id;
dt=dt<>dhms(date,0,0,time);
end;
drop dt;
run;
ballardw
Super User

If the data is sorted so that the last is correct then

 

data want;
   set have;
   by id;
   if last.id;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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