4.2 C
London
Friday, December 20, 2024
HomeSASData Munging in SASHow to Calculate Difference Between Two Dates in SAS

How to Calculate Difference Between Two Dates in SAS

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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 print data=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 print data=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 print data=new_data;

Notice the difference between this table and the previous table.

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:

How to Normalize Data in SAS
How to Replace Characters in a String in SAS
How to Replace Missing Values with Zero in SAS
How to Remove Duplicates in SAS

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories