oatmilkyway
oatmilkyway

Reputation: 449

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.

Upvotes: 2

Views: 107

Answers (2)

Jignesh Sutar
Jignesh Sutar

Reputation: 2929

Your syntax looks good but in fact is logically not as you intend as Jay points out in his answer.

Also, you can simplify the syntax as follow to avoid complicated Boolean negations:

COMPUTE ped.crossing.with.signal = ANY(ped.action, 1, 2) AND ANY(reason, 15, 16)=0.

Using a single COMPUTE command in this way shall make your processing more efficient/faster, not to mention more parsimonious code also.

Upvotes: 1

lroha
lroha

Reputation: 34441

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.

Upvotes: 2

Related Questions