Search code examples
spss

Wonky results from basic IF AND OR Syntax in SPSS


My SPSS Syntax code below does not produce the results intended. Even when reason is equal to 15 or 16, it will flag ped.crossing.with.signal to 1.

COMPUTE ped.crossing.with.signal = 0.
IF  ((ped.action = 1 | ped.action = 2) AND (reason ~= 15 | reason ~= 16)) ped.crossing.with.signal = 1.
EXECUTE.

When I do it like this, it works... but why?

COMPUTE ped.crossing.with.signal = 0.
IF  (ped.action = 1 | ped.action = 2) ped.crossing.with.signal = 1. 
IF  (reason = 15 | reason = 16) ped.crossing.with.signal = 0.
EXECUTE.

Solution

  • It's not a wonky result, but rather the correct application of boolean algebra which is explained by De Morgan's Laws.

    The expression (reason ~= 15 | reason ~= 16) is equivalent to ~(reason = 15 and reason = 16) which in this case can never evaluate to false (because a single variable can't hold two values). Logically, the correct expression to use would be (reason ~= 15 & reason ~= 16) or ~(reason = 15 | reason = 16) although as pointed out already using the any function is more straightforward.