Susan Slaughter

YRDIF and Me: A Tale of Woe

In Everything, Little SAS Book Series, SAS, SAS Papers on June 29, 2010 at 12:37 pm


Since I originally wrote this blog post, a new option has been added for the YRDIF function.  The AGE option is not the easy-to-use option that I dreamed of.  However, it does compute accurate ages and that is a huge improvement!  I am now happy to recommend this method for computing current age in integer years:

CurrentAge = INT( YRDIF(BirthDate, TODAY(), ‘AGE’));

However, you should be aware that there are still situations where an alternate method may be better than YRDIF.  You should define exactly what your requirements are, and test different methods (such as INTCK) to see which method best meets your needs.

The original blog post:

In all the long history of The Little SAS Book: A Primer (four editions, five if you count the Revised Second Edition) we have removed only three topics from the book.   We dropped PROC COPY and PROC PLOT (that’s PLOT, not GPLOT or SGPLOT) because they had become largely obsolete.  Now we have removed the YRDIF function, but this situation is different. For the first time ever, we have removed a topic because it doesn’t work.

The YRDIF function has been removed from The Little SAS Book: A Primer, Fourth Edition, starting with the third printing, because it does not compute accurate ages. (YRDIF never appeared in earlier editions.)  In defense of YRDIF I have to point out that it does work the way it was designed to work; but YRDIF, it turns out, was never designed to compute ages.  Specifically, ages computed using the YRDIF function may be incorrect if the starting or ending date falls in a leap year, and the ending date is the person’s birthday. Of course, this situation doesn’t happen all that oftenbut it does happen. So, if you need really accurate ages, then you can’t use YRDIF.

We considered substituting some other method for computing ages (such as INTCK), but it turns out that not only are those methods too complicated to squeeze into the tiny space we had, they also come with their own limitations and problems.  We were forced to conclude that currently there is no single definitive method for computing ages in SAS.  If you need to compute ages in SAS, the best you can do is to choose the one that has the fewest problems for your particular situation.  After much debate, Lora Delwiche and I felt that we had no choice but to simply remove YRDIF from our book.

As a result of all this, we have written a paper, “Computing Ages in SAS: The Removal of the YRDIF Function from The Little SAS Book: A Primer, Fourth Edition.”  This paper explains how YRDIF really works, and then discusses alternate methods for computing ages. You can download this paper by clicking here.

  1. YrDif with the ‘AGE’ parameter is nice, but the INTCHK method is still more accurate for persons born on a February 29th. For persons born on a February 29th, YrDif will show them as being one year older than they actually are on February 28th of each year whereas the INTCK method will show the correct age.

    A quick run comparing YrDif with INTCHK for persons born in 1960, 1964, and 1968 on a February 29th using years 2000 – 2016 as the comparison year shows that YrDif shows the incorrect age every February 28th.


    Obs birthDate asOfDate SASAge correctAge Year_Type
    1 29FEB1960 28FEB2000 40 39 Leap Year
    2 29FEB1960 28FEB2001 41 40
    3 29FEB1960 28FEB2002 42 41
    4 29FEB1960 28FEB2003 43 42
    5 29FEB1960 28FEB2004 44 43 Leap Year
    6 29FEB1960 28FEB2005 45 44
    7 29FEB1960 28FEB2006 46 45
    8 29FEB1960 28FEB2007 47 46
    9 29FEB1960 28FEB2008 48 47 Leap Year
    10 29FEB1960 28FEB2009 49 48
    11 29FEB1960 28FEB2010 50 49
    12 29FEB1960 28FEB2011 51 50
    13 29FEB1960 28FEB2012 52 51 Leap Year
    14 29FEB1960 28FEB2013 53 52
    15 29FEB1960 28FEB2014 54 53
    16 29FEB1960 28FEB2015 55 54
    17 29FEB1960 28FEB2016 56 55 Leap Year
    18 29FEB1964 28FEB2000 36 35 Leap Year
    19 29FEB1964 28FEB2001 37 36
    20 29FEB1964 28FEB2002 38 37
    21 29FEB1964 28FEB2003 39 38
    22 29FEB1964 28FEB2004 40 39 Leap Year
    23 29FEB1964 28FEB2005 41 40
    24 29FEB1964 28FEB2006 42 41
    25 29FEB1964 28FEB2007 43 42
    26 29FEB1964 28FEB2008 44 43 Leap Year
    27 29FEB1964 28FEB2009 45 44
    28 29FEB1964 28FEB2010 46 45
    29 29FEB1964 28FEB2011 47 46
    30 29FEB1964 28FEB2012 48 47 Leap Year
    31 29FEB1964 28FEB2013 49 48
    32 29FEB1964 28FEB2014 50 49
    33 29FEB1964 28FEB2015 51 50
    34 29FEB1964 28FEB2016 52 51 Leap Year
    35 29FEB1968 28FEB2000 32 31 Leap Year
    36 29FEB1968 28FEB2001 33 32
    37 29FEB1968 28FEB2002 34 33
    38 29FEB1968 28FEB2003 35 34
    39 29FEB1968 28FEB2004 36 35 Leap Year
    40 29FEB1968 28FEB2005 37 36
    41 29FEB1968 28FEB2006 38 37
    42 29FEB1968 28FEB2007 39 38
    43 29FEB1968 28FEB2008 40 39 Leap Year
    44 29FEB1968 28FEB2009 41 40
    45 29FEB1968 28FEB2010 42 41
    46 29FEB1968 28FEB2011 43 42
    47 29FEB1968 28FEB2012 44 43 Leap Year
    48 29FEB1968 28FEB2013 45 44
    49 29FEB1968 28FEB2014 46 45
    50 29FEB1968 28FEB2015 47 46
    51 29FEB1968 28FEB2016 48 47 Leap Year

    • Jim,

      Thank you for your comment! Yes, computing ages continues to be fraught with challenges. To make matters worse, I recently learned (thank you to Eric Tilanus!) that different countries handle the ages of people born on February 29 differently. In some countries, if you are born on Leap Day, then in non-leap years you are considered to be one year older on February 28. In other countries, you must wait until March 1 to be one year older. This is not an idle debate, but a legal matter. The good news is that SAS can handle any of these situations. So if this kind of accuracy is important to you, then please take the time to find the method that will give you exactly what you need.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: