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
* QUESTION: How to replace missing values of bdifu1 TO bdifu20 by the mean 
* value (for the applicable sex) but only when (bmiss < 20).

* Answer by rlevesque@videotron.ca.

* Define dummy data file for illustration purposes.
NEW file.
INPUT PROGRAM.
SET SEED=7654321.
VECTOR bdifu(20F8.3).
LOOP id=1 TO 50.
LEAVE id.
* about 50% of bmiss will be less than 20.
COMPUTE bmiss=UNIFORM(40).
COMPUTE sex=(UNIFORM(1)<.5).
LOOP #id=1 TO 20.
* about 10% of bdiful will be missing.
	IF (UNIFORM(1)<.9) bdifu(#id)=TRUNC(UNIFORM(5)+.5).
END LOOP.
END CASE.
END LOOP.
END FILE.
END INPUT PROGRAM.
EXECUTE.

* Start the job.
SORT CASES BY sex.
aggregate outfile=bdifu.sav
   /presorted
   /break=sex
   /mbdifu1 to mbdifu20=mean(bdifu1 to bdifu20).

match files file= * /table=bdifu.sav
   /by sex.

do if (bmiss lt 20).
+  do repeat orig = bdifu1 to bdifu20   /copy=mbdifu1 to mbdifu20.
+  if (missing (orig)) orig=copy.
+  end repeat.
end if.
EXECUTE.