[Skip Global Navigation]

SPSS Tech Tips

SPSS Direct Home

Computing a new variable in SPSS® for Windows® indicating the highest ranking of other variables within a case

This is a complimentary tip available only to SPSS Direct subscribers.

Question

I have a data file containing five variables which represent the results of a test taken at five different intervals. I would like to create a new variable that indicates which test result was highest for each subject. How can I create a new variable in SPSS for Windows that contains the name of an existing variable with the highest value?

Answer

You can create this new variable by using SPSS command syntax. For this example, we will use the simplified dataset shown below.


Figure 1: This file contains the test results shown in variables “test1” through “test5.”
(Click to enlarge)

As shown below, you first will need to recode your variables to account for missing values. Begin by recoding “missing” so that it has a value of 0. Next, use the LOOP and VECTOR commands to compare each test result to the other results within the subjects.

RECODE test1 test2 test3 test4 test5 (MISSING=0).
EXECUTE.
VECTOR X (5)/V=test1 TO test5.
LOOP #i=1 to 5.

Vector syntax associates a vector name with a set of existing variables, or it defines a vector of new variables. A vector is a set of variables that can be referred to by using an index. With vector syntax, you can compute new variables to indicate which test score was the highest. Begin by using the MAX subcommand. This will produce new variables with a value of 1 for the highest score and a value of 0 for the others. END LOOP and EXECUTE commands complete this portion of the syntax.

COMPUTE X(#i)=(V(#i)=MAX(test1,test2,test3,test4,test5)).
END LOOP.
EXECUTE.

Finally, you need to create your new variable, “newvar.” This new variable will indicate which one of the variables has the highest value per subject. This is done by a series of “if” statements referencing the set of variables created by the prior COMPUTE command. The variable with a value of 1.00 will correspond to the test variable with the highest value and the variable name we want to appear in the new variable. The syntax below inputs the variable name as a string into the new variable.

STRING newvar (A12).
IF (X1=1.00) newvar = 'test1' .
IF (X2=1.00) newvar = 'test2' .
IF (X3=1.00) newvar = 'test3' .
IF (X4=1.00) newvar = 'test4' .
IF (X5=1.00) newvar = 'test5' .
EXECUTE .

The complete syntax and resulting data file are shown below.


Figure 2: The complete syntax.
(Click to enlarge)


Figure 3: The data file that results after the complete syntax has run.
(Click to enlarge)

For more information on using syntax within SPSS for Windows, please refer to the SPSS 14.0 Command Syntax Reference that accompanies your SPSS software.