Fluorite | Level 6

## Calculate Age from Birth Date

The date variable (BIRTH_DATE) in my dataset is formatted as DDMMMYYYY and I've trying to create an age integer variable from it like '21'. Below is my code:

``````PROC SQL;
CREATE TABLE age_pool AS SELECT * FROM test
ORDER BY ID;
QUIT;

DATA age_calc; SET age_pool;
today=DATE();
age = FLOOR((INTCK('month',BIRTH_DATE,today) - (day(today) < day(BIRTH_DATE)))/12);
run;``````

This gives me the variable 'today' filled out properly but the age column is blank.

1 ACCEPTED SOLUTION

Accepted Solutions
Tourmaline | Level 20

## Re: Calculate Age from Birth Date

is it a datetime value rather than a date value then use:

``age=floor(YRDIF(datepart(birth_Date), today(),"AGE"))``

14 REPLIES 14
Tourmaline | Level 20

## Re: Calculate Age from Birth Date

try:

age=floor(YRDIF(birth_Date, today(),"AGE"))

Fluorite | Level 6

## Re: Calculate Age from Birth Date

Still get a blank output for age. Do I need to reformat the BIRTH_DATE variable?
Super User

## Re: Calculate Age from Birth Date

@spcoman wrote:
Still get a blank output for age. Do I need to reformat the BIRTH_DATE variable?

And include the format and type for the variable birth_date.

Super User

## Re: Calculate Age from Birth Date

Today is missing the parenthesis, so SAS thinks it's a variable not a function that returns the value of todays date. Try TODAY().
PROC Star

## Re: Calculate Age from Birth Date

While there are many formulas for age, let me just comment on your code, not on any one formula or another.

You have unbalanced parentheses.  The missing closing parenthesis would go here:

(day(today) < day(BIRTH_DATE))

Fluorite | Level 6

## Re: Calculate Age from Birth Date

Yes sorry, I accidentally deleted it when I copied it over and had to rename one of the variables for privacy. I correct the parentheses, this does not resolve the issue when I run the code.

PROC Star

## Re: Calculate Age from Birth Date

OK, given that correction, the next issue is the contents of BIRTH_DATE.  Is it actually a SAS date, or is it a character string in DDMMMYYYY form?  PROC CONTENTS will tell you.

If it's a character string, the DATA step references (not the SQL code) to BIRTH_DATE should be replaced by:

input(birth_date, date9.)

Fluorite | Level 6

## Re: Calculate Age from Birth Date

That variable is

Type: Numeric
Group: Date
Informat: DATETIME20.
Tourmaline | Level 20

## Re: Calculate Age from Birth Date

is it a datetime value rather than a date value then use:

``age=floor(YRDIF(datepart(birth_Date), today(),"AGE"))``

Fluorite | Level 6

## Re: Calculate Age from Birth Date

That worked. Just so I understand, the issue is that it was a datetime format instead of a date format?

Does that solution account for leap years in the age calc?
Opal | Level 21

## Re: Calculate Age from Birth Date

Yes, yrdif accounts for leap years! It takes the approach that one born on Feb 29 of a leap year ages another year on Feb 28 of a non-leap year.

Art, CEO, AnalystFinder.com

Fluorite | Level 6

## Re: Calculate Age from Birth Date

When I run that code I get the following error,

"WARNING: Limit set by ERRORS= option reached..."
Fluorite | Level 6

## Re: Calculate Age from Birth Date

Thank you!
Fluorite | Level 6

## Re: Calculate Age from Birth Date

I used the INTCK method for years.

AGE=INT((INTCK('MONTH',DOB,refDate) - (DAY(refDate)<DAY(DOB)))/12);

I stopped using it when I discovered that the calculation would sometimes produce odd results. Especially when trying to find newborns where age is less than 1.

I use the YRDIF method  now.

AGE = INT(yrdif(DOB,refDate,'actual'));

Working with dates is always tricky and you should always check your results.

With some recent data, I was using the YRDIF method and ran into some special cases where I was calculating ages on some claims data for children across three different years. The precision of the MemberDateOfBirth (a stored SAS date variable) was causing the age15 variable to resolve to 18 with no decimal places for children born 31Dec1996 using 31Dec2015 as the reference year. The reference to the variable "age15b" below was because when I tested the YRDIF method for age15 below without the INT function, the age resolved to 19.000.

This only occurred for the children born 31Dec1996 using 31Dec2015 as a reference date. The same issue didn't happen to any other Dec 31st children for any of the other years in my data.

yearDOB = year(MemberDateOfBirth);

age15 = INT(yrdif(MemberDateOfBirth,'31Dec2015'd,'actual'));

** fix special case where only for reference year 2015, kids born on
Dec 31 are getting odd ages of 19.000 for age15 and 18 for age15b.
This only affects 2015 reference year.;

if age15 eq 18 and MemberDateOfBirth eq '31DEC1996'd then do;
age15 = 19;
end;

Discussion stats
• 14 replies
• 53693 views
• 2 likes
• 6 in conversation