3

I'm trying to create a running total in SPSS. Furthermore, I need more than one. My SPSS dataset looks like this (except the running total):

  • [x] | [y] | [running total]
  • 10 | 1 | 10 (= 0 + 10)
  • 20 | 1 | 30 (= 10 + 20)
  • 30 | 2 | 30 (= 0 + 30)
  • 40 | 2 | 70 (= 30 + 40)
  • 50 | 3 | 50 ...

For each value of y (group ID) I want to create a running total. I was able to create a running total over the whole dataset, but that is not what I need. I need something like this:

For y = 1 to 500 compute x = x + lag(x)

Sadly, SPSS isn't able to execute x = x + lag(x) in a loop. I need this done in SPSS syntax (or Python), otherwise I would do it manually in Excel, even though it's a lot of data. I'm pretty frustrated and would really appreciate any help!

DerGilb
  • 33
  • 2

2 Answers2

2

You can use SPLIT FILE on your id variable, and then use CREATE to calculate the cumulative sums within IDs.

SPLIT FILE BY ID. 
CREATE /RunTotal=CSUM(X). 
SPLIT FILE OFF.
Andy W
  • 5,031
  • 3
  • 25
  • 51
2

SPLIT FILE / CREATE approach is certainly most simple to code and very intuitive and is exactly how I was achieving this up until recently when David Marso on another forum suggested another approach which worked much faster on larger datasets.

DO IF ($CASENUM= 1 OR Y<>LAG(Y)).
    COMPUTE RunTot=X.
ELSE.
    COMPUTE RunTot=SUM(X, LAG(RunTot)).
END IF. 
Jignesh Sutar
  • 2,909
  • 10
  • 13