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
*Does anyone know how to substitute missing values with valid values
> randomly selected from the other cases? For instance, if case #x is
> missing var1, I would like to randomly select another subject who has a
> valid var1 value and substitute that var1 value for case #x, var1. I
> would like to do this for every case which has a missing value for var1.
> 
> Thanks in advance.
> 
> --
> Jeff Wayman 
***************************************************


*****.
fre postnum/format notable.
*** in this file 17363 cases have valid values in postnum.
if postnum >0 x1=$casenum.
execute.
*** use $casenum as originally numbering of cases.
RANK
VARIABLES=x1 (A) /RANK /PRINT=YES
/TIES=LOW .
** produces rx1.
if sysmis(postnum) rx1=trunc(1+uniform(17362)).
execute.
** asign a random case, based on the rank of the case, to the one with a missing value in postnum.
sort cases by rx1(a) x1(d).
execute.
** don't forget the sorting order.
compute postadd=postnum.
*** safety copy.
if sysmis(postadd) postadd=lag(postadd).
execute.

*It works even if the randomisation leads to identical values in rx1. 

*Smik (from usenet 09/25/1997 re:How do I do this?!).