Search code examples
if-statementsyntaxspssmissing-data

spss IF loop MISSINGS ignored in special cases


I want to compute a variable X=x1+x2+x3. x1, x2 and x3 have either the values 1 or 0. I recoded system missings to -77. There are some conditions which should be met.

1) If there is a missing value in either x1,x2 or x3, then it should be ignored if one or two of the other variables have the value 1. So the sum should be calculated although there is a missing value but only if there is at least one 1 (Eg. X = x1 + x2 + x3 = 0 + missing + 1 = 1)

2) If there is a missing value in either x1, x2 or x3, then it should not be ignored if there is no 1 at all and the sum should not be calculated. (Eg. X = x1 + x2 + x3 = 0 + missing + missing = missing).

I tried to make a loop with IF but it won't work and I just can't figure out why.

COMPUTE X =SUM(x1, x2, x3).
IF (x1=-77 AND x2~=1 AND x3~=1) X=999. 
IF (x2=-77 AND x1~=1 AND x3~=1) X=999.
IF (x3=-77 AND x1~=1 AND x2 ~=1)X=999.  
EXECUTE.

These are the returned results: when x1=1, x2 = 0, x3=-77 then X=1. (That is the result I want. The problem arises when x1=-77, x2=0, x3=0 because then X=0 and not 999 as I want it to be.

I think that with the loop above I am close to the result but something is missing. Below I post some other loops I made, but neither did work and I think the one above is the closest to the right answer.

Thank you so much for your help and happy easter! Cheers desperate Ichav :)

COMPUTE X = x1 + x2 + x3.
RECODE X (SYSMIS=-77).
IF ((X = -77 AND x1 = 1) OR (X = -77 AND x2 = 1) OR (X = -77 AND x3 = 1)) X =1.
EXECUTE.

Here X is always returned as -77.


Solution

  • This is to create some sample data to work on:

    data list list/x1 x2 x3.
    begin data
    1 0  -77
    0 -77 0
    0 0 0 
    1 0 1
    end data.
    MISSING VALUES x1 x2 x3 (-77).
    

    As you can see this is assuming -77 was defined as a missing value - otherwise calculating sum(x1, x2, x3) will fail.

    COMPUTE X=SUM(x1, x2, x3).
    if X=0 and nmiss(x1, x2, x3)>0 X=999.
    

    Now - if there were no 1 values, the sum is 0. If the sum is zero and there were any (more than 0) missing values involved - the sum is changed to 999.

    If you somehow calculated X indirectly without turning -77 into a missing value, you would be able to use the value -77 in if statements (as you tried before). An easier way to do it then:

    if X=0 and any(-77, x1, x2, x3) X=999.