BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

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

2 REPLIES 2
PGStats
Opal | Level 21

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
ballardw
Super User

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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