* ======================================================================= * File: age.SPS . * Date: 10-Feb-2005 . * Author: Bruce Weaver, bweaver@lakeheadu.ca . * Notes: Calculate AGE using date of birth (DOB) and another date. * ======================================================================= . DATA LIST LIST /id (f5.0) dob(date11). BEGIN DATA. 1 8-Aug-1936 2 9-Aug-1937 3 26-Jan-1957 4 18-May-1960 5 21-Aug-1963 6 12-Aug-1965 END DATA. numeric today (date11). compute today = $time. exe. var width dob today(11). * Compute AGE using CTIME.DAYS . * CTIME.DAYS yields the number of days between two date variables. * To convert days to years, divide by 365.25 (the .25 to account for leap years). compute age = ctime.days(today-dob)/365.25. exe. list all. * Unfortunately, there is a poor example of computing age in one * of the SPSS manuals. It uses the YRMODA function, as follows. COMPUTE age2 = ( YRMODA(XDATE.YEAR(today),XDATE.MONTH(today) ,XDATE.MDAY(today))-YRMODA(XDATE.YEAR(dob),XDATE.MONTH(dob) ,XDATE.MDAY(dob))) / 365.25 . EXECUTE . list all. * As you can see, it yields essentially the same results, but is far * more complicated than the CTIME.DAYS method. * ------------------------------------------------------------- . * NOTE: I just discovered the DATEDIFF function in version 13. * It allows you to compute age as follows: * COMPUTE AGE = DATEDIFF(today,dob,"days") / 365.25. * This is very similar to the CTIME.DAYS method shown above. * For backward compatibility, I would stick with CTIME.DAYS. * ------------------------------------------------------------- . * ======================================================================= .