Indiana University
University Information Technology Services
  
What are archived documents?
Login>>
Login

Login is for authorized groups (e.g., UITS, OVPIT, and TCC) that need access to specialized Knowledge Base documents. Otherwise, simply use the Knowledge Base without logging in.

Close

In SAS, how can I calculate age from the date-of-birth data?

In SAS, date type variables contain the number of days between January 1, 1960, and the date specified. To compute age using a date of birth and the current date, use the following code:

DATA birth; INPUT id birthday MMDDYY6.; today = DATE(); days = today - birthday; age = floor(days / 365); DATALINES; 01 122275 02 010865 03 030586 . . RUN;

The input format MMDDYY6. specifies month, day, and year in six digits. You can also use other formats, such as MMDDYY8. (e.g., 12/31/68, 12-31-68, 12.31.68, or 12311968), DDMMYY6., and DDMMYY8..

DATE() returns the current date set in the computer. To specify a particular day, use the MDY() function as follows:

today = MDY(03,31,2008);

The floor function takes the integer part of age for colloquial usage, but does not take into account the effect of leap years. To avoid this problem, use the intck function by replacing age = floor(days / 365); in the above code with:

age = floor ((intck('month',birthday,today) - (day(today) < day(birthday))) / 12);

The intck function will correct for leap years. The first part of the command, intck('month',birthday,today), returns the number of times the first day of a month is passed between birthday and today. To correct for the number of times the same day of the starting month has passed, the logical test (day(today) < day(birthday)) is used, resulting in a value of 0 or 1 to be subtracted from the initial calculation of months. Finally, the corrected number of months is divided by 12 and rounded to produce the correct age. For more details, see the SAS knowledge base document Accurately Calculating Age with Only One Line of Code.

For more about statistical and mathematical software, email the UITS Stat/Math Center, visit the center's web page, or phone 812-855-4724 (IUB) or 317-278-4740 (IUPUI). The center is located in Bloomington at 410 N. Park Avenue, and is open for consultation by appointment Monday-Friday 9am-5pm.

This is document aczw in domain all.
Last modified on February 09, 2011.

Comments/Questions/Corrections

Use this form to offer suggestions, corrections, and additions to the Knowledge Base. We welcome your input!

If you are affiliated with Indiana University and would like assistance with a specific computing problem, please use the Ask a Consultant form, or contact your campus Support Center.

Contact Information

Note: We will reply to your comment at this address. If your message concerns a problem receiving email, please enter an alternate email address.