Many a times while working with dates we need to compare multiple dates and need to calculate the differences in days / months / years / quarters etc.
In SAS, all this can be done using a very powerful function INTCK which is used to compare two dates and returns the difference between them.
Syntax:
INTCK( <duration> , date1,date2, 'type')
Duration: To calculate the difference in years / months / days / weekdays / weeks / quarters.
Task: Let us try to get the age of a person in years as on today!
TODAY( ) function in SAS returns today's date.
DATA _NULL_ : It does not create any SAS dataset
PUT : Does not open any output window rather, prints the result in the log.
For this we have defined <duration> as 'year' in INTCK
DATA _null_;
FORMAT birthdt date9. Today date9.;
birthdt = '18Oct2007'd;
Today = today();
diff = intck('year',birthdt, Today);
PUT "Birthdate is: " birthdt ", Today's date is: " Today ", Age is: " diff " years";
RUN;
In the log we can see the following output!
Task: Let us get the person's age in months
For this we have defined <duration> as 'month'
DATA _null_;
FORMAT birthdt date9. Today date9.;
birthdt = '18Oct2007'd;
Today = today();
diff = intck('month',birthdt, Today);
PUT "Birthdate is: " birthdt ", Today's date is: " Today ", Age is: " diff " mnths";
RUN;
Task: To get the difference in weeks we specify <duration> as 'week'
DATA _null_;
FORMAT birthdt date9. Today date9.;
birthdt = '18Oct2007'd;
Today = today();
diff = intck('week',birthdt, Today);
PUT "Birthdate is: " birthdt ", Today's date is: " Today ", Age is: " diff " weeks";
RUN;
Task: To get the difference in weeks we specify <duration> as 'qtr'
DATA _null_;
FORMAT birthdt date9. Today date9.;
birthdt = '18Oct2019'd;
Today = today();
diff = intck('qtr',birthdt, Today);
PUT "Birthdate is: " birthdt ", Today's date is: " Today ", Age is: " diff " quarters";
RUN;
Understanding <type> parameter in INTCK!
To understand the fourth parameter in INTCK let us firstly calculate:
Task: The difference in months between 18Oct2019 and 10Nov2019.
DATA _null_;
FORMAT birthdt date9. Today date9.;
birthdt = '18Oct2019'd;
Today = '10Nov2019'd;
diff = intck('month',birthdt, Today);
PUT "Birthdate is: " birthdt ", Today's date is: " Today ", Age is: " diff " month";
RUN;
We know that the difference is not even 1 month but because of default parameters INTCK returns 1 month.
By default: SAS compares th beginning of each duration to calculate the difference, irrespective of the day. Thus SAS has compared 1st Oct and 1st Nov 2019 - Leading to a difference of 1 month.
To compare exactly same dates we have defined fourth parameter as 'continuous' in INTCK.
DATA _null_;
FORMAT birthdt date9. Today date9.;
birthdt = '18Oct2019'd;
Today = '10Nov2019'd;
diff = intck('month',birthdt, Today,'continuous');
PUT "Birthdate is: " birthdt ", Today's date is: " Today ", Age is: " diff " month";
RUN;
Difference in weekdays
Task: Calculate the difference in weekdays between the two dates.
By default SAS considers weekends as Saturday and Sunday
DATA _null_;
FORMAT birthdt date9. Today date9.;
birthdt = '18Oct2019'd;
Today = '10Nov2019'd;
diff = intck('weekday',birthdt, Today,'continuous');
PUT "Birthdate is: " birthdt ", Today's date is: " Today ", Age is: " diff " weekdays";
RUN;
SAS uses following coding for the days of the weeks:
1: Sunday
2: Monday
3: Tuesday
4: Wednesday
5: Thursday
6: Friday
7: Saturday
To get the number of weekdays treating Sunday and Monday as holidays (weekends) we define <duration> as weekday12W.
DATA _null_;
FORMAT birthdt date9. Today date9.;
birthdt = '18Oct2019'd;
Today = '10Nov2019'd;
diff = intck('weekday12W',birthdt, Today,'continuous');
PUT "Birthdate is: " birthdt ", Today's date is: " Today ", Age is: " diff " weekdays";
RUN;
Similarly to treat Tuesday and Wednesday as weekends we set duration to weekday23W
Comments