How do I calculate the difference between two dates in SAS? 2

How do I calculate the difference between two dates in SAS?

Calculating the difference between two dates in SAS can be done using the INTCK function. This function calculates the number of time intervals between two dates, such as days, weeks, months, or years. The syntax for using INTCK is INTCK(interval, start_date, end_date), where the interval represents the desired time interval. The result of the INTCK function can be stored in a new variable or used in further calculations. This function is useful for analyzing time-based data and can aid in tasks such as calculating age or tracking the duration between events. By using the INTCK function, SAS users can easily determine the difference between two dates and incorporate this information into their data analysis.

Calculate Difference Between Two Dates in SAS


You can use the INTCK function in SAS to quickly calculate the difference between two dates in SAS.

This function uses the following basic syntax:

INTCK(interval, start date, end data, method)

where:

  • interval: Interval to calculate (day, week, month, year, etc.)
  • start date: The start date
  • end date: The end date
  • method: Whether to count complete intervals (‘D’ = No (Default), ‘C’ = Yes)

The following example shows how to use this function in practice.

Example : Calculate Difference Between Dates in SAS

Suppose we have the following dataset in SAS that contains two date variables:

/*create dataset*/
data original_data;
    format start_date end_date date9.;
    input start_date :date9. end_date :date9.;
    datalines;
01JAN2022 09JAN2022
01FEB2022 22FEB2022 
14MAR2022 04APR2022
01MAY2022 14AUG2022
;
run;

/*view dataset*/
proc printdata=original_data;

We can use the following code to calculate the difference between the values in the start_date and end_date variables in days, weeks, and months:

/*create new dataset*/
data new_data;
    set original_data;
    days_diff = intck('day', start_date, end_date);
    weeks_diff = intck('weeks', start_date, end_date);
    months_diff = intck('months', start_date, end_date);
run;

/*view new dataset*/
proc printdata=new_data;

The three new variables show the difference between start_date and end_date in days, weeks, and months.

Note that we can use the ‘c‘ argument in the INTCK function to only calculate the difference in complete days, weeks, and months:

/*create new dataset*/
data new_data;
    set original_data;
    days_diff = intck('day', start_date, end_date, 'c');
    weeks_diff = intck('weeks', start_date, end_date, 'c');
    months_diff = intck('months', start_date, end_date, 'c');
run;

/*view new dataset*/
proc printdata=new_data;

In this table, the difference in weeks between Jan 1st and Jan 9th is calculated as 1 since only one whole week can fit between these dates.

However, in the previous table the difference in weeks was calculated as 2 since there were two partial weeks that fit between these two dates.

Additional Resources

The following tutorials explain how to perform other common tasks in SAS:

Cite this article

stats writer (2024). How do I calculate the difference between two dates in SAS?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-i-calculate-the-difference-between-two-dates-in-sas/

stats writer. "How do I calculate the difference between two dates in SAS?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-do-i-calculate-the-difference-between-two-dates-in-sas/.

stats writer. "How do I calculate the difference between two dates in SAS?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-i-calculate-the-difference-between-two-dates-in-sas/.

stats writer (2024) 'How do I calculate the difference between two dates in SAS?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-i-calculate-the-difference-between-two-dates-in-sas/.

[1] stats writer, "How do I calculate the difference between two dates in SAS?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.

stats writer. How do I calculate the difference between two dates in SAS?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top