BookmarkSubscribeRSS Feed
deleted_user
Not applicable
I have sas data set as below

id var2 var3 date
001 B 11 20080601
001 B 11 20080709
001 B 11 20090113

I have 3 lines but same id, i would like to keep only the latest date line (in this case is 20090113). How do i keep it ?
5 REPLIES 5
FredrikE
Rhodochrosite | Level 12
proc sort data = DSNAME;
by id date;
if last.id then output;
run;

//Fredrik
DanielSantos
Barite | Level 11
Somethings wrong about the code above.

The correct syntax would be.

proc sort data = yourdata; /* arrange data by id, and descending date */
by id descending date;
run;

proc sort nodupkey; /* keep first id of each group */
by id;
run;

OR:

proc sort data = yourdata; /* arrange data by id, and descending date */
by id descending date;
run;

data yourdata; /* keep first id of each group */
set yourdata;
by id;
if first.id;
run;

both do the same.

Greetings from Portugal.

Daniel Santos at www.cgd.pt.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
The SORT-only execution requires the EQUALS parameter, if not the default for your sort package. And the use of FIRST. or LAST. with regards to OUTPUT delivers the same result.

Scott Barry
SBBWorks, Inc.
DanielSantos
Barite | Level 11
Scott is right.
My system options is set to SORTEQUALS, so if you're not sure about yours, you should specify the EQUALS options for the sort procedure to ensure that the order is preserved.

_data null_'s solution is a great alternative (didn't knew about the idgroup option).

Greetings from Portugal.

Daniel Santos at www.cgd.pt.
data_null__
Jade | Level 19
PROC SUMMARY provides nice single step solution to this problem.

[pre]
data have;
input id:$3. var2:$1. var3 date :yymmdd.;
format date date9.;
cards;
001 B 11 20080601
001 B 11 20080709
001 B 11 20090113
002 B 11 20080601
002 a 12 20080709
002 B 11 20060113
;;;;
run;
proc summary data=work.have nway missing;
class id;
output out=need(drop=_:) idgroup(max(date) out(date var:)=);
run;
proc print;
run;
[/pre]

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 5 replies
  • 1006 views
  • 0 likes
  • 5 in conversation