📆 SAS Date Interval Functions Explained: INTNX vs INTCK with Examples

Date manipulation is a core part of data analysis in SAS. When working with time series, financial models, or scheduling, calculating intervals and moving dates is essential. That’s where SAS functions like INTNX and INTCK come in.

SAS Date Functions - INTNX and INTCK

In this blog post, we will explore:

  • What are INTNX and INTCK functions?
  • Complete syntax and arguments
  • Important options
  • Practical real-life examples
  • Comparison and use-cases

🔍 What is INTNX in SAS?

The INTNX (Interval Next) function adds a specified number of intervals to a date or datetime and returns a new date.

📌 Syntax:

INTNX(interval, start-from, increment <, 'alignment' <, method>>)

✅ Arguments:

ParameterDescription
intervalThe type of interval to add (e.g., day, week, month, year).
start-fromThe base SAS date or datetime value.
incrementNumber of intervals to move forward or backward.
'alignment' (optional)'BEGINNING', 'MIDDLE', 'END', 'SAME' (default is 'BEGINNING').
method (optional)'S' (simple), 'C' (concurrent), or 'E' (end of period alignment).

🧪 INTNX Examples

Example 1: Add 3 months to a date

data result;
new_date = intnx('month', '01JAN2024'd, 3); format new_date date9.; run;

Output: 01APR2024


Example 2: Move 2 years back and align to end of year

data result;
year_end = intnx('year', '15JUL2024'd, -2, 'END'); format year_end date9.; run;

Output: 31DEC2022


Example 3: Use 'SAME' alignment

data result;
same_date = intnx('month', '10JAN2024'd, 1, 'SAME'); format same_date date9.; run;

Output: 10FEB2024


🧠 Pro Tip:

You can use INTNX with time intervals too, like 'hour', 'minute', 'qtr', 'dtday', etc.


⏳ What is INTCK in SAS?

The INTCK (Interval Check) function calculates the number of intervals between two dates or datetimes.

📌 Syntax:

INTCK(interval, start, end <, 'method'>)

✅ Arguments:

ParameterDescription
intervalType of interval (e.g., day, week, month, year).
startStarting SAS date or datetime.
endEnding SAS date or datetime.
'method' (optional)'C' (continuous), 'D' (discrete), or 'S' (simple)

🧪 INTCK Examples

Example 1: Count number of months between two dates

data result;
month_diff = intck('month', '01JAN2023'd, '01JUL2023'd); run;

Output: 6


Example 2: Days between two dates

data result;
day_diff = intck('day', '15MAR2023'd, '20APR2023'd); run;

Output: 36


Example 3: Count number of years using 'CONTINUOUS' method

data result;
year_cont = intck('year', '31DEC2019'd, '01JAN2021'd, 'C'); run;

Output: 2


🔄 INTNX vs INTCK in SAS

FeatureINTNXINTCK
PurposeMove a date forward/backwardCalculate number of intervals
ReturnsDate or datetime valueInteger (count of intervals)
Optional ArgAlignment ('BEGINNING', etc.)Method ('C', 'D', 'S')
Use CaseScheduling future/past eventsAnalyzing gaps/durations

📘 Common Intervals in SAS

Interval TypeExampleDescription
day'day'Daily interval
week'week'Weekly interval
month'month'Monthly interval
qtr'qtr'Quarterly interval
year'year'Yearly interval
dtday'dtday'Datetime day interval
hour'hour'Hour interval

🛠 Real-World Use Cases

🧾 1. Loan Payment Schedules

due_date = intnx('month', start_date, 12, 'END');

📊 2. Monthly Sales Comparison

month_diff = intck('month', previous_sale, current_sale);

🧑‍💼 3. Employee Tenure Calculation

years_worked = intck('year', hire_date, today());

🎯 Tips for Using INTNX and INTCK

  • Always format the date with FORMAT datevar DATE9. or DATETIME20..
  • Use 'SAME' alignment when you want the exact day repeated.
  • Prefer 'C' method in INTCK for financial year or continuous interval calculations.


🧾 Summary

FunctionPurposeReturnsKey Argument
INTNXAdd intervals to a dateDate'alignment'
INTCKCount intervals between two datesInteger'method'

Both INTNX and INTCK are indispensable tools in time-based analysis in SAS. Whether you’re calculating tenure, creating forecasts, or aligning schedules, mastering these functions will significantly enhance your date handling capabilities.

Keywords: SAS INTNX function, SAS INTCK function, SAS date intervals, SAS date functions, INTNX vs INTCK in SAS, INTCK examples, INTNX options

Post a Comment

0 Comments