I have a variable A and I would like to subtract all the 1001 elements of this variable as follows: B[n] = A[n+1] - A[n]
using spss syntax.
My code:
compute ctord = $casenum .
vector tot(1001).
vector B(1001).
compute tot(ctord) = A.
execute.
loop #i = 1 to 1001.
compute B(#i)=tot(#i+1)-tot(#i).
end loop.
But this code is not working in the loop.
For creating the Variable B[n] = A[n+1] - A[n]
, where n
is your case number, you first need to compute A[n+1]
as a new variable:
CREATE
/A_lead = LEAD(A,1).
Then you can compute B easily with:
COMPUTE B = A_lead - A.
EXECUTE.
After that, you can delete A_lead if you want to.
DELETE VARIABLES a_lead.