Yrdif returns unexpected values ​​for specific date ranges?

Trying to calculate the age, I did a little search and found that yrdif was updated in 9.3 to include the handy "AGE" option.

However, using this, I noticed that when calculating the date periods from January 1st to December 31st, we get some unexpected results. Examples:

age = yrdif('01Jan1932'd,'31Dec2012'd,'Age');
put age;

      

The above gives 81 years when it should be one day less than 81 years old (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;

      

We now get the expected value (80.997222).

Bug? Is there something else going on here that I don't know about? The desired next step was to just do the gender (yrdif (dob, dod, 'AGE)) to get the age, but it looks like it won't be that easy.

+3


source to share


1 answer


In 9.3 TS1M2 and 9.4 TS1M2, I get the expected output:

1    data _null_;
2    age = yrdif('01Jan1932'd,'31Dec2012'd,'Age');
3    put age;
4    run;

80.997260274

      

This may have been a fixed bug. Searching for TS notes for this brings nothing.

In 9.3+, you can also use INTCK to calculate the age correctly if you want years to be integers.



age2= intck('YEAR','01Jan1932'd,'31Dec2012'd,'c');

      

'c'

at the end asks SAS to treat the interval as continuous, so it handles intuitive differences correctly.

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 age 3 is correct 80, and age4 is correct 81; this would have been wrong in the past (both would have been 81).

+3


source







All Articles