Computing Exact Age in Years - SAS Programming

Age in years(as of the last birthday)was computed using an approximate calculation based on the fact that an average year contains 365.25 days.While this is usually good enough,an exact method is shown next in case you need to use it someday.It also serves as another example of the INTCK function.

This calculation involves counting the number of months between two dates (e.g. date of birth and the current date) and subtracting one month if the current day of the month is less than the day of the month of the person's birthday.This calculation was originally posted on a public SAS bulletin board (SAS-L) by William Kreuter,and we present it here (with a slight modification) with his permission:

AGE=INT( (INTCK('MONTH',BIRTH,CURRENT) - (DAY(CURRENT)<DAY(BIRTH)))/12 );

BIRTH is the date of birth and CURRENT is the date of the age that you want to compute.The result is the age as of the last birthday. Note the clever use of the logical expression (DAY(CURRENT) < DAY(BIRTH)) in the equation.If the current day of the month is less than the person's birthday,the logical expression is true and you subtract 1 from the number of months.If the current day of the month is greater than the birthday day of the month,the expression is false,which means you subtract 0 from the number of months (you count the current month in the total). Dividing by 12 converts the months to years,and the INT function drops any fractional part of the year.

Let's test this with several current dates. If a person were born on October 21, 1992, and the current date is November 23, 1994,the AGE will be 2 years since INT ((25 - 0)/12) = 2.If the current date is September 15,1994, the number of months is 23.You subtract 1 since the current day of the month (15) is less than the day of the month at birth (21).The result is AGE=1.Finally, if the current date is October 3, 1994,the number of months is 24, but you subtract 1 (3 is less than 21 and the logical expression is true) to get 23. Dividing this number by 12 and than applying the INT function to the result yields the correct answer, 1.

One situation where this exact method differs from the approximate method is when you are computing ages and the current date is a person's birthday. For example, if the date of birth is 10/21/92,and you compute the age in years for a current date of 10/21/93 by the approximate method (using the INT function), the age will be computed as 0; by the exact method it will be 1 year.

The reason for the bogus number is the .25 in the divisor. In this example, the difference in days is 365.Dividing this by 365.25 yields a number less than 1, and applying the INT function to that result yields 0. Be careful.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

SAS Programming Topics