- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
is it a datetime value rather than a date value then use:
age=floor(YRDIF(datepart(birth_Date), today(),"AGE"))
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
try:
age=floor(YRDIF(birth_Date, today(),"AGE"))
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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))
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Type: Numeric
Group: Date
Informat: DATETIME20.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
is it a datetime value rather than a date value then use:
age=floor(YRDIF(datepart(birth_Date), today(),"AGE"))
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Does that solution account for leap years in the age calc?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
"WARNING: Limit set by ERRORS= option reached..."
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;