Interview Questions

SAS* Dates

Computing Age

Suppose you have a SAS data set EMPLOYEE which contains variables ID, DOB (date of birth),and several other variables. One requirement is to compute AGE from a date of birth. You have several choices to make.You can round the age to the nearest year or drop all fractional parts and take the age as of the last birthday.

You also need to consider as of what date you want the age computed.The example that follows is technically an approximate value for age,using the fact that an "average" year has 365.25 days (1leap year every 4 years). More sophisticated programming is needed to compute exact ages. First, let's look at ways to compute ages using the approximate method. Here is a program to compute a person's age several different ways:

Example 

DATA AGE;
SET EMPLOYEE;
AGE1=INT (('01JAN95'D-DOB)/365.25);
AGE2=ROUND (('01JAN95'D-DOB)/365,25,1);
AGE3=INT ((TODAY()-DOB)/365.25);
AGE4=ROUND ((TODAY()-DOB)/365.25,1);
RUN;

AGE1 and AGE2 represent the age as of January 1,1995,with AGE2 rounded to the nearest year.These assignment statements use a date literal which is of the form: 'DDMMMYY'D DD is a two-digit day of the month, MMM is a month abbreviation, and YY is a two- or fourdigit year (it would be'DDMMYYYY'D in the latter case).

This string is placed in single or double quotation marks,followed by an upper-or lower-case D. SAS software will convert this literal into a SAS date (i.e. the number of days since January 1, 1960).Variables AGE3 and AGE4 are ages computed as of the current date which is represented by the TODAY() function.

The null parentheses () following the function name are required by SAS System syntax; all functions, even ones without any arguments, need parentheses so that the software can distinguish variable names from functions names.(Question: What did the TODAY function say to the MEAN function? Answer: "Don't give me any arguments!") Finally, AGE1 and AGE3 compute age as of the last birthday—the INT function truncates (throws away) the decimal part of the number.

AGE2 and AGE4 use the ROUND function to compute age to the nearest year.These functions are described in more detail in Example 3 in Chapter 5, "SAS Functions." We demonstrate an exact method to compute age later in this chapter.


Pragna Meter
Next Chapter  
e-University Search
Related Jobs