• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / SAS / intcx SAS – Find Time Periods Between Two Dates in SAS Data Step

intcx SAS – Find Time Periods Between Two Dates in SAS Data Step

January 23, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  SAS rename Statement – How to Rename Variables in a Data Step
  • 2.  SAS If Then Statements with Multiple Variables
  • 3.  Multiple Condition If Statements in SAS Macro Language
  • 4.  SAS Uppercase – Make String Letters Uppercase with SAS upcase function
  • 5.  SAS scan Function – Return nth Word from Character String
  • 6.  SAS Month Year Format monyyw.
  • 7.  SAS Remove Labels from Dataset with PROC DATASETS
  • 8.  SAS trim – Remove All Trailing Blanks from String Variable in Data Step
  • 9.  SAS include – Execute Code from Other Files in SAS with %include
  • 10.  SAS round – Rounding Numbers in a SAS Data Step

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy