By Ruben Geert van den Berg on May 29, 2013 under 3.2. SPSS Date and Time Variables.

# 3.2.5. Compute Age

## Summary

In some cases, one would like to calculate respondents' age given their birthday. As long as birthday is an actual date variable, age is readily calculated by using the DATEDIFF function.

Copy-paste-run the syntax below for a demonstration of `DATEDIFF`

.

## SPSS Datediff Syntax Example

***1. Create test data.**

data list free/bday(a10).

begin data

15-09-1978

15-11-1990

11-10-1976

end data.

***2. Convert birthday into date variable.**

alter type bday(edate10).

***3. Compute current age.**

compute age_years=datediff($time,bday,"years").

compute age_months=mod(datediff($time,bday,"months"),12).

exe.

## Explanation

- Somebody's age is obviously the current date minus his birthday. In SPSS, the system variable
`$time`

holds the current date and dates can be subtracted using`DATEDIFF`

. - In the latter, a time unit (such as year) can be specified. Note that the result is always truncated.
- Since the example presented here focusses on readability, only years and months were calculated. If age is to be used in any calculations, it may be a better idea to use a fixed date instead of
`$time`

and smaller time units (for example just "days").