SAS has a really neat function called intnx, which will increment a date to the next of an interval.

For example, if you have a date (any date. maybe the current date?), you can get the date of the first day of the next month by doing this:

data _null_;
  d = '11JUN2011'd;
  format d date9.;
  put d;
  d = intnx('month',d,1);
  put d;
run;

Which outputs

11JUN2011
01JUL2011

Here’s a clever way to get the “Last Day” of the current month. The last day of the current month is the day before the first day of the next month. SAS Dates are internally stored as a number of days since some point, so just subtract one from it.

data _null_;
  d = '11JUN2011'd;
  format d date9.;
  put d;
  d = intnx('month',d,1)-1;
  put d;
run;

Which outputs

11JUN2011
30JUN2011

5 thoughts on “How to get the Last Day of a Month in SAS

  1. You don’t really have to be clever, but can use the alignment argument set to ‘ending’ with an interval of zero to get the last day of a month on a give date.

    d = intnx(‘month’,d,0,’e’);

    Reply
    • LOL you must read all the parameters of the any sas function.. before you be clever and of course before you share your wisdom by posting in a blog..

      Reply
      • Bill…I guess you should not ridicule without exploring yourself.
        Laurent had the correct answer

        Reply
  2. May Be using this you can find the End date of the current month.

    data _null_;
    a = today();
    put a;
    d = put(intnx(‘month’,a,1)-1,date9.);
    put d;
    run;

    Reply

Leave a reply

required

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>