IBM Support

Ranking variables within cases in SPSS

Troubleshooting


Problem

I have a series of numeric variables v1 to vN. I would like to create another series of N variables with values ranging from 1 to N which reflect the relative rank of v1 to vN within cases. If, for example, v1 has the third highest value among v1 to vN, I would like the first rank variable to hold a 3, and so on. SPSS appears to have a MIN and a MAX function to identify the lowest and highest values across variables and within cases, but I need to identify more than the extremes. Are there any means of doing this in SPSS?

Resolving The Problem

There are probably a few ways of doing this, many of which involving
comparisons of the values with SPSS functions. For large numbers of
variables, however, such comparisons might get fairly complicated. One
route which can avoid some of these complications would be simply to flip
the file, use the SPSS RANK procedure to compute rank scores on the new
variables (i.e. what were formerly cases), separate the old, flipped
variables from the new ones into distinct files, flip each back to the
original cases base, and match them. The annotated file below illustrates
a simple example of this process. It can be copied and pasted to an SPSS
syntax window and run, though it might be more instructive to run it in
stages in order to observe changes in the active file.


**This just reads in some simple data values.

DATA LIST /v1 TO v5 1-5.
BEGIN DATA
15962
45863
35719
25369
END DATA.

**We first need to flip the file.

FLIP.

**Next, we assign rank values to those values in each of the
**new variables, formerly cases.

RANK VARIABLES=var001 var002 var003 var004 (A) /RANK /PRINT=YES
/TIES=MEAN.

**We then need to save a copy of the file that holds only these
**newly created rank variables.

SAVE OUTFILE='temp1.sav'/KEEP=rvar001 rvar002 rvar003 rvar004.

**The next step is to re-flip the active file, preserving only
**the original flipped variables; this effectively returns the
**active file to the state it was in before the initial flip,
**save for the "case_lbl" variable that FLIP will always generate.

FLIP VARIABLES=var001 var002 var003 var004 /NEWNAME=case_lbl.

**We then save this file, dropping the "case_lbl" variable we'll
**no longer need.

SAVE OUTFILE='temp2.sav'/DROP=case_lbl.

**Next we need to call the file with the ranking variables. Since
**the cases in this file correspond to what are now variables in
**the main file (temp2.sav), we'll need to flip this one back to
**our original case base. We can rename the variables at this
**point so the names have a little more meaning.

GET FILE='temp1.sav'.
FLIP.

RENAME VARS (var001 var002 var003 var004 var005=
rankv1 rankv2 rankv3 rankv4 rankv5).

**All that remains is to match the two files together. If
**everything was done correctly up until this point, this should
**involve a straightforward parallel match and no key variable
**would be required. We can also drop the unneeded "case_lbl"
**variable that the last flip generated in the process.

MATCH FILES FILE='temp2.sav'/FILE=*/DROP=case_lbl.
EXE.

[{"Product":{"code":"SSLVMB","label":"IBM SPSS Statistics"},"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Component":"Not Applicable","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"Not Applicable","Edition":"","Line of Business":{"code":"LOB10","label":"Data and AI"}}]

Historical Number

17100

Document Information

Modified date:
16 April 2020

UID

swg21476161