```Date: Mon, 13 Nov 2006 12:13:14 -0500 Reply-To: Richard Ristow Sender: "SPSSX(r) Discussion" From: Richard Ristow Subject: Re: Question on looping syntax Comments: cc: hillel In-Reply-To: <45585628.7050000@bgu.ac.il> Content-Type: text/plain; charset="us-ascii"; format=flowed At 06:25 AM 11/13/2006, hillel wrote: >The same calculation can be done with no need of loop. > >Here is the syntax for it as will as Richard Ristow do reapeat example >. > >compute #sum1=sum(#sum1,num, - lag(num,6)) . >compute sum1 = #sum1. Right. That's the other way to do it: keep a running running sum, your "#sum1". I think you want "lag(num,5)" instead of "lag(num,6)", though. Remember, the running sum of 5 is of the current value through 4 back, not through 5 back. Your logic treats all missing values in the sequence as 0; that may or may not be what's wanted. In particular, before the 5th case, you add all of the previous cases; those results may be what is desired, but could be seriously misleading. The DO REPEAT logic could be written either way; I've written it to treat missing values as missing. SPSS draft output: * ................................................... . LIST. |-----------------------------|---------------------------| |Output Created |13-NOV-2006 12:03:21 | |-----------------------------|---------------------------| ID DATUM 001 .1 002 .3 003 .5 004 .7 005 .9 006 1.1 007 1.3 008 1.5 009 . 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. * The same, with "running running sum" logic: . NUMERIC SUM1 (F6.1). NUMERIC #SUM1 (F6.1). compute #sum1=sum(#sum1,DATUM, - lag(DATUM,5)) . compute sum1 = #sum1. LIST. |-----------------------------|---------------------------| |Output Created |13-NOV-2006 12:03:22 | |-----------------------------|---------------------------| ID DATUM RNINGSUM SUM1 001 .1 . .1 002 .3 . .4 003 .5 . .9 004 .7 . 1.6 005 .9 2.5 2.5 006 1.1 3.5 3.5 007 1.3 4.5 4.5 008 1.5 5.5 5.5 009 . . 4.8 010 1.9 . 5.8 011 2.1 . 6.8 012 2.3 . 7.8 013 2.5 . 8.8 014 2.7 11.5 11.5 015 2.9 12.5 12.5 016 3.1 13.5 13.5 017 3.3 14.5 14.5 018 3.5 15.5 15.5 019 3.7 16.5 16.5 020 3.9 17.5 17.5 Number of cases read: 20 Number of cases listed: 20 ++++++++++++++++++++++++++++++++++++ I promised this: Here is the test-data generator. NEW FILE. INPUT PROGRAM. . NUMERIC ID (N3) /DATUM (F4.1). . LOOP ID = 1 TO 20. . COMPUTE DATUM = (2*ID-1)/10. . IF (ID EQ 9) DATUM = \$SYSMIS. . END CASE. . END LOOP. END FILE. END INPUT PROGRAM. ```

Back to: Top of message | Previous page | Main SPSSX-L page