Scratch Variables
You can use scratch variables to facilitate operations in transformation blocks and input programs.
- To create a scratch variable, specify a variable name that begins with the # character—for example, #ID. Scratch variables can be either numeric or string.
- Scratch variables are initialized to 0 for numeric variables or blank for string variables.
- Scratch variables cannot be used in procedures and
cannot be saved in a data file (but they can be written to an external
text file with
PRINT
orWRITE
). - Scratch variables cannot be assigned missing values, variable labels, or value labels.
- Scratch variables can be created between procedures but are always discarded as the next procedure begins.
- Scratch variables are discarded once a
TEMPORARY
command is specified. - The keyword
TO
cannot refer to scratch variables and permanent variables at the same time. - Scratch variables cannot be specified on a
WEIGHT
command. - Scratch variable cannot be specified on the
LEAVE
command. - Scratch variables are not reinitialized when a new
case is read. Their values are always carried across cases. (So using
a scratch variable can be essentially equivalent to using the
LEAVE
command.)
Because scratch variables are discarded, they are often useful as loop index variables and as other variables that do not need to be retained at the end of a transformation block. See the topic Indexing Clause (LOOP-END LOOP command) for more information. Because scratch variables are not reinitialized for each case, they are also useful in loops that span cases in an input program. See the topic Creating Data (LOOP-END LOOP command) for more information.
Example
DATA LIST LIST (",") /Name (A15).
BEGIN DATA
Nick Lowe
Dave Edmunds
END DATA.
STRING LastName (A15).
COMPUTE #index=INDEX(Name, " ").
COMPUTE LastName=SUBSTR(Name, #index+1).
LIST.
Name LastName
Nick Lowe Lowe
Dave Edmunds Edmunds
- #index is a scratch variable that is set to the numeric position of the first occurrence of a blank space in Name.
- The scratch variable is then used in the second
COMPUTE
command to determine the starting position of LastName within Name. - The default
LIST
command will list the values of all variables for all cases. It does not include #index becauseLIST
is a procedure that reads the data, and all scratch variables are discarded at that point.
In this example, you could have obtained the same end result without the scratch variable, using:
COMPUTE LastName=SUBSTR(Name, INDEX(Name, " ")+1).
The use of a scratch variable here simply makes the code easier to read.
Example: Scratch variable initialization
DATA LIST FREE /Var1.
BEGIN DATA
2 2 2
END DATA.
COMPUTE Var2=Var1+Var2.
COMPUTE Var3=0.
COMPUTE Var3=Var1+Var3.
COMPUTE #ScratchVar=Var1+#ScratchVar.
COMPUTE Var4=#ScratchVar.
LIST.
Var1 Var2 Var3 Var4
2.00 . 2.00 2.00
2.00 . 2.00 4.00
2.00 . 2.00 6.00
- The new variable Var2 is reinitialized to system-missing for each case, therefore
Var1+Var2
always results in system-missing. - The new variable Var3 is reset to 0 for each case (
COMPUTE Var3=0
), thereforeVar1+Var3
is always equivalent toVar1+0
. -
#ScratchVar is
initialized to 0 for the first case and is not reinitialized for subsequent
cases; so
Var1+#ScratchVar
is equivalent toVar1+0
for the first case,Var1+2
for the second case, andVar1+4
for the third case. - Var4 is set to the value of #ScratchVar in this example so that the value can be displayed in the case listing.
In this example, the commands:
COMPUTE #ScratchVar=Var1+#ScratchVar.
COMPUTE Var4=#ScratchVar.
are equivalent to:
COMPUTE Var4=Var1+Var4.
LEAVE Var4.