## Calculating age from birthdate

Occasional Contributor
Posts: 12

# Calculating age from birthdate

I am having trouble calculating age from from date;

Attached is the code for someone who's birthday is 19591207

IF YEAR_TERM EQ '20144' THEN DO;             v;

BIRTHDATE = (CCYY*10000)+(MM*100)+DD;

IF MM LT 10 THEN AGE = 2014-CCYY;

IF MM EQ 10 THEN DO;

IF DD LE 10 THEN AGE = 2014-CCYY;

IF DD GT 10 THEN AGE = 2013-CCYY;

END;

IF MM GT 10 THEN AGE = 2013-CCYY;     ;

END;

Any help would be appreciated, Patty

Super User
Posts: 23,714

## Re: Calculating age from birthdate

Occasional Contributor
Posts: 12

## Re: Calculating age from birthdate

Sorry, I should have been more clear, the birth date above was an example of the format. I have over 20,000 dates.

I am getting the error messages with all the birth dates dates including the one mentioned above:

NOTE: Invalid argument to function YEAR(19591207) at line 9224 column 16.

NOTE: Invalid argument to function MONTH(19591207) at line 9225 column 14.

NOTE: Invalid argument to function DAY(19591207) at line 9226 column 14.

Posts: 4,736

## Re: Calculating age from birthdate

Functions like "year()" require a SAS Date value as input. A SAS Date value is the count of days since 1/1/1960. You need to convert your "dates" to a SAS Date value. See code below.

data test;

date_as_number=19591207;

date_as_SASDate_value=input(put(date_as_number,8.),yymmdd8.);

year_part=YEAR(date_as_SASDate_value);

age=yrdif(date_as_SASDate_value,today(),'age');

format date_as_SASDate_value date9.;

output;

stop;

run;

proc print data=test;

run;

Super User
Posts: 13,537

## Re: Calculating age from birthdate

The largest date value SAS currently will accept (at least in 9.2) is 6589335 corresponding to 31 Dec 20,000 (yes 5 digit year!)

Using 19591207 would be about the year 50,000.

Discussion stats
• 4 replies
• 371 views
• 0 likes
• 4 in conversation