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

I have a data set like this :

ID      NAME      BEGIN               END

345   CARL      16MAR2017     31MAR2017

345   CARL      03APR2017      30APR2017

120   ROSA      01JAN2017    14FEB2017

120   ROSA      02DEC2016      31DEC2016

 

How can I delete occurence but taking the most recent date ? I mean get some thing like this :

ID      NAME      BEGIN               END

345   CARL      03APR2017      30APR2017

120   ROSA      01JAN2017    14FEB2017

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

SAS can handle this easily enough, but the solution in part depends on whether your BEGIN and END variables are valid SAS dates vs. whether they are character strings.  PROC CONTENTS will tell you that.  If they are valid SAS dates:

 

proc sort data=have;

by id begin;

run;

 

data want;

set have;

by id begin;

if last.id;

run;

 

If your dates are actually character strings, does "JANV" actually appear in your data, or is that supposed to be "JAN"?  If there are invalid dates (which would include "JANV") they would need to be fixed first.  Then ...

 

data want;

set have;

begin_sasdate = input(begin, date9.);

end_sasdate = input(end, date9.);

format begin_sasdate end_sasdate date9.;

run;

 

proc sort data=want;

by id begin_sasdate;

run;

 

data want;

set want;

by id begin_sasdate;

if last.id;

run;

 

It's the same approach, but this approach requires that your data contain SAS dates, not character strings.

View solution in original post

2 REPLIES 2
Astounding
PROC Star

SAS can handle this easily enough, but the solution in part depends on whether your BEGIN and END variables are valid SAS dates vs. whether they are character strings.  PROC CONTENTS will tell you that.  If they are valid SAS dates:

 

proc sort data=have;

by id begin;

run;

 

data want;

set have;

by id begin;

if last.id;

run;

 

If your dates are actually character strings, does "JANV" actually appear in your data, or is that supposed to be "JAN"?  If there are invalid dates (which would include "JANV") they would need to be fixed first.  Then ...

 

data want;

set have;

begin_sasdate = input(begin, date9.);

end_sasdate = input(end, date9.);

format begin_sasdate end_sasdate date9.;

run;

 

proc sort data=want;

by id begin_sasdate;

run;

 

data want;

set want;

by id begin_sasdate;

if last.id;

run;

 

It's the same approach, but this approach requires that your data contain SAS dates, not character strings.

John4
Obsidian | Level 7

yes its valid date (I edit the topic, its JAN instead of JANV)

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 971 views
  • 1 like
  • 2 in conversation