BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
spcoman
Fluorite | Level 6

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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

14 REPLIES 14
novinosrin
Tourmaline | Level 20

try:

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

spcoman
Fluorite | Level 6
Still get a blank output for age. Do I need to reformat the BIRTH_DATE variable?
Reeza
Super User

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

 

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

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

spcoman
Fluorite | Level 6

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.

Astounding
PROC Star

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.

spcoman
Fluorite | Level 6
That variable is

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

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

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

 

 

spcoman
Fluorite | Level 6
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?
art297
Opal | Level 21

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

 

spcoman
Fluorite | Level 6
When I run that code I get the following error,

"WARNING: Limit set by ERRORS= option reached..."
spcoman
Fluorite | Level 6
Thank you!
Stephen
Fluorite | Level 6

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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