SAS has a really interesting function INTNX where you can control dates and it can be used to get any desired value from dates. Furthermore you can easily assign that value to the macro variable.
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) );
Interval: It can be set as ‘year’, ‘month’, ‘week’, ‘weekday’, etc.
“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
The following example shows how to determine the date of the start of the year that is 5 years from the year 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('year', '15MAY2025'd , 5);
put x=date9.;
run;
INTNX returns the value 01JAN2030.
How to Get the Last Day of the Month?
If you have any date or maybe today’s date (more dynamic), for now let’s assume 05MAY2025 and you want to get the last day of the month.
Approach 1:
Last day of the month= (First day of the next month) - 1
You can easily get the date of the first day of the next month by using INTNX with the ‘month’ parameter.
data _null_;
current_date='05MAY2025'd;
firstDayofNextMonth=intnx('month', current_date, 1);
put current_date=date9.;
put firstDayofNextMonth=date9.;
run;
The above code returns the following output:
current_date=05MAY2025
firstDayofNextMonth=01JUN2025
Now you know what is the date of the first day of the next month. To calculate the last day of the current month you can simply subtract 1 day from the first day of the next month.
Simple formula:
Last day of the current month= (First day of the next month) – 1 day
lastDayofTheCurrentMonth = intnx('month',current_date,1)-1;
/* Example to calculate last day of the month */
data _null_;
current_date='05MAY2025'd;
lastDayofTheCurrentMonth=intnx('month', current_date, 1)-1;
put current_date=date9.;
put lastDayofTheCurrentMonth=date9.;
run;
Approach 2: Easy way to calculate Last day of the Month in SAS
There are multiple ways to calculate the last day of the given month but here is one more way which is even easier than the previous one.
INTNX has “alignment” as an optional parameter to pass. It controls the position of SAS dates within the interval.
You can specify ‘E’ that set the alignment to the end. If you pass current date, set ‘month’ as an interval, and increment 0, it will give you last date of the month with alignment=’E’ option.
data _null_;
current_date='05MAY2025'd;
firstDayofTheCurrentMonth=intnx('month', current_date, 0);
lastDayofTheCurrentMonth=intnx('month', current_date, 0, 'E');
lastDayofTheNextMonth=intnx('month', current_date, 1, 'E');
put firstDayofTheCurrentMonth=date9.;
put lastDayofTheCurrentMonth=date9.;
put lastDayofTheNextMonth=date9.;
run;
FAQ
You can calculate last day of the month using to approaches:
1. lastDayofTheMonth= firstDayofNextMonth – 1
2. lastDayofTheMonth= Use INTNX function with alignment=’E’
You can easily calculate first day of the current month by using INTNX function with ‘month’ interval.
firstDayofTheCurrentMonth=intnx('month', current_date, 0);
You can easily calculate the last day of the Next month by using INTNX function with ‘month’ interval.
lastDayofTheNextMonth=intnx('month', current_date, 1, 'E');