SPSS AnswerNet: Result
Solution ID: 100008309
Product: SPSS Base
Version:
O/S:
Question Type: Syntax/Batch/Scripting
Question Subtype: Data Transformations
Title:
Standardizing a Set of Variables as Z-scores Within Cases in SPSS
Description:
Q.
I want to transform each of a set of variables in an SPSS data file
to within-person Z-scores (i.e. having a mean of 0 and standard
deviation (SD) of 1 for each person), based on the person's mean
and SD on the original variables.
A.
You can perform these transformations through the menus in the data
editor, but it is simpler to use command syntax. Three examples are
shown below.
Example 1:
The simplest example is as follows.
If the variables to be standardized are adjacent in the data set,
you can use a VECTOR-LOOP structure. In this example the variables
to be standardized are called VARX1 to VARX8. (They don't actually
have to be remotely similar to each other in name, as long as they
are adjacent in the data file.) The new standardized variables are
called ZX1 to ZX8.
*************.
COMPUTE avgx = MEAN(varx1 TO varx8).
COMPUTE sdx = SD(varx1 TO varx8).
VECTOR xraw = varx1 TO varx8.
VECTOR zx (8).
LOOP #i = 1 TO 8.
COMPUTE zx(#i) = (xraw(#i) - avgx)/sdx .
END LOOP.
EXECUTE.
***********.
Example 2:
If your variables had more meaningful names and you wanted to use
similar names for the z-scores, the following example should help.
Suppose that you want to standardize 8 variables named
color, price, power, steer, safety, space, miles, and style,
which are stored in that order in the data file. The NUMERIC
command creates the listed variables in the data dictionary
without storing any values for them. Once again, it is assumed
here that the variables COLOR to STYLE are adjacent in the
data file.
*******************.
COMPUTE avgx = MEAN(color TO style).
COMPUTE sdx = SD(color TO style).
VECTOR att = color TO style.
NUMERIC zcolor zprice zpower zsteer zsafety zspace zmiles zstyle (F8.2).
VECTOR zatt = zcolor TO zstyle .
LOOP #i = 1 TO 8.
COMPUTE zatt(#i) = (att(#i) - avgx)/sdx .
END LOOP.
EXECUTE.
*****************.
Example 3:
If the original variables are not adjacent in the data set, you can
either rearrange them or use the DO REPEAT structure, as in: .
*****************.
COMPUTE avgx =
MEAN(color, price, power, steer, safety, space, miles, style).
COMPUTE sdx =
SD(color, price, power, steer, safety, space, miles, style).
DO REPEAT
att = color price power steer safety space miles style
/zatt = zcolor zprice zpower zsteer zsafety zspace zmiles zstyle.
COMPUTE zatt = (att - avgx)/sdx .
END REPEAT.
EXECUTE.
***************.