Date: Mon, 13 Nov 2006 14:20:43 +0800
Reply-To: Winson Yeung <winson.yeung@spss.com.hk>
Sender: "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From: Winson Yeung <winson.yeung@spss.com.hk>
Subject: Re: Question on looping syntax
In-Reply-To: <7.0.1.0.2.20061113000849.047b9510@mindspring.com>
Content-Type: text/plain; charset="us-ascii"
Thanks Richard. DO REPEAT syntax does work in my case.
Thanks & Regards,
Winson
-----Original Message-----
From: SPSSX(r) Discussion [mailto:SPSSX-L@LISTSERV.UGA.EDU]On Behalf Of Richard Ristow
Sent: Monday, November 13, 2006 1:47 PM
To: SPSSX-L@LISTSERV.UGA.EDU
Subject: Re: Question on looping syntax
OK, late at night ...
At 08:34 PM 11/12/2006, Winson Yeung wrote:
>I would like to calculate the formula
>
>DV100 = DAILYVOL + LAG(DAILYVOL,1) + LAG(DAILYVOL,2) + . . . +
>LAG(DAILYVOL,100)
>
>as follow:
>
>COMPUTE DV100 = DAILYVOL.
>LOOP #I = 1 TO 100.
>. COMPUTE DV100 = DV100 + LAG(DAILYVOL,#I).
>END LOOP.
>EXECUTE.
>
>However, the above syntax doesn't work. Does anyone know how to fix
>it?
Well, to start with, why yours can't work:
>>LAG(arg,n) The value of the variable n cases before. The first
>>argument is a variable. The second argument, if specified, IS A
>>CONSTANT and must be a positive integer; the default is 1.
>>-SPSS 14 Command Syntax Reference, p.60
Emphasis added. Sorry; but you *are* writing from an SPSS.COM address.
But DO REPEAT syntax that looks almost identical, >does< work. That's
because DO REPEAT is a macro-like facility: it 'unrolls' its loop,
generating separate code for each loop pass, and the 'loop index'
LAGVAL expands as a constant in the code for each single pass. The
following is tested - SPSS draft output, and for a running sum of 5
lags, not 100:
* ................................................... .
LIST.
|-----------------------------|---------------------------|
|Output Created |13-NOV-2006 00:39:40 |
|-----------------------------|---------------------------|
ID DATUM
001 .1
002 .3
003 .5
004 .7
005 .9
006 1.1
007 1.3
008 1.5
009 1.7
010 1.9
011 2.1
012 2.3
013 2.5
014 2.7
015 2.9
016 3.1
017 3.3
018 3.5
019 3.7
020 3.9
Number of cases read: 20 Number of cases listed: 20
* Compute running sum of the previous 5 values, counting the .
* current value. .
NUMERIC RNINGSUM (F6.1).
COMPUTE RNINGSUM = DATUM.
DO REPEAT LAGVAL = 1 TO 4.
. COMPUTE RNINGSUM = RNINGSUM + LAG(DATUM,LAGVAL).
END REPEAT.
LIST.
|-----------------------------|---------------------------|
|Output Created |13-NOV-2006 00:39:41 |
|-----------------------------|---------------------------|
ID DATUM RNINGSUM
001 .1 .
002 .3 .
003 .5 .
004 .7 .
005 .9 2.5
006 1.1 3.5
007 1.3 4.5
008 1.5 5.5
009 1.7 6.5
010 1.9 7.5
011 2.1 8.5
012 2.3 9.5
013 2.5 10.5
014 2.7 11.5
015 2.9 12.5
016 3.1 13.5
017 3.3 14.5
018 3.5 15.5
019 3.7 16.5
020 3.9 17.5
Number of cases read: 20 Number of cases listed: 20