Daren Myo Paing
Daren Myo Paing

Reputation: 11

In a series of IF - ELSE IF statements, only the first IF works

I am trying to create a New variable "X", from Variables "A" "B" and "C". This is the code that I am using.

I only get the first option "VAS_USAGE=1", the other options are lost. What I wanted was for VAS_USAGE to have 1 to 6 values.
What am I doing wrong?

 ```
compute VAS_USAGE=0.
DO IF ((VAS1=1) AND (VAS2=1) AND (VAS3=1)). 
COMPUTE VAS_USAGE=1.
ELSE IF ((VAS1=1) AND (VAS2=1) AND (VAS3=0)).
COMPUTE VAS_USAGE=2.
ELSE IF ((VAS1=1) AND (VAS2=0) AND (VAS3=1)).
COMPUTE VAS_USAGE=3.
ELSE IF ((VAS1=1) AND (VAS2=0) AND (VAS3=0)).
COMPUTE VAS_USAGE=4.
ELSE IF ((VAS1=0) AND (VAS2=0) AND (VAS3=1)). 
COMPUTE VAS_USAGE=5.
ELSE IF ((VAS1=0) AND (VAS2=0) AND (VAS3=0)). 
COMPUTE VAS_USAGE=6.
END IF.   
EXECUTE.```

This is how the nested table looks like. This is what I was expecting to get.

enter image description here This is what I got.

1.00 = 63

Upvotes: 1

Views: 44

Answers (1)

eli-k
eli-k

Reputation: 11310

As suggested by @user45392 I would check if the answers 'yes' and 'no' are indeed coded as 1 and 0. In any case I would suggest avoiding the complex 'do if' scheme which might be prone to errors and harder to debug. For example, you could create VAS_USAGE like this:

compute VAS_USAGE = 100*VAS1 + 10*VAS2 + VAS3.

Or if you want to stick to the specific values you gave in your post, add this:

recode VAS_USAGE (111=1)(110=2)(101=3)(100=4)(1=5)(0=6).

Also if you just want a simple index from 1 to 8 you can do this:

compute VAS_USAGE = 4*VAS1 + 2*VAS2 + VAS3 + 1.

Upvotes: 1

Related Questions