Search code examples
spssdatediff

Calculate age from 2 dates on SPSS - reference issue


I am trying to calculate the age of some participants based of the date of birth (currently named 'age') and date of evaluation (currently name 'date')

I wrote this script, which seem to be the right one, but when I run it, the data cells remain empty.

Compute age=datediff(date,age,'years').

If I write the following, the new variable created has the correct values, which make me think the formula is right, but since theres a reference to the old variable in the formula it might create an error.

Compute age2=datediff(date,age,'years').

Therefore I was wondering why the first script didn't work and if there is a way to make it work.


Solution

  • I assume variables date and age are date variables. When you calculate the difference between them in a new variable, it is automatically a numeric variable, which goes well with the integer value of the function. But when you calculate it into an existing date variable, you get a missing value because a two digit integer has no meaning in a date variable.

    If you insist on having variable age contain the result of the calculation, do this:

    rename vars age=born.
    Compute age=datediff(date,born,'years').