Monday, January 9, 2017

Hack 4.7 Increasing the Accuracy of Age Calculations with the YRDIF Function


SAS Programming Professionals,

Did you know that the YRDIF function can increase the accuracy of your age calculations?

The YRDIF function returns the number of years between two dates.  So, it can be useful in cases where you are looking for age, years in a study, duration of dosage, etc.  The format of the YRDIF function is:

               YRDIF(start-date, end-date, basis)

…where basis describes how SAS should calculate the difference between dates.  The most common basis is “ACT/ACT”, which specifies that SAS is to calculate YRDIF 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.  Here is an example:

data mybirthday;

     my_age_in_years = yrdif("27APR1975"d,today(),"ACT/ACT");

     label my_age_in_years = "My Current Age";

run;

proc print noobs label data=mybirthday;
run;

In the example, we determine the difference between my birth date and today’s date.  I entered a date literal for start-date and the TODAY() function for end- date, but I could have easily used two variables containing date values, instead.  The SAS listing looks like this:

My Current Age

   39.1534

Of course the year calculation is only as accurate as the information you feed into the YRDIF function.  If you are curious about other possible values for basis, look up the YRDIF function in the SAS Online Documentation on support.sas.com.

Best of luck in all your SAS endeavors!

---MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)
Author of the new cult classic for computer programmers:  It Only Hurts When I Hit <ENTER>
Print edition:  http://tinyurl.com/z8bzx2e 
Kindle edition: http://tinyurl.com/zypgqa7 

The hack above is an excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers

No comments:

Post a Comment