How to get the Last Day of a Month in SAS

by philihp

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

But to get the “Last Day” of the current month, you have to be clever. 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