Calculate Age From Two Dates in SAS
Use this interactive calculator to measure age, elapsed time, complete years, total months, and total days between two dates. It is designed for analysts, clinical programmers, data managers, and SAS users who need fast validation before writing production code.
Age Calculator
Results
Ready to calculate.
Choose two dates and click Calculate age to see SAS style age, exact span, total days, and a chart of the elapsed period.
How to calculate age from two dates in SAS
Calculating age from two dates in SAS looks simple at first, but it quickly becomes a quality issue if you ignore leap years, month boundaries, or partial birthdays. In regulated reporting, claims analytics, education research, survey processing, and customer data platforms, a one day shift can move a person into the wrong age band. That is why experienced SAS programmers do not rely on rough formulas like dividing days by 365. Instead, they choose a method that matches the business definition of age.
In SAS, age is usually derived from a date of birth and a reference date such as randomization date, treatment start date, exam date, enrollment date, or event date. The most common requirement is completed years of age, meaning the integer number of birthdays that have occurred by the reference date. Another common requirement is the exact elapsed interval expressed as years, months, and days. These are not identical. A patient can be 39 years old in completed years, while the exact span may be 39 years, 11 months, and 29 days.
Why SAS date values matter
SAS stores dates as integers representing the number of days since 01JAN1960. Because dates are numeric under the hood, the subtraction of two SAS dates directly returns a count of days. That is useful, but it is not enough to define age. If you simply compute end_date - birth_date, you get elapsed days, not legal age, clinical age, or reporting age. To convert days into meaningful age, you need interval logic.
Three approaches dominate practical SAS work:
- Completed years age for eligibility, demographics, and age bands.
- Exact span for audit checks, time since event, and longitudinal reporting.
- Decimal years for modeling, normalization, and some actuarial or analytic summaries.
Best SAS functions for age calculation
1. YRDIF for age in years
The SAS YRDIF function is widely used to calculate years between two dates. When the basis aligns with age logic, it can return a decimal year result that is closer to business expectations than a simple day count divided by 365. However, programmers still need to document the basis used and confirm that it matches the sponsor, department, or reporting specification.
This pattern is popular because it clearly separates the exact decimal age from the completed age in years. In many clinical and demographic contexts, the final age variable is the floored integer result.
2. INTCK with CONTINUOUS for completed birthdays
The INTCK function is another strong choice. Analysts often use it to count completed intervals. The continuous option is especially useful because it counts anniversaries more naturally than default boundary counting.
For many teams, this is one of the cleanest ways to derive age in completed years. It answers a direct question: how many full year anniversaries have passed between the start and end date? If you need to reconstruct the leftover months and days after completed years, you can then layer INTNX and a second date difference calculation.
3. INTNX for anniversary alignment
INTNX helps when you need to move from the date of birth to the exact anniversary that corresponds to the computed age. Once you know the number of complete years, INTNX can generate the birthday in the reference year. From there, you can derive remaining months or days.
This approach is excellent for transparent QC because it shows intermediate logic that can be reviewed by another programmer, statistician, or validator.
Choosing the right SAS age definition
There is no single universal age formula. The right method depends on your domain. Here is a practical comparison:
| Use case | Recommended SAS approach | Why it works | Main caution |
|---|---|---|---|
| Clinical demographics | intck('year', dob, refdt, 'continuous') |
Returns completed years, which matches most demographic table definitions. | Always confirm sponsor convention for leap day births. |
| Insurance and underwriting screens | floor(yrdif(dob, refdt, 'AGE')) |
Produces an age style year measure that is easy to store and compare. | Do not replace with days divided by 365. |
| Elapsed time reporting | INTCK plus INTNX plus date subtraction |
Lets you show years, months, and days in a traceable way. | Month end adjustments must be tested carefully. |
| Modeling and rate calculations | yrdif(...) decimal result |
Retains fractional years, helpful for continuous variables. | Document basis and rounding policy. |
Real calendar statistics that affect age calculations
Age logic is not just code. It is calendar math. A robust SAS implementation respects real world calendar structure. These basic statistics explain why rough approximations fail:
| Calendar fact | Value | Why SAS programmers care |
|---|---|---|
| Days in a common year | 365 | Using only 365 ignores leap years and causes drift over long intervals. |
| Days in a leap year | 366 | Any birth date or event span crossing February in leap years can shift age results. |
| Leap years in a 400 year Gregorian cycle | 97 | This is why the Gregorian average year length is 365.2425 days. |
| Average Gregorian year length | 365.2425 days | Better than 365 for decimal estimates, but still not a substitute for exact age logic. |
| Shortest month length | 28 days | Month based calculations must account for February, especially near month end. |
| Longest month length | 31 days | Elapsed month logic is not interchangeable with fixed 30 day assumptions. |
The takeaway is straightforward: if your definition of age is completed birthdays, use anniversary logic. If your definition is elapsed duration, compute an exact calendar span. If your definition is a continuous decimal variable, use an appropriate basis and document it.
Recommended step by step method in SAS
- Ensure both variables are valid SAS dates, not raw character strings.
- Normalize incoming values with
INPUTif needed. - Choose the business definition: completed years, exact span, or decimal years.
- Use
INTCKwith'continuous'orYRDIFas required. - Test edge cases such as leap day births, same day birthdays, month end dates, and reverse date order.
- Apply a clear rounding policy only after the correct raw result is derived.
- Validate a sample of records independently with a calculator or QC program.
Example with raw character dates
This pattern is easy to audit. It clearly converts strings to numeric SAS dates, applies formats for readability, and stores both the integer and decimal result. In many data pipelines, that combination is ideal because demographics tables may use the integer age while modeling or stratification checks may use the decimal age.
Common mistakes when calculating age from two dates in SAS
- Using days divided by 365. This is fast but wrong for many practical cases because it ignores leap years and anniversary boundaries.
- Ignoring date formats. A printed date that looks valid may still be a character string rather than a numeric SAS date.
- Using default INTCK boundary counting. Without the right option, interval counts can reflect boundaries crossed rather than full anniversaries completed.
- Not documenting leap day handling. Births on February 29 can expose hidden logic gaps and reconciliation disputes.
- Mixing business and analytic definitions. Eligibility criteria may require completed years, while a model input may require decimal age.
Edge cases every SAS programmer should test
Quality teams should always build a small edge case panel before production deployment. Include records where the reference date is exactly on the birthday, one day before the birthday, one day after the birthday, on February 28, on February 29, and on March 1. Also test records where the reference date precedes the birth date so your process can flag invalid input. If your organization stores partial dates, such as month and year only, define a separate imputation policy before calculating age.
Suggested QC scenarios
- 1990-01-15 to 2024-01-14 should be 33 completed years.
- 1990-01-15 to 2024-01-15 should be 34 completed years.
- 2000-02-29 to 2023-02-28 requires a documented leap day convention.
- 2024-05-01 to 2024-05-01 should return zero elapsed time.
- 2025-01-01 to 2024-12-31 should trigger an error or negative interval policy.
Why this matters in analytics and reporting
Age is often a stratification variable, an eligibility rule, a covariate, a disclosure suppression threshold, or a reporting dimension. A small logic error can therefore cascade into summary tables, exposure analyses, patient counts, school enrollment cohorts, and policy reporting. The practical impact is larger than many teams expect. In demographic summaries, one incorrect birthday threshold can move subjects between age bands, change denominators, and affect downstream tables. In longitudinal systems, repeated age derivations can create inconsistent snapshots if the formula changes over time.
If your workflow includes regulated outputs, keep the derivation explicit, version controlled, and independently reviewable. That usually means storing the raw source dates, the derived numeric age, the derivation method, and the reference date used. Transparent derivations make audit responses easier and reduce rework.
Authoritative references for date and time concepts
If you want external references to support your understanding of calendar and time standards, these are solid places to start:
- National Institute of Standards and Technology time and frequency resources
- U.S. Census Bureau population estimates and age related statistics
- U.S. National Library of Medicine resources for health data and terminology
Practical conclusion
To calculate age from two dates in SAS correctly, first define what age means in your project. For most business and clinical settings, completed years is the right answer, and INTCK('year', start, end, 'continuous') is often the cleanest implementation. When you need fractional years, use YRDIF with a documented basis. When you need a readable elapsed period, break the interval into years, months, and days using anniversary based logic. The calculator above helps you validate date pairs quickly before you convert the rule into SAS code.