1

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?

Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
NonSleeper
  • 849
  • 1
  • 6
  • 17
  • 1
    Simply, what you cite are not double compound or (the term used) compound double quotes at all so far as Stata is concerned. They are left and right single quotes used in dereferencing or evaluating local macros. I am not an SPSS user, but I doubt that your question will make much sense to most SPSS user-programmers and even if they know Stata too the question would be much more useful to others if you just posted as a question about a double loop to create indicator variables. Incidentally in Stata you can condense the inner statements to `generate dum\`i'_\`j' = var\`i' == \`j'` – Nick Cox Apr 10 '17 at 10:21
  • Thanks for your clarification of the term. The creation of the dummy/indicator variables is half of what I intend to do. The other half is to assign them values based on the original variables in a rather efficient way (like Stata does). Due to historical reason, the project I'm working on inherited both SPSS and Stata bits, so I think this may be helpful for those working on multiple platforms. – NonSleeper Apr 11 '17 at 06:51

1 Answers1

1

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.

Andy W
  • 5,031
  • 3
  • 25
  • 51