DATA Step, Macro, Functions and more

How to create a complete date values from partially provided dates

Reply
Super Contributor
Posts: 272

How to create a complete date values from partially provided dates

Dear,

I need create a an ENDDATE variable from date variable values.

 

If complete date is provided then the date will be the end date.

If Month provided then last day of the month will be the ENDDATE value.
If year only provided then last day of the year will be the ENDDATE value.

 

Please help

 

id     date
1     2015-01-06
2     2015-11
3     2015
4     2015-02


Output needed:

ENDDATE

06JAN2015
30NOV2015
31DEC2015
28FEB2015

Respected Advisor
Posts: 4,644

Re: How to create a complete date values from partially provided dates

Generate a valid SAS date and then use SAS function intnx()

 

enddate = intnx("MONTH", date, 0, "END");

/* or */

enddate = intnx('YEAR", date, 0, "END");

PG
Super User
Posts: 10,497

Re: How to create a complete date values from partially provided dates

Here's one way for character values.


data want;
   set have;
   /* assumes your "date" is CHARACTER*/
   Enddate = input(date,anydtdte32.);
   /* missing is because year provides insufficient information to anydtdte*/
   /* so assign last day of dec as day and month*/
   if missing(enddate) then enddate = mdy(12,31,input(date,best4.));
   /* anydtdte when day of month is missing assigns day to be 1, this is 
   one way to advance to last day of current month */
   if length(date)=7 then enddate=intnx('month',enddate,1)-1;
   format enddate date9.;
run;
Ask a Question
Discussion stats
  • 2 replies
  • 198 views
  • 2 likes
  • 3 in conversation