Question

As far know, IF is more efficient when used to execute a single, CONDITIONAL TRANSFORMATION. For multiple IF statements to define the condition, it is usually more efficient to use the RECODE command.

I have the following commands in SPSS :

IF (EDUC>12 AND GENDER='M')GROUP=1.
IF (EDUC<12 AND GENDER='M')GROUP=2.
IF (EDUC>12 AND GENDER='F')GROUP=3.
IF (EDUC<12 AND GENDER='F')GROUP=4.
EXECUTE.

Now i tried to RECODE the above commands as:

COMPUTE GROUP = 0
RECODE GROUP (EDUC>12 AND GENDER='M'=1)(EDUC<12 AND GENDER='M'=2)(EDUC>12 AND GENDER='F') (EDUC<12 AND GENDER='F').
EXECUTE.

But it's showing error.

How can i recode the above commands ? or is it possible to recode it ?

Was it helpful?

Solution

RECODE only takes a 1 to 1 mapping, and old value to a new value, it does not accept conditional statements like you have specified. You might consider a DO IF block in this circumstance. (Hopefully no one has exactly 12 for education!)

DO IF (EDUC>12 AND GENDER='M').
  COMPUTE GROUP=1.
ELSE IF (EDUC<12 AND GENDER='M').
  COMPUTE GROUP=2.
ELSE IF (EDUC>12 AND GENDER='F').
  COMPUTE GROUP=3.
ELSE IF (EDUC<12 AND GENDER='F').
  COMPUTE GROUP=4.
END IF.

This evaluates the IF statements case by case, and so if you have 1 million records falling into the (EDUC>12 AND GENDER='M') and only a few falling into the other categories, for those million cases it will evaluate the first condition as true and will not evaluate the subsequent IF statements (which is not true of writing out the equivalent IF on multiple lines).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top