1
votes

I am completely new in SPSS and now trying to write syntax to manipulate my dataset.

I now have a dataset which looks like this:

V1 V2

1 2007/01/23

2 2007/02/25

. . . .

First row "V1" is the row number and second row "V2" is the date.

I would like to find the difference between the dates "V2" for each 2 subsequent rows and create a new third column of data for the result.

However, I have no idea how to do the subtraction for data in two different rows.

2
are the values of v1 sequential, i.e. can you subtract odd rows from even ones ? - Ian Kenney

2 Answers

3
votes

Look up the lag function. It gives you access to previous rows. If you need to calculate against forward values look at the SHIFT VALUES command.

1
votes

I assume that you are interested in the number of days that lie between two dates. The "ctime.days" function converts the value of the subtraction into the number of days.

COMPUTE ddiff = ctime.days(v2 - LAG(v2)).
EXECUTE.

Another solution would be using the "datediff" function: The general syntax of this function is:

datediff(date1,date2,unit)

In your case you would use the following syntax:

COMPUTE ddiff = datediff(v2,LAG(v2),"days").
EXECUTE.

The second solution is a bit more flexible, as you can choose other output units such as "years", "quarters", "months", "weeks" and so on.