1
votes

I need to create a new variable based on 3 variables.

If someone is coded as 1 against any 1 of 3 variables, they are coded as 1 in the new variable

If they don’t code 1 on any variable, but code as 2 against any 1 of 3 variables they are coded as 2 in the new variable

Everything else is coded as 99

In syntax, I’ve written this as:

IF (Keep_Any=1 OR Find_Any=1 OR Improve_Any=1) Keep_Find_Improve=1.
IF ((Keep_Find_Improve~= 1) & (Keep_Any=2 | Find_Any=2 | Improve_Any=2)) Keep_Find_Improve=2.
IF (Keep_Find_Improve~=1 & Keep_Find_Improve~=2) Keep_Find_Improve=99.
EXECUTE.

However, the first part correctly identifies cases coded as 1, but the rest of the syntax doesn't work. This is in despite of using some syntax that uses the exact same logic on other variables:

COMPUTE Keep_Any= Q9a_recoded = 1 | Q9b_recoded = 1 | Q9c_recoded = 1.
EXECUTE.

IF (Q9A_recoded=1 OR Q9B_recoded=1 OR Q9C_recoded=1) Keep_Any=1. 
IF ((Keep_Any~=1) & (Q9A_recoded= 2 OR Q9B_recoded=2 OR Q9C_recoded=2)) Keep_Any=2.
IF (Keep_Any~=1 & Keep_Any~=2) Keep_Any=99.
EXECUTE.

Does anyone have any ideas of why this is happening and how to fix it?

2
Could it be to do with missing? Ensure your variables do not have missing. If they are intended to have missing make sure they are account for correctly in your code? - Jignesh Sutar

2 Answers

1
votes

Try:

COMPUTE Target1=99.
COMPUTE Target1=ANY(2, V1, V2, V3).
COMPUTE Target1=ANY(1, V1, V2, V3).

Or better still (more efficient, even if more lines of code)

DO IF ANY(1, V1, V2, V3)=1.
   COMPUTE Target2= 1.
ELSE IF ANY(2, V1, V2, V3)=1.
  COMPUTE Target2 = 2.
ELSE.
  COMPUTE Target2=99.
END IF.
0
votes

The reason your syntax isn't working is the second condition you are using:
IF ((Keep_Find_Improve~= 1) & ...
When the first condition wasn't met, Keep_Find_Improve is still missing, and therefore doesn't meet the condition of ~=1. The same thing goes later for IF (Keep_Any~=1 & Keep_Any~=2). So you shouldn't be comparing Keep_Find_Improve to 1 or 2, you should be checking if it has received a value or if it's still missing:

IF (Keep_Any=1 OR Find_Any=1 OR Improve_Any=1) Keep_Find_Improve=1.
IF (missing(Keep_Find_Improve) & (Keep_Any=2 | Find_Any=2 | Improve_Any=2)) Keep_Find_Improve=2.
IF missing(Keep_Find_Improve) Keep_Find_Improve=99.
* alternatively: recode Keep_Find_Improve(miss=99).
EXECUTE.

All this being said, I recommend you use the more advanced code suggested by @JigneshSutar.