1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
* The LAG function works only with a constant. 
* The following syntax produce the lagged values which equal LAG(y,lgx) where lgx is a variable whose value varies for each case.
* Raynald Levesque rlevesque@videotron.ca (solution no 1 was posted to usenet on 2000/06/26).

DATA LIST FREE / LGX Y.
BEGIN DATA
1 5  2 6  1 4  3 3  1 2  3 5  2 3  1 3  3 2  2 2  3 4  2 5  1 6  3 7  2 5  1 4
3 3  2 3  1 6  3 7  2 5  3 3  2 5  1 3  3 2  2 1  3 6  2 8  2 7  2 5  2 3  2 9
END DATA.
SAVE OUTFILE='c:\\temp\\mydata.sav'.

**********************.
*** SOLUTION no 1: this assumes we DO know the max value of lgx.
**********************.
DO REPEAT cnt=1 TO 20.
IF lgx=cnt lagg=lag(y,cnt).
END REPEAT.
EXE.

**********************.
*** SOLUTION no 2: this is a solution when we DO NOT know the min and max values of lgx. 
**********************.
GET FILE='c:\\temp\\mydata.sav'.
COMPUTE dummy=1.
AGGREGATE
  /OUTFILE=*
  /BREAK=dummy
  /lagmin = MIN(lgx) /lagmax = MAX(lgx).

WRITE OUTFILE='c:\\temp\\temp.sps' /'DO REPEAT cnt='lagmin' TO 'lagmax'.'
	/'IF lgx=cnt lagg=lag(y,cnt).'
	/'END REPEAT.'.
EXECUTE.
GET FILE='c:\\temp\\mydata.sav'.
INCLUDE FILE='c:\\temp\\temp.sps'.
EXECUTE.