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
title 'Hot deck substitution of missing values in X within STRATUM'.

* Author: 	Th. van der Weegen       .
* Katholieke Universiteit e-mail:   T.vanderWeegen@maw.ru.nl .
* Date:		2006/03/24.

* READ DATA.

get file='c:\\program files\\spss\\Employee data.sav'.

* create some missing values.

IF RANGE(id,1,40) salary=0.
RENAME variables (id,jobcat,salary=respnr,stratum,x).
match files file=*  /keep=respnr,stratum,x.
descriptives var=x.


* DATA PREPARATION.

recode x (missing=1)(else=0) into xnew.
recode x (missing=1)(else=0) into xmis.
compute seqnbeg=$casenum.
* necessary for reconstruction original sequence.
sort cases by stratum,xmis,seqnbeg.
compute #strat=lag(stratum).
if ($casenum=1 or #strat ne stratum) seqnstr=1.
compute #volg=lag(seqnstr).

do if (missing(seqnstr)).
+ compute seqnstr=#volg+1.
end if.
formats seqnbeg,seqnstr(f7.0).

* counting number of valid codes for x within stratum.

aggregate outfile='$hot.sav' /presorted /break=stratum /stratn=n(stratum)
   /stratm=nmiss(x).
match files table='$hot.sav' /file=* /by stratum 
  /keep=respnr,stratum,x,seqnbeg,seqnstr,xnew,xmis,stratn,stratm.


* IF MISSING THEN RANDOM SEQUENCE NUMBER FROM STRATUM.
if (xmis=1) seqnstr=trunc(1+uniform(stratn-stratm)).


* SORTING SEQUENCENUMBERS.

sort cases by stratum,seqnstr,xnew.
do if (xnew=0).
+ compute xnew=x.
else if (seqnstr=lag(seqnstr) and stratum=lag(stratum)). 
+ compute xnew=lag(xnew).
else.
+ compute xnew=$sysmis.
end if.


* SAVING RESULTS IN ORIGINAL SEQUENCE AND LABELING.

sort cases by seqnbeg.
compute x=xnew.
descriptives var=x.
save outfile='hotresult.sav' /keep=respnr,stratum,x.