To find the time periods between two dates in a SAS data step, we can use the SAS intcx() function. The return value of intcx() is an integer.
data data_new;
set data;
num_days_between_d1_d2 = intck('day',d1,d2);
num_months_between_d1_d2 = intck('month',d1,d2);
num_years_between_d1_d2 = intck('years',d1,d2);
run;
When working with dates in our data, it’s very useful to be able to compute how much time there is between dates.
The SAS intcx() function allows us find the difference between dates very easily. We can compute the number of time units between two dates for a given interval (second, minute, hour, day, week, month, year).
The SAS intcx() function returns an integer, which can be interpreted as the number of “full” time periods between two dates.
Let’s say we have the following SAS dataset with some date variables.
data data_with_dates;
input d1 :date9. d2 :date9.;
format d1 date9. d2 date9.;
datalines;
31DEC2021 28FEB2022
24OCT2020 01JUN2023
12DEC2019 04MAY2021
07JUN2019 21NOV2022
17FEB2021 06JUN2024
12JAN2021 18FEB2019
03MAR2020 31JUL2017
;
run;
Let’s find the days, months and years between the two date variables. We can easily do so using intcx() in the following SAS code.
data date_new;
set data_with_dates;
num_days_between_d1_d2 = intck('day',d1,d2);
num_months_between_d1_d2 = intck('month',d1,d2);
num_years_between_d1_d2 = intck('years',d1,d2);
run;
/* Output */
d1 d2 num_days_between_d1_d2 num_months_between_d1_d2 num_years_between_d1_d2
1 31DEC2021 28FEB2022 424 14 2
2 24OCT2020 01JUN2023 950 32 3
3 12DEC2019 04MAY2021 509 17 2
4 07JUN2019 21NOV2022 1263 41 3
5 17FEB2021 06JUN2024 1205 40 3
6 12JAN2021 18FEB2019 -694 -23 -2
7 03MAR2020 31JUL2017 -946 -32 -3
How to Find Number of Days to Between dates in SAS Data Step
To find the number of days between two SAS date variables, we can use the SAS intcx() function. We pass ‘day’ to the ‘interval’ argument in the intcx() function.
intcx("day", date1, date2);
How to Find Number of Weeks to Between dates in SAS Data Step
To find the number of weeks between two SAS date variables, we can use the SAS intcx() function. We pass ‘week’ to the ‘interval’ argument in the intcx() function.
intcx("week", date1, date2);
How to Find Number of Months to Between dates in SAS Data Step
To find the number of months between two SAS date variables, we can use the SAS intcx() function. We pass ‘month’ to the ‘interval’ argument in the intcx() function.
intcx("month", date1, date2);
How to Find Number of Quarters to Between dates in SAS Data Step
To find the number of quarters between two SAS date variables, we can use the SAS intcx() function. We pass ‘qtr’ to the ‘interval’ argument in the intcx() function.
intcx("qtr", date1, date2);
How to Find Number of Years to Between dates in SAS Data Step
To find the number of yearsbetween two SAS date variables, we can use the SAS intcx() function. We pass ‘year’ to the ‘interval’ argument in the intcx() function.
intcx("year", date1, date2);
Hopefully this article has been useful for you to help you understand how to use the SAS intnx() function to increment the date for SAS date variables.