dear spss-pros,
i think a well-developed stat-language - as spss is used to be - should do this, but i still cannot figure out how: can the [lag(var,i)] subcommand (within COMPUTE) be implemented within a loop, so that the lag-distance [i] can be incremented? as my algorithms have to search back in an indexed row, it does not make too much sense to copy it several times .... if this is possible - can the index counter also be passed over to a subfile? (connected via [include "path\file.sps"]) ? SPSS 13.0 on win XP thanx in advance norbert |
Well, one nice thing is how questions come around again(1).
At 04:29 PM 3/13/2007, Norbert NEUWIRTH wrote: >can the [lag(var,i)] subcommand (within COMPUTE) be implemented within >a loop, so that the lag-distance [i] can be incremented? First, no it can't. From SPSS 14 Command Syntax Reference, p.60, >>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. For probable reason, see note (2). >As my algorithms have to search back in an indexed row, it does not >make too much sense to copy it several times .... There are a couple of pretty good work-arounds, though. A. Use DO REPEAT to make a vector of lagged values, and index into that. Code not tested, but if you need up to 10 lags of numeric variable MEASURE, with format F5.1, like this: NUMERIC #MEAS.01 TO #MEAS.10 (F5.1) /* Note a. */. DO REPEAT /LAG_IDX = 1 TO 10 LAGD_VAL = #MEAS.01 TO #MEAS.10. . COMPUTE LAGD_VAL = LAG(MEASURE,LAG_IDX) /* Note b. */. END REPEAT. VECTOR LAG_MEAS = #MEAS.01 TO #MEAS.10. ........... Then, for I from 1 to 10, LAG_MEAS(I)=LAG(MEASURE,I). a. Variables whose names begin with '#' are 'scratch variables', and won't be kept in the file. b. This looks a lot like a variable argument to LAG, which I said you can't do, right? It works because DO REPEAT works like a macro loop: it 'unrolls' the loop, generating the 10 (in this case) different COMPUTE statements substituting the constant value for LAG_IDX each time. To see this, specify "END REPEAT PRINT". ==================== B. Or use 'shift' logic to build the same vector; it's arguably 'cuter', and may be more efficient. It's probably harder to write accurately. * At the >start< of the transformation program: . NUMERIC #MEAS.01 TO #MEAS.10 (F5.1) /* Note c. */. VECTOR LAG_MEAS = #MEAS.01 TO #MEAS.10. DO IF $CASENUM EQ 1. /* Note d. */. . RECODE #MEAS.01 TO #MEAS.10 /* Note e. */. (ELSE = SYSMIS). END IF. <whatever transformation code you want> * At the >end< of the transformation program: . LOOP #LAG = 10 TO 2 BY -1. . COMPUTE LAG_MEAS(#LAG) = LAG_MEAS(#LAG-1) /* Note f. */. END LOOP. COMPUTE LAG_MEAS(1) = MEASURE /* Note f. */. ........... Then, for I from 1 to 10, LAG_MEAS(I)=LAG(MEASURE,I). c. Another property of scratch variables is that their values persist from one case to the next. This implementation relies on that property. d. Still another property of scratch variables is that they're initialized to 0, not system-missing. You don't want 'lagged' values that can't exist (because they'd come from before the first case) to have value 0; so, this code starts them as system-missing e. Using RECODE with (ELSE=...) is a 'cute' way to assign the same value to a list of variables. f. This 'shifting' loop gives the "lag" variables the values they should have when the next record is processed. ................................. >Can the index counter also be passed over to a subfile? (connected via >[include "path\file.sps"]) ? If I understand you, it should work fine. An included file isn't really a subfile. Its contents are included in your program as text, and the code has access to the same variables as any other part of the code. >SPSS 13.0 on win XP It should work on any SPSS that has LAG and VECTOR, which is back to 9 at least, probably a good deal more. ================================= (1) I'm looking up the answers from "Re: Question on looping syntax", Richard Ristow <[hidden email]>, Mon, 13 Nov 2006 <00:46:46 -0500>, to SPSSX-L. (2) SPSS is built around processing its files in their entirety, reading records sequentially. "Reaching back" an indeterminate number of records is antithetical to this, as it's essentially random access to the file. I don't know how the file access 'looks' now, but adding the capacity for arbitrary look-back could introduce serious efficiencies that would apply even when LAG is not used. |
Free forum by Nabble | Edit this page |