Dear all,
I would like to make some looping syntax 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? Thanks & Regards, Winson |
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 |
Thanks Richard. DO REPEAT syntax does work in my case.
Thanks & Regards, Winson -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]]On Behalf Of Richard Ristow Sent: Monday, November 13, 2006 1:47 PM To: [hidden email] 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 |
In reply to this post by Winson Yeung
Shalom
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 . title runing sum. input program . loop subjectnum =1 to 20 . compute num= trunc(unifrom(60)). end case . end loop . end file . end input program . execute . compute #sum1=sum(#sum1,num, - lag(num,6)) . compute sum1 = #sum1. COMPUTE RNINGSUM = NUM. DO REPEAT LAGVAL = 1 TO 5. . COMPUTE RNINGSUM = RNINGSUM + LAG(NUM,LAGVAL). END REPEAT. execute . Hillel Vardi Winson Yeung wrote: > Dear all, > > I would like to make some looping syntax 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? > > Thanks & Regards, > Winson > |
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. |
Free forum by Nabble | Edit this page |