# Calculate the Difference Between Two Dates in SPSS

In this quick tutorial, we’re going to look at how to compute the difference between two dates in SPSS.

This is something you might want to do, for example, if you’ve got a pretest/posttest design, involving a therapeutic intervention at the pretest stage, and you want to see whether the length of time between the therapy and posttest measurement makes a difference to the treatment outcome.

Consequently, we’re going to use data from a hypothetical study that looks at the effect of a new treatment for asthma by measuring the peak flow of a group of asthma patients immediately before and then sometime after treatment. Our task will be to compute the difference between the date of the before treatment peak flow measure and the date of the after treatment measure.

## The Data

This is our pretend data as it appears in the Data View of SPSS.

The variables PrePEF and PostPEF comprise the pretest and posttest peak expiratory flow measurements. The date of each pretest measurement can be found in the PrePEFDate variable (highlighted), and the date of each posttest measurement in the PostPEFDate variable (highlighted).

At this point it is worth noting that the data is in the dd.mm.yy format (so, for example, 07.08.19 is the 7th August 2019).

We want to compute how many days there are between the pretest and posttest measurements for each subject, and we want to record this data in a new variable.

This is how it’s done using the Compute Variable dialog box.

## Compute the Difference Between Two Dates

To begin, click Transform -> Compute Variable, which will bring up the Compute Variable dialog box.

Here are the things you’ve got to do to set up this dialog box so that it will compute the difference in days between two dates, and save the results to a new variable.

First, type the name of your new variable into the Target Variable box. As you can see above, in our example the new variable is going to be called “DifferenceInDays”.

Second, select Date Arithmetic in Function group on the right of the dialog. This will allow you to select Datediff in the Functions and Special Variables box. This is the function that does the heavy lifting when calculating the difference in time between two dates.

Third, you’ve got to drag the Datediff function into the Numeric Expression box (as per the red arrow above).

The fourth stage is a little more involved.

### The DATEDIFF Function

If you look in the box beneath the keypad (see above), you’ll see the syntax for the Datediff function is specified as follows:

DATEDIFF(datetime2, datetime1, “unit”).

This tells you how you need to set up the function in the Numeric Expression box.

Currently, the function looks like this DATEDIF(?,?,?).

You’ve got to replace the question marks with datetime2, datetime1, and the unit of time in which you want the difference between the two dates to be measured (the options are years, quarters, months, weeks, days, hours, minutes, seconds, and you’ve got to surround the option you choose in quotes).

In our example, we want the expression to look like DATEDIFF(PostTestDate, PreTestDate, “days”). So we’ve got the date of our posttest in the datetime2 slot, the date of our pretest in the datetime1 slot, and we want the result to be in days.

A few things to note here: (a) datetime2 & datetime1 have to be date or time format variables; (b) pay attention to their order – if you have earlier dates in the datetime1 position, then you’re going to get negative difference numbers, for example, minus 17 days; (c) you can drag and drop your date time variables into the Numeric Expression box, but be sure to delete the question marks if you do (alternatively you can just type over them).

Once you’re done you want the dialog box to look something like this.

Right, that’s it for the set up. Check everything looks good, and then hit the OK button. SPSS will create a new variable called DifferenceInDays, and fill it with the new data.

## The Result

Here you can see the final result.

The new DifferenceInDays variable (highlighted, above) shows the difference in days between the pretest date and the posttest date. You can now run calculations on this variable.

***************

Okay, that’s job done. You should now be able to use the Compute Variable dialog box to compute the difference between two dates in SPSS.