Help using Base SAS procedures

Delete occurrences by keeping the most recent date

Accepted Solution Solved
Reply
Contributor
Posts: 36
Accepted Solution

Delete occurrences by keeping the most recent date

[ Edited ]

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

 


Accepted Solutions
Solution
‎04-15-2017 06:32 AM
Super User
Posts: 5,498

Re: Delete occurrences by keeping the most recent date

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


All Replies
Solution
‎04-15-2017 06:32 AM
Super User
Posts: 5,498

Re: Delete occurrences by keeping the most recent date

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.

Contributor
Posts: 36

Re: Delete occurrences by keeping the most recent date

Posted in reply to Astounding

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

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 155 views
  • 1 like
  • 2 in conversation