In SPSS temporary variables are called “scratch” variables. You may want to use scratch variables when you are writing a program and want to be sure that you do not overwrite variables in the existing dataset. However, scratch variables cannot be used in SPSS macros. In this example, we show one possible way to create and delete variables within an SPSS macro. This macro, called ab, requires three arguments (ID, x and iota). With this information, ab does some math and creates two new variables, called a and b. The set seed, compute and aggregate commands that appear at the top are used to create some of the variables to be used in the macro. The dataset https://stats.idre.ucla.edu/wp-content/uploads/2016/02/data_for_looping_wo_scratch_vars.sav contains only one variable, called subject_id.
set seed 9961458. compute iota1 = rv.normal(0,1). aggregate outfile = * mode = addvariables overwrite = yes /break = subject_id /x1 = nu(subject_id). exe. define !ab (ID = !charend('/') /x = !charend('/') /iota = !cmdend ) compute my_ID = !ID. compute my_x = !x. compute my_iota = !iota. compute a = 0. compute b = 0. exe. loop i = 1 to my_x - 1. compute a = a+ my_iota**(1*i)*(my_x + i). end loop. compute a = 2/my_x + 2*a/(my_x**4). loop i = 2-my_x to my_x-1. compute b = b + my_iota**(1*(my_x-i))*(my_x+abs(i)). end loop. compute b = b / (my_x**4). formats a b (f8.6). exe. delete variables my_ID my_x i my_iota. exe. !enddefine. !ab ID = subject_id /x = x1 /iota = iota1. list.
subject_id iota1 x1 a b 1 2.18 12 20.71195 51006.67 1 -.56 12 .166228 -.000404 1 .07 12 .166759 .000081 1 .05 12 .166737 .000062 1 .05 12 .166731 .000056 1 -.45 12 .166285 -.000350 1 .00 12 .166671 .000004 1 2.19 12 22.01318 57941.46 1 -.80 12 .166045 -.000500 1 .85 12 .174131 .005018 1 -1.14 12 .160783 .010173 1 .58 12 .168544 .001421 2 .67 8 .259883 .006539 2 -.26 8 .249102 -.000777 2 -1.17 8 .235630 .014480 2 1.51 8 .578922 2.856295 2 .60 8 .257225 .004905 2 -1.61 8 .120478 1.621782 2 1.39 8 .454921 1.017976 2 .25 8 .251501 .001180 3 .08 10 .200195 .000167 3 -2.43 10 -7.73109 10911.35 3 -1.33 10 .170643 .167896 3 -1.74 10 -.164870 25.45830 3 -.86 10 .198570 -.000855 3 -.83 10 .198708 -.000861 3 .41 10 .201635 .001279 3 -.24 10 .199581 -.000372 3 -.09 10 .199826 -.000152 3 .89 10 .214761 .010533 4 -.76 6 .327085 -.003653 4 -1.26 6 .296840 .037971 4 -1.16 6 .307841 .013728 4 .88 6 .380613 .036235 4 -.23 6 .331357 -.001621 4 1.31 6 .513102 .395622 5 1.44 4 .814710 .536701 5 -.40 4 .488325 -.008169 5 .32 4 .518827 .011884 5 1.29 4 .745783 .337227 Number of cases read: 40 Number of cases listed: 40