The easiest way to add days to a date variable in SAS is to use the INTNX function.
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
The following example shows how to use this syntax in practice.
Example: Add Days to Date in SAS
Suppose we have the following dataset in SAS that shows the total sales made on various days at some store:
/*create dataset*/
data data1;
input month day year sales;
datalines;
10 15 2022 45
10 19 2022 50
10 25 2022 39
11 05 2022 14
12 19 2022 29
12 23 2022 40
;
run;
/*create second dataset with date formatted*/
data data2;
set data1;
date=mdy(month,day,year);
format date mmddyy10.;
drop month day year;
run;
/*view dataset*/
proc print data=data2;
We can use the following code to create a new column called date_plus5 that adds five days to the values in the date column:
/*create new dataset with column that adds 5 days to date*/
data data3;
set data2;
date_plus5=intnx('day', date, 5);
format date_plus5 mmddyy10.;
run;
/*view dataset*/
proc print data=data3;
Notice that the new column called date_plus5 contains the values in the date column with fives days added to them.
Note that 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 to date*/
data data3;
set data2;
date_minus5=intnx('day', date, -5);
format date_minus5 mmddyy10.;
run;
/*view dataset*/
proc print data=data3;
Notice that the new column called dateminus5 contains the values in the date column with fives days subtracted from them.
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 Normalize Data in SAS
How to Identify Outliers in SAS
How to Create Frequency Tables in SAS