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

I have a column of dates. Is there a way to make a new field that returns the closest month end?

 

For example,

Make 3/27/2020 -> 3/31/2020

Make 1/28/2017 -> 1/31/2017

etc.

 

My date field is currently a "date 9" format if that makes a difference!

 

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star

On the assumption you are going forward to the nearest month-end:

data test;
  My_Date = '27Mar2020'd;
  Month_End = intnx('MONTH', My_Date , 0, 'END');
  format My_Date Month_End date9.;
  put _all_;
run;

 

View solution in original post

4 REPLIES 4
SASKiwi
PROC Star

On the assumption you are going forward to the nearest month-end:

data test;
  My_Date = '27Mar2020'd;
  Month_End = intnx('MONTH', My_Date , 0, 'END');
  format My_Date Month_End date9.;
  put _all_;
run;

 

Reeza
Super User

What do you want for 3/14/2020 or 2/15/2020 (leap month!), in terms of nearest date?

 

IMO, using @SASKiwi suggestion of end of the current month is a good approach but different than what you seem to be requesting. 

 


@anweinbe wrote:

I have a column of dates. Is there a way to make a new field that returns the closest month end?

 

For example,

Make 3/27/2020 -> 3/31/2020

Make 1/28/2017 -> 1/31/2017

etc.

 

My date field is currently a "date 9" format if that makes a difference!

 


 

s_lassen
Meteorite | Level 14

If I understand your requirement correctly, you want to map e.g. February 3rd into January 31st. How about this:

data have;
  format date date9.;
  input date date9.;
cards;
03JAN2012
23DEC2019
15JAN2013
;run;

data want;
  set have;
  format date_eom date9.;
  date_eom=intnx('month',date,0,'E');
  if date_eom-date>day(date) then
    date_eom=intnx('month',date,-1,'E');
run;
anweinbe
Quartz | Level 8

I believe that I will always be going forward not backwards. I should have specified that. In any case I will save this too! I appreciate your response :-).

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2135 views
  • 0 likes
  • 4 in conversation