🕒Must-Know Date & Time
Functions in Base SAS
📅 From Age Calculations to Scheduling Visits
These functions are the backbone of real-world SAS programming.
Let's decode each with clear logic and syntax!
➡️ Swipe to begin
Dayanand Yengandul 🇮🇳
INTCK() – Count Intervals Between Dates
📝Syntax:
x = INTCK('interval', start_date, end_date);
📌Common intervals: 'day', 'week', 'month', 'qtr', 'year'
💡 Use:
Returns the number of complete intervals (like days,
months, years) between two dates.
🎯 Use Cases:
Calculate patient age
Measure treatment duration
Track time between visits
✅ Examples:
months = INTCK('month', '01JAN2024'd, '15MAR2024'd);
→ 2 months
years = INTCK('year', dob, today()); → age in years
🧠 Tip: INTCK counts the number of intervals crossed.
E.g. Jan to Mar = 2 months, even if it’s mid-March.
INTNX() – Increment Dates by Intervals
(Forward or Backward)
📝Syntax:
x = INTNX('interval', date, No_of_intervals, <alignment>);
📌Common intervals: 'day', 'week', 'month', 'qtr', 'year'
📌 Alignment options: 'b' = beginning, 'm' = middle, 'e' = end, 's' = same date
📌 The no_of_intervals increments the date:
positive = future, negative = past, 0 = same interval
💡 Use:
Returns a new SAS date by incrementing from a starting date using the
given interval and alignment.
🎯 Use Cases:
Schedule follow-up visits
Calculate past/future visit dates
Define clinical time windows (e.g., treatment phases)
✅ Examples:
next_month = INTNX('month', '15JAN2024'd, 1, 's'); → 15FEB2024
last_qtr = INTNX('qtr', '10MAY2024'd, -1, 'e'); → 31MAR2024
🧠 Tip: Use 's' to stay on the same day, 'e' to jump to the end of the interval.
perfect for scheduling visits.
DATDIF() & YRDIF()
Calculate Exact Days and Years
📝Syntax:
x = DATDIF(date1, date2, 'ACT/ACT');
y = YRDIF(date1, date2, 'ACT/ACT');
📌 DATDIF() returns exact number of days
📌 YRDIF() returns precise fractional years (e.g., 4.75 years)
💡 Use:
These functions are ideal when you need precise age or duration between
two dates — especially in clinical and regulatory reporting.
🎯 Use Cases:
Calculate patient age at visit
Determine study exposure duration
Derive age for inclusion/exclusion criteria
✅ Examples:
age = YRDIF(dob, today(), 'ACT/ACT'); → 24.5 years
duration = DATDIF('01JAN2024'd, '15MAR2024'd, 'ACT/ACT');
→ 74 days
🧠 Tip: 'ACT/ACT' counts actual calendar days.
YRDIF() is the standard for calculating age in clinical SDTM datasets.
TODAY(), TIME(), MDY()
Get Current Date/Time or Create a Date Manually in SAS
📝Syntax:
date = TODAY(); → returns current system date
time = TIME(); → returns current time in seconds since midnight
dob = MDY(12, 25, 1999); → builds a SAS date - Month, Day & Year
💡 Use:
Get the current date/time or create a SAS date from raw values like DOB.
🎯 Use Cases:
Add current date/time in reports
Time-stamp records for logging
Convert M/D/Y fields into valid SAS dates
✅ Examples:
today_dt = TODAY(); → 26JUN2025
cur_time = TIME(); → 56783 seconds
birth_dt = MDY(12, 25, 1999); → 25DEC1999
🧠 Tip: Use FORMAT datevar DATE9. to display dates clearly.
MDY() is helpful when DOB is split into separate fields.
YEAR(), MONTH(), DAY(), QTR(), WEEKDAY()
Extract Parts from a Date
📝Syntax:
yr = YEAR(date); mon = MONTH(date);
wk = WEEKDAY(date); qtr = QTR(date);
📌 Extract specific parts from a SAS date:
YEAR() = year, MONTH() = 1–12, DAY() = 1–31
QTR() = quarter (1–4), WEEKDAY() = day of week (1=Sunday)
💡 Use:
Break down a date into components for grouping, filtering, or reporting.
🎯 Use Cases:
Group patients by month, quarter, or year
Filter records by weekend/weekday
Perform time-based analysis (e.g., monthly trends)
✅ Examples:
yr = YEAR('26Jun2025'd); → 2025
qt = QTR('26Jun2025'd); →2
mon = MONTH('26Jun2025'd); →6
wk = WEEKDAY('26Jun2025'd); →5
🧠 Tip: These functions help group data by time. WEEKDAY() is useful to
flag weekends.
🕒Thanks for Exploring SAS Date & Time Functions!
Hope this guide helped simplify timelines, age logic, and
scheduling in SAS.
🧠 Let’s learn together
📌 Follow for more tips on Base SAS & Clinical SAS
💬 Drop questions or suggestions in the comments!
Dayanand Yengandul 🇮🇳
Learning Clinical SAS | B. Pharmacy Graduate