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
* (Q) How can I repalce missing values with the mean within 
	the category of a classified variable (which has 25 categories).

*(A) Method 1:  Posted to SPSSX list on 2003/03/30 by Raynald Levesque
      Method 2: Added by Ray on 2004/01/17 .
      

* Create a data file with some missing values.

SET SEED=123456.
INPUT PROGRAM.
LOOP id=1 TO 250.
- COMPUTE cat1=RND(UNIFORM(25)).
- IF (UNIFORM(1)>.15) var1=UNIFORM(5).
- END CASE.
END LOOP.
END FILE.
END INPUT PROGRAM.
SAVE OUTFILE='c:\\temp\\temp.sav'.

********************************.
* Start job (METHOD 1).
********************************.

GET FILE='c:\\temp\\temp.sav'.
SORT CASES BY cat1.
AGGREGATE OUTFILE=*
	/PRESORTED
	/BREAK=cat1
	/meanvar1=MEAN(var1).
LIST		/* to be able to check results */.
COMPUTE nobreak=1.
MATCH FILES FILE=* /BY=nobreak /FIRST=first /LAST=last.
WRITE OUTFILE='c:\\temp\\define vars1.sps'
	/'IF MISSING(var2) AND cat1 = 'cat1' var2='meanvar1'.'.
EXECUTE.

GET FILE='c:\\temp\\temp.sav'.
COMPUTE var2=var1.	/* preserve original values*/.
INCLUDE 'c:\\temp\\define vars1.sps'.
EXECUTE.

********************************.
* Start job (METHOD 2).
********************************.

GET FILE='c:\\temp\\temp.sav'.
SORT CASES BY cat1.
SPLIT FILE BY cat1.
RMV var2=MEAN(var1,ALL).
SPIT FILE OFF.