Handy-Dandy SAS® Functions, Part 1 of 3: Calculating Age in Months

This Tech Tip was provided by Onecia Gibson, Research and Analysis Director Assistant, Office of Institutional Research, University of Kentucky.

Data manipulation is a powerful component of SAS®.  However, the wide variety of functions and techniques can sometimes overwhelm even the most experienced programmers.  In a series of three tech tips, I will present data manipulation functions and techniques that I have found valuable.

This first technique calculates a person’s age in completed months based upon date of birth (DOB) and a specific date of interest. This is a common practice in IR required for IPEDS, CDS, US News, NSF surveys, etc. Suppose you need to know a student’s age at the time of graduation.  Specifically, suppose the student’s DOB was November 26, 1987, and the student graduated on May 8, 2010. Using the SAS® code in the screen shots below, the intck, mdy, and day functions are used to find the student’s age in completed months to be 22.42 years.  



To understand age calculation more completely, let’s consider the formula in four steps.


In the SAS® code above, the following statements are used:

  • Step 1 uses the intck function to return the number of months from the student’s DOB (11/26/1987) and the graduation date (5/8/2010). 

  • Step 2 is a logical test between the day of graduation (8th) and the day of the DOB (26th). Step 2 returns the value 1 when the day of the graduation is less than the day of the DOB and 0 otherwise.  The reason Step 2 is needed is because the day of the student’s DOB (26th) exceeds the day of the graduation (8th), therefore the student has not yet completed another full month of age. 

  • Step 3 subtracts one or zero from the value produced by Step 1 (due to the reason stated in Step 2). 

  • Step 4 divides the number of months completed by 12, giving age in years based upon number of completed months.

Reference: Sample 24808: Calculating Age with Only One Line of Code. (2012). Retrieved April 16, 2012, from the SAS website.


To add a comment, Sign In
Total Comments: 3
Gary posted on 6/29/2012 4:53 PM
Thanks for the thought-provoking tip! If you want to report age in years, wouldn't a simple rounded-down date difference calc be sufficient (provided that one is working with time as periods, and not durations)? If you wanted to include fractions of years (months) to improve accuracy, why not also include number of days in the not-completed month? I'd be interested in reading you comments on this. Below is my replication using R. It was a useful exercise - thank you!

as.period(as.interval(ymd("2010-5-8") - ymd("1987-11-26"), start=ymd("1987-11-26")))

#OUTPUT: 22 years, 5 months and 12 days
Onecia posted on 7/3/2012 9:14 AM
Thank you for the comments. For my purposes, calculating the fractional number of years based upon numbers of months was sufficient. However, if it were necessary to calculate fractional number of years based upon number of days, I believe the following code would work. My understanding is that the intck function has many options for the interval argument (days, weeks, months, etc.), so the user has the flexibility to choose the precision needed.
data example2;
Meghal posted on 9/5/2014 11:24 AM
So in our database systems, the student term gets saved in a text character format as "201408" and students birthdate gets saved as "19850111" (text format again)
Can someone please help me use this function to make age calculations similar to one shown above? Thanks!