``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74``` ```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. ***************. ```
Related pages

...