Scratch Variables

You can use scratch variables to facilitate operations in transformation blocks and input programs.

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.
Figure 1. Listing of case values
Name            LastName

Nick Lowe       Lowe
Dave Edmunds    Edmunds

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.
Figure 2. Listing of case values
    Var1     Var2     Var3     Var4

    2.00      .       2.00     2.00
    2.00      .       2.00     4.00
    2.00      .       2.00     6.00

In this example, the commands:

COMPUTE #ScratchVar=Var1+#ScratchVar. 
COMPUTE Var4=#ScratchVar.

are equivalent to:

COMPUTE Var4=Var1+Var4.
LEAVE Var4.