SAS has a really interesting function known as INTNX. You can use this function to control the dates and get desired value by passing proper arguments in the INTNX function.
SAS INTNX Function:
The INTNX function increments a date, time, or datetime value by intervals such as DAY, WEEK, QTR, and MINUTE, or a custom interval that you define.
Syntax:
INTNX(Interval, start-from, increment , “alignment” (optional) );
The following example shows how to determine the date of the start of the month that is 1 month from the month of the given date.
To convert the SAS date value to a calendar date, use any valid SAS date format, such as the DATE9. format.
data _null_;
x=intnx('month', '15MAY2025'd , 1);
put x date9.;
run;
INTNX returns the value 01JUN2025.
Similarly, you can find the date of the first day of the last month by passing increment value “-1”.
data _null_;
x=intnx('month', '15MAY2025'd , -1);
put x date9.;
run;
Above code returns the following output: 01APR2025
Calculate the Last day of the Last Month in SAS:
INTNX is a very powerful function. If you observe in the above examples we haven’t used “alignment” arguments.
But here you can use “alignment” to get the desired output “last day of the last month” by writing minimal code.
“alignment“: It controls the position of SAS dates within the interval.
- BEGINNING (B) : returned date value is aligned to the beginning of the interval.
- MIDDLE (M) : returned date value is aligned to the midpoint of the interval.
- END (E) : returned date value is aligned to the end of the interval.
- SAME (S) : date that is returned has the same alignment as the input date
In the previous example you can use the END (E) argument to get the last date of the last month.
data _null_;
firstDayofTheLastMonth=intnx('month', '15MAY2025'd , -1);
put firstDayofTheLastMonth= date9.;
lastDayofTheLastMonth=intnx('month', '15MAY2025'd , -1, 'E');
put lastDayofTheLastMonth= date9.;
run;
Above code returns the following output:
firstDayofTheLastMonth=01APR2025
lastDayofTheLastMonth=30APR2025
In case if you’re only interested in getting the last day of last month (not the entire date) then you can use the DAY() function and assign value to the macro variable.
data _null_;
firstDayofTheLastMonth=intnx('month', '15MAY2025'd , -1);
lastDayofTheLastMonth=intnx('month', '15MAY2025'd , -1, 'E');
lastDaylastMonth=day(intnx('month', '15MAY2025'd , -1, 'E'));
put firstDayofTheLastMonth=date9.;
put lastDayofTheLastMonth=date9.;
put lastDaylastMonth=;
run;
Create a macro variable and assign value: last day of the last month
/* create macro variable and assign Last Day of Last Month value */
data _null_;
lastDaylastMonth=day(intnx('month', '15MAY2025'd , -1, 'E'));
call symput("LastDayofLastMonth", lastDaylastMonth);
run;
%put LastDayofLastMonth= &LastDayofLastMonth ;
Output:
LastDayofLastMonth= 30
The INTNX function in SAS can be used whenever you’re dealing with the dates and calculating last or first day, date, month, or year from any month or year. It’s so powerful function that is useful in various scenarios.
FAQ
The last date of the last month can be calculated using INTNX function with alignment= ‘E’
lastDayofTheLastMonth=intnx('month', current_date , -1, 'E');
The last date of the last month can be calculated using INTNX function with alignment= ‘E’. With DAY() function in SAS further you can extract day from that date.
lastDaylastMonth=day(intnx('month', current_date, -1, 'E'));