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
* Proportionate sampling without replacement.


*************************************************************.
*Generate a file of 200 cases for illustration purposes.
*************************************************************.
NEW file.
input program.
loop block=1 to 200.
leave block.
compute pop=RND(uniform(10000)).
FORMATS block pop (F8.0).
end case.
end loop.
end file.
end input program.
execute.

SET MPRINT=ON.
*-------------------------------BEG OF MACRO1---------------------------------------------------.
DEFINE !GETONE().
COMPUTE case# =$casenum.
CREATE t_weight=CSUM(pop).
SORT CASES BY case#(D).
CREATE c_weight= CSUM(pop).
SORT CASES BY case#.
EXECUTE.
* Draw a random number and pick one line.
DO IF ($casenum=1).
+	compute #draw=uniform(1)*c_weight.
+ 	COMPUTE draw=#draw.
+	compute filter_$=(#draw<t_weight).		/* this record is selected if filter=1.
ELSE.
+	compute filter_$=(#draw<t_weight) & (#draw>lag(t_weight)).	/* this record is selected if filter=1.
+	COMPUTE draw=#draw.
END IF.
EXECUTE.

!ENDDEFINE.
*----------------------END OF MACRO1----------------------------------------------------.



*----------------------------BEG OF MACRO2-----------------------------.

DEFINE !SAMPLE (size=!TOKENS(1)).
!DO !count=1 !TO !size.

!GETONE.
SAVE OUTFILE='C:\\temp\\temp4.sav'.

SELECT IF(filter_$=1).
EXECUTE .

!IF (!count<>1) !THEN ADD FILES /FILE=* /FILE='C:\\temp\\select4.sav'.
!IFEND.
SAVE OUTFILE='C:\\temp\\select4.sav'.
GET FILE='C:\\temp\\temp4.sav'.
EXECUTE .

SELECT IF(filter_$=0).
EXECUTE.
!DOEND.
!ENDDEFINE.
*--------------------------END OF MACRO2----------------------------------------.


!SAMPLE size=10.

GET FILE='C:\\temp\\select4.sav'.
LIST.