DATA Step, Macro, Functions and more

Calculate Age from Birth Date

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

Calculate Age from Birth Date

[ Edited ]

I've seen some other threads about this but haven't been able to get my code to work based on those.

 

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. 


Accepted Solutions
Solution
‎02-07-2018 01:50 PM
Super User
Posts: 2,068

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"))

 

 

View solution in original post


All Replies
Super User
Posts: 2,068

Re: Calculate Age from Birth Date

try:

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

Occasional Contributor
Posts: 14

Re: Calculate Age from Birth Date

Posted in reply to novinosrin
Still get a blank output for age. Do I need to reformat the BIRTH_DATE variable?
Super User
Posts: 24,026

Re: Calculate Age from Birth Date


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

Post your code and log.

And include the format and type for the variable birth_date.

 

Super User
Posts: 24,026

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().
Super User
Posts: 6,935

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))

Occasional Contributor
Posts: 14

Re: Calculate Age from Birth Date

Posted in reply to Astounding

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.

Super User
Posts: 6,935

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.)

 

Also, messages from your SAS log might be helpful here.

Occasional Contributor
Posts: 14

Re: Calculate Age from Birth Date

Posted in reply to Astounding
That variable is

Type: Numeric
Group: Date
Informat: DATETIME20.
Solution
‎02-07-2018 01:50 PM
Super User
Posts: 2,068

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"))

 

 

Occasional Contributor
Posts: 14

Re: Calculate Age from Birth Date

Posted in reply to novinosrin
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?
Super User
Posts: 8,220

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

 

Occasional Contributor
Posts: 14

Re: Calculate Age from Birth Date

Posted in reply to Astounding
When I run that code I get the following error,

"WARNING: Limit set by ERRORS= option reached..."
Occasional Contributor
Posts: 14

Re: Calculate Age from Birth Date

Thank you!
Occasional Contributor
Posts: 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;

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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