conditional-statementsspss

SPSS If statement with multiple conditions


in SPSS syntax, I am trying to create a filter that indicates if all the variables are 1, it would return a summary variable indicating 1.

I've tried the following in the SPSS syntax:

if (var1 TO var25 = 1 AND var29 TO var54 = 1) filtervar = 1.

gave me the following error:

2937 if An expression contains an invalid or inappropriate use of the TO keyword. The TO keyword may be used in the MEAN, SUM, and other similar functions in lieu of entering individual variable names, but it may not be used in other contexts.

I understand the long way would be to write:

if (var1 = 1 & var2 = 1 & var3 =1 ...) filtervar=1.

but that would take too long (I have 74 vairables! I would like the script to be concise as well).

Any recommendations?


Solution

  • If all the variables you are checking are dichotomous (as in 0 or 1) then one way to do this is this:

    compute filtervar=1.
    if any(0, var1 TO var25, var29 TO var54) filtervar=0.
    

    (also compute filtervar=1 - any(0, var1 TO var25, var29 TO var54).)

    Now if there are other possible values then we need something a bit more complex. One way is to loop on the variables:

    compute filtervar=1.
    do repeat vr=var1 TO var25 var29 TO var54.
      if vr<>1 filtervar=0.
    end repeat.
    

    Both possibilities do not take care of missing values - if you do have missing values you should define what should happen to filtervar in case of a missing value in one of the variables, and we can take care of it in the syntax.