BookmarkSubscribeRSS Feed
AlexeyS
Pyrite | Level 9

Hello, i want to ask a question about two options of yrdif function : 'ACT/ACT' vs 'AGE'

yr_act_act = yrdif(startdate, enddate, 'act/act');
yr_age = yrdif(startdate, enddate, 'AGE');

1. 'Age' options is most accurate, it takes into account for example, leap year, right?

2. What is difference between two options?

Thank you

 

4 REPLIES 4
ed_sas_member
Meteorite | Level 14

Hi @AlexeyS 

 

This is very weird indeed.

In the documentation, the calculation seems to be the same, i.e. SAS takes into account leap years.

However, the result is not exactly the same (even if the difference is slight and in the decimal part):

Capture d’écran 2020-05-17 à 11.45.58.png

SAS recommends the us of 'AGE' to calculates AGES.

ChrisNZ
Tourmaline | Level 20

The decimal difference is explained by the handling of the leap years, as the documentation states:

'ACT/ACT'  uses the actual number of days between dates in calculating the number of years. SAS calculates this value as the number of days that fall in 365-day years divided by 365 plus the number of days that fall in 366-day years divided by 366.

Year 2000 is a leap year.

 

data ONEDAY;
  YEAR_FRACTION=yrdif('01jan2000'd,'02jan2000'd,'age');      output;
  YEAR_FRACTION=1/365;                                       output;
  YEAR_FRACTION=yrdif('01jan2000'd,'02jan2000'd,'act/act');  output;
  YEAR_FRACTION=1/366;                                       output;
run;  
 
YEAR_FRACTION
.002739726
.002739726
.002732240
.002732240

 

@ed_sas_member 2020 is a leap year too. If you run your exact same test next year, using the today() function, there will be no difference.

 

 

 

 

data ONEDAY;
  YEAR_FRACTION=yrdif('01jan2000'd,'02jan2000'd,'age');      output;
  YEAR_FRACTION=1/365;                                       output;
  YEAR_FRACTION=yrdif('01jan2000'd,'02jan2000'd,'act/act');  output;
  YEAR_FRACTION=1/366;                                       output;
run;  
 
YEAR_FRACTION
.002739726
.002739726
.002732240
.002732240
FreelanceReinh
Jade | Level 19

Hello @AlexeyS,

 

The algorithm of ACT/ACT is described in Calculations That Use ACT/ACT Basis: "YRDIF=n365/365.0 + n366/366.0" with the numbers n365 and n366 of days (between start and end date) in a 365-day year or 366-day year, respectively. It should be noted that "between" means including the start date, but excluding the end date. That is, the date difference increases by 1/366 or 1/365 when the end date advances by one day, depending on whether the day before the new end date (i.e., the old end date) falls in a leap year or not.

 

The subsequent section Computing a Person’s Age is less precise, though. It appears that YRDIF(...,'AGE') also uses two different increments when the end date advances by one day: 0 (!) if the end date is a leap day (February 29th), else 1/365. That is, the result is 1/365 times the date difference reduced by the number of leap days greater than start date and less than or equal to end date.

 

As a consequence, both algorithms should produce identical results if only complete leap years, if any, are involved because a complete leap year is counted as 1 year by both algorithms. More precisely, the results should be the same

  • if neither the start date nor the end date falls in a leap year
  • or if both dates fall in a leap year and day and month of the start date are the same as day and month of the end date
  • or if the start date is January 1st of a leap year and the end date falls in a non-leap year
  • or if the end date is January 1st of a leap year and the start date falls in a non-leap year (see exclusion of the end date mentioned above).

In all other cases the date interval contains an incomplete leap year, which leads to different results (differences in both directions) because of the different increments (1/366 vs. 0 or 1/365).

 

In addition to the discrepancies due to the different algorithms we see tiny differences (like 4.44E-16) due to rounding errors. This must be considered (e.g., using the ROUND function with rounding unit 1e-9) in comparisons of results.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 3302 views
  • 6 likes
  • 5 in conversation