Attempting to calculate age, I did a bit of googling and discovered that yrdif was updated in 9.3 to include a handy-dandy 'AGE' option.
However, in using it, I noticed that when calculating date spans ranging from Jan 1st to Dec 31st, we get some unexpected results. Examples:
age = yrdif('01Jan1932'd,'31Dec2012'd,'Age');
put age;
The above yields 81 years, when it should be one day less than 81 years (80.9972222). But more surprising is the result when we increment the dates by one day:
age = yrdif('02Jan1932'd,'01Jan2013'd,'AGE');
put age;
Now we get the expected value (80.997222).
Bug? Something else going on here that I'm not aware of? Desired next step was to simply do floor(yrdif(dob,dod,'AGE')) to get age, but it seems like it will not be quite so easy.
In 9.3 TS1M2 and 9.4 TS1M2 I get the expected result:
1 data _null_;
2 age = yrdif('01Jan1932'd,'31Dec2012'd,'Age');
3 put age;
4 run;
80.997260274
Perhaps it was a fixed bug. Searching TS notes for that doesn't come up with anything.
In 9.3+ you can also use INTCK to correctly calculate age if you want the years as an integer.
age2= intck('YEAR','01Jan1932'd,'31Dec2012'd,'c');
The 'c' at the end asks SAS to consider the interval continuous, so it correctly handles intrayear differences.
data _null_;
age = yrdif('01Jan1932'd,'02Jan2013'd,'Age');
age2= intck('YEAR','01Jan1932'd,'31Dec2012'd,'c');
age3= intck('YEAR','01Mar1932'd,'01Jan2013'd,'c');
age4= intck('YEAR','01Mar1932'd,'01Apr2013'd,'c');
put age= age2= age3= age4=;
run;
So here, age3 is correctly 80 while age4 is correctly 81; in the past this would've been incorrect (both would be 81).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With