02-07-2018 01:07 PM - edited 02-07-2018 01:19 PM
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.
02-07-2018 01:29 PM
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.
02-07-2018 01:17 PM
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))
02-07-2018 01:20 PM
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.
02-07-2018 01:24 PM
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:
Also, messages from your SAS log might be helpful here.
02-07-2018 01:50 PM
02-07-2018 01:53 PM
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
03-14-2018 02:25 PM
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;
Need further help from the community? Please ask a new question.