Example 9.13 Calculating a Person's Age

Goal

Determine a person's age by using his or her date of birth.

Example Features

Featured StepDATA step
Featured Step Options and StatementsFLOOR and INTCK functions
A Closer LookMore about Calculating Ages and Anniversaries

Input Data Set

Data set MARCH_HIRES has the names and dates of birth for 10 employees hired on March 1, 2009. Variable DOB is a SAS date value. It is displayed with the WORDDATE20. format.

                 MARCH_HIRES

Obs    name                               dob
  1    JK Allen                 March 1, 1972
  2    RT Brown             February 28, 1980
  3    WI Carter               March 29, 1980
  4    LG Johnson               March 1, 1980
  5    MH Johnson           December 12, 1980
  6    PO Phillips          February 14, 1971
  7    TR Smith              November 9, 1967
  8    AG Rodriguez              July 2, 1955
  9    EW Washington            July 30, 1960
 10    NA Young                April 22, 1984

Resulting Data Set

Output 9.13a HIREAGE Data Set

                 Example 9.13 HIREAGE Data Set

 Obs   name                             dob     hiredate      age

   1   JK Allen               March 1, 1972   03/01/2009       37
   2   RT Brown           February 28, 1980   03/01/2009       29
   3   WI Carter             March 29, 1980   03/01/2009       28
   4   LG Johnson             March 1, 1980   03/01/2009       29
   5   MH Johnson         December 12, 1980   03/01/2009       28
   6   PO Phillips        February 14, 1971   03/01/2009       38
   7   TR Smith            November 9, 1967   03/01/2009       41
   8   AG Rodriguez            July 2, 1955   03/01/2009       53
   9   EW Washington          July 30, 1960   03/01/2009       48
  10   NA Young              April 22, 1984   03/01/2009       24


Example Overview

This example computes a person's age by subtracting the SAS date value of the date of birth from another date. The following DATA step computes the age at hire for a group of employees hired on March 1, 2009.

To accurately compute the employee's age, the DATA step uses the INTCK function to count the number of months between the date of birth and the hire date. The number of months is divided by 12 to produce the number of years. An adjustment might need to be made if the day of hire is after the day of birth. The assignment statement that computes the age uses Boolean logic to return a value of 0 or 1 when testing if the day of hire is before the day of birth. If it is, a value of 1 is returned by the Boolean expression and the number of months is reduced by 1.

Last, the FLOOR function is applied to the number of years so that the age is represented as an integer. The FLOOR function returns the largest integer less than or equal to the argument.

Program

Create data set HIREAGE. Read observations from MARCH_HIRES. Define the hire date, which is the same for all employees in MARCH_HIRES.

Compute the age at hire for each employee. Count the number of months between the employee's date of birth and date of hire. If the month of hire is the same as the month of birth and the day of hire is before the day of birth, subtract 1 from the value that is returned by INTCK.

data hireage;
  set march_hires;

  retain hiredate '01mar2009'd;

  format hiredate mmddyy10.;
  age=floor((intck('month',dob,hiredate) -
       (day(hiredate) < day(dob)))/12);

run;

A Closer Look

More about Calculating Ages and Anniversaries

The preceding DATA step demonstrates that there is no simple way to compute the age of a person or the anniversary of an event. The assignment statement that computes the ages of the new hires uses three functions (FLOOR, INTCK, and DAY), a Boolean expression, and arithmetic. The structure of the Gregorian calendar requires a complicated statement because it must take into account the rules that govern the lengths of months and years. The calculation of age or anniversary must produce an integer that is incremented by 1 on the person's birthday or anniversary date.

Commonly, programmers have computed age as shown in the following assignment statement, and most of the time this statement calculates the correct age.

age=floor((hiredate-dob)/365.25);

Inaccuracies can occur associated with leap year calculations. For example, data set MARCH_HIRES does have one observation, "JK Allen", where the two methods of computing age produce different values. The following DATA step is modified below to compute AGE_NOTACC with the formula immediately above. Output 9.13b shows the different values for age for "JK Allen".

Compute AGE_NOTACC by using the common method of age calculation.

data twoages;
  set march_hires;

  retain hiredate '01mar2009'd;
  format hiredate mmddyy10.;

  age=floor((intck('month',dob,hiredate) -
       (day(hiredate) < day(dob)))/12);
  age_notacc=floor((hiredate-dob)/365.25);

run;

Output 9.13b lists the observations in data set TWOAGES.

Output 9.13b TWOAGES Data Set

                 Example 9.13 TWOAGES Data Set
                                                         age_
  Obs name                           dob   hiredate age notacc

    1 JK Allen             March 1, 1972 03/01/2009  37   36
    2 RT Brown         February 28, 1980 03/01/2009  29   29
    3 WI Carter           March 29, 1980 03/01/2009  28   28
    4 LG Johnson           March 1, 1980 03/01/2009  29   28
    5 MH Johnson       December 12, 1980 03/01/2009  28   28
    6 PO Phillips      February 14, 1971 03/01/2009  38   38
    7 TR Smith          November 9, 1967 03/01/2009  41   41
    8 AG Rodriguez          July 2, 1955 03/01/2009  53   53
    9 EW Washington        July 30, 1960 03/01/2009  48   48
   10 NA Young            April 22, 1984 03/01/2009  24   24


The INTCK function with the "MONTH" argument in the main example is used because it can count the number of months between two dates.

intck('month',dob,hiredate)

This specification returns the number of times the first day of a month is passed between the two dates that are specified as the second and third arguments to INTCK. This result will not be accurate if the day-number of HIREDATE is earlier than the day-number of DOB.

For example, using two dates close in range, the number of times the first of a month is passed between April 15, 2009, and April 4, 2011, is twenty-four times, which would return two years when 24 is divided by 12. However, when computing an age or anniversary using these two dates, you instead want the number of months to be 23 so that the division by 12 and application of the FLOOR function returns a value of 1. Therefore, when the day-number of the third argument is less than the day-number of the second argument, subtract a 1 from the number of months.

A Boolean expression is embedded in the assignment statement that tests whether the day-number of the date in the second argument is before the day-number of the third argument.

  • When the day-number of the date in the second argument is before the day-number of the third argument, the Boolean expression evaluates to 1 (true), which causes the statement to subtract 1 from the number of months that are returned by the INTCK function. The final result is then divided by 12.

  • When the day-number of the date in the second argument is after the day-number of the third argument, the Boolean expression evaluates to 0 (false). The number of months that is returned by the INTCK function is not changed and it is divided by 12 to obtain the age.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.185.155