You can use the INTNX function in SAS to increment a date by a specific interval such as a day, week, month, etc.
This function uses the following basic syntax:
INTNX(interval, start_date, increment)
where:
- interval: The interval to add to date (day, week, month, year, etc.)
- start_date: Variable that contains start dates
- increment: The number of intervals to add
To subtract an interval, supply a negative number to the increment argument.
The following examples show some common ways to use the INTNX function in practice with the following dataset in SAS:
/*create dataset*/
data original_data;
format date date9.;
input date :date9. sales;
datalines;
01JAN2022 50
01FEB2022 34
14MAR2022 26
01MAY2022 22
24AUG2022 27
28OCT2022 48
14NOV2022 97
04DEC2022 88
;
run;
/*view dataset*/
proc print data=original_data;
Example 1: Use INTNX to Add Days to Date
We can use the INTNX function to create a new column called plus5days that adds five days to each date in the date column:
/*create new dataset with column that adds 5 days to date*/
data new_data;
set original_data;
plus5days=intnx('day', date, 5);
format plus5days date9.;
run;
/*view dataset*/
proc print data=new_data;
Notice that the new column called plus5days contains the values in the date column with fives days added to them.
Example 2: Use INTNX to Subtract Days from Date
You can also subtract days by simply using a negative value in the INTNX function.
For example, we can use the following code to subtract five days from each value in the date column:
/*create new dataset with column that subtracts 5 days from date*/
data new_data;
set original_data;
minus5days=intnx('day', date, -5);
format minusdays date9.;
run;
/*view dataset*/
proc print data=new_data;
Notice that the new column called minus5days contains the values in the date column with fives days subtracted from them.
Example 3: Use INTNX to Find First Day of Month
We can use the INTNX function to create a new column called firstmonth that contains the first day of the month for each date in the date column:
/*create new dataset with column that contains first day of the month*/
data new_data;
set original_data;
firstmonth=intnx('month', date, 0);
format firstmonth date9.;
run;
/*view dataset*/
proc print data=new_data;
Notice that the new column called firstmonth contains the first day of the month for each date in the date column.
Example 4: Use INTNX to Find First Day of Year
We can also use the INTNX function to create a new column called firstyear that contains the first day of the year for each date in the date column:
/*create new dataset with column that contains first day of the year*/
data new_data;
set original_data;
firstyear=intnx('year', date, 0);
format firstyear date9.;
run;
/*view dataset*/
proc print data=new_data;
Notice that the new column called firstyear contains the first day of the year for each date in the date column.
Note: You can find the complete documentation for the SAS INTNX function here.
Additional Resources
The following tutorials explain how to perform other common tasks in SAS:
How to Convert Datetime to Date in SAS
How to Convert Numeric Variable to Date in SAS
How to Calculate Difference Between Two Dates in SAS