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 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!

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