Search code examples
sassas-macro

SAS macro to get the last date of the current month


I would like to use a macro in SAS to calculate the last day of the current month when executed.

As i'm quite new to the SAS macro's i've tried to create on based on the information i've found on the internet. %let last_day = %sysfunc(putn(%sysfunc(intnx(month,%sysfunc(today()),e), date9.));

However it does not seem to work when i execute it.


Solution

  • You left out the number of intervals in the INTNX() function call.

    To create a macro variable with the string that looks like the last day of the current month in the style produced by the DATE9. format just use:

    %let last_day = %sysfunc(intnx(month,%sysfunc(today()),0,e), date9.);
    

    You could then use that macro variable to generate strings. Such as in a TITLE statement.

     TITLE "End of the month is &last_day";
    

    If you want to use it as an actual date you will need to convert it to a date literal by adding quotes and the letter d.

      ...
      where date <= "&last_day"d ;
    

    And if so it is probably simpler to not use the DATE9. format at all and just store the raw number of days since 1960 in the macro variable.

    %let last_day = %sysfunc(intnx(month,%sysfunc(today()),0,e));
    ...
    where date <= &last_day ;