In the world of data analytics and clinical trials, knowing how to calculate age in SAS accurately is a fundamental skill. Whether you are dealing with patient demographics or insurance actuarial tables, the method you choose matters for precision.
SAS Age Simulation Tool
Enter dates below to see how SAS would calculate the age using the YRDIF function.
Understanding the YRDIF Function
The most common and recommended way to calculate age in SAS is by using the YRDIF function. This function computes the difference in years between two SAS date values.
The syntax for YRDIF is:
Age = YRDIF(start_date, end_date, 'basis');
Why the 'AGE' Basis Matters
When calculating age for human beings, you should almost always use the 'AGE' basis. This specific basis tells SAS to account for leap years correctly, ensuring that a person only turns a year older on their actual birthday.
- 'AGE': Calculates age based on the actual number of days, correctly handling Feb 29th.
- 'ACT/ACT': Actual/Actual basis, often used in financial calculations.
- '30/360': Assumes 30 days per month and 360 days per year.
Alternative: Using the INTCK Function
Another approach is the INTCK function, which counts the number of interval boundaries between two dates. However, using INTCK('YEAR', birth, ref) can be misleading because it simply looks at the year change, not the specific day and month.
To get the accurate age with INTCK, you would often use the 'continuous' method or adjust for the month/day:
data _null_;
birth = '15MAY1995'd;
today = '14MAY2023'd;
/* This returns 28, which is correct */
age = floor(yrdif(birth, today, 'AGE'));
put age=;
run;
Handling Leap Year Birthdays
One of the trickiest parts of the "calculate age sas" logic is handling individuals born on February 29th. Using yrdif(..., 'AGE') automatically handles this by treating the birthday as occurring on February 28th in non-leap years, which is the standard legal convention in many jurisdictions.
Summary Checklist for SAS Programmers
- Ensure your dates are in SAS date format (numeric).
- Use
YRDIFfor decimal precision if needed. - Use
FLOOR()around your YRDIF function to get the integer age. - Always specify
'AGE'as the third argument for demographic data.