user2930765
user2930765

Reputation: 11

SPSS - How to compute a variable use previous line?

The data set is like this:

V1 V2 V3 V4 V5 
x  1  2  n  .
x  3  4  .  .
x  5  6          

If I want to calculate

V5 = V4 - V3
V4 = V5(previous line) + V2

How can I do it using syntax?

Upvotes: 1

Views: 489

Answers (1)

Andy W
Andy W

Reputation: 5089

Use the LAG operator to access elements in the previous row.

DATA LIST FREE / V1 to V5.
BEGIN DATA
1 2 3 4 5
4 5 6 7 8
7 8 9 0 1
END DATA.

COMPUTE V5_2 = V4 - V3.
COMPUTE V4_2 = LAG(V5) + V2.

Upvotes: 2

Related Questions