Take an example when we have a series of 10 categorical variables var1, var2,..., var10
which take values from 1 to 5.
We create 5 dummy variables from each of these variables. For example, from var1
we generate dumvar1_1,..., dumvar1_5
. A dummy will receive the value of 1 if the original variable has the corresponding value with the dummy's order. That is, dumvar1_1 = 1 if var1 = 1
; dumvar1_1 = 0
otherwise. Likewise, dumvar1_2 = 1 if var1 = 2
; dumvar1_2 = 0
otherwise. The same with other dummies.
If I do in Stata, I will do like this:
forvalues i = 1(1)10 {
forvalues j = 1(1)5 {
generate dumvar`i'_`j' = 0
replace dumvar`i'_`j' = 1 if var`i' == `j'
}
}
Is there a way to do the same in SPSS?
Simply using the SPSSINC CREATE DUMMIES
extension command (that comes installed with my V24) does close to what you want.
SPSSINC CREATE DUMMIES VARIABLE=var1 TO var10
ROOTNAME1=dumvar1 dumvar2 dumvar3 dumvar4 dumvar5 dumvar6 dumvar7 dumvar8 dumvar9 dumvar10
/OPTIONS ORDER=A USEVALUELABELS=NO USEML=YES OMITFIRST=NO.
The only difference is the post script numbers go from 1 to 50, instead of repeating every 1 to 5. (If each var1
to var10
has all possible 5 values.)
For a plain vanilla SPSS approach, you can use the macro facility.
DATA LIST FREE / var1 TO var10 (10F1.0).
BEGIN DATA
1 2 3 4 5 1 2 3 4 5
END DATA.
DATASET NAME Sim.
EXECUTE.
DEFINE !MakeDums (Pre = !TOKENS(1)
/N = !TOKENS(1)
/V = !CMDEND)
VECTOR V = !V.
!DO !I = 1 !TO !N
!LET !VecStub = !CONCAT(!Pre,!I,"_")
VECTOR !VecStub (5,F1.0).
COMPUTE !VecStub(V(!I)) = 1.
!DOEND
!ENDDEFINE.
!MakeDums Pre=dumvar N=10 V = var1 TO var10.
RECODE dumvar1_1 TO dumvar10_5 (SYSMIS = 0).
EXECUTE.
You can do nested loops in SPSS similar to what the Stata code does, but you need to create the variables first outside of the LOOP
or DO REPEAT
syntax. So I use the macro facility.