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
72
73
74
75
76
77
78
79
* RL wrote this to answer following Q in usenet on April 1,2000.

********************************************.
*QUESTION:
>I am doing a simulation in SPSS 9.0 that involves repeatedly generating
>samples (size say N = 100) with binary variables  variables,and examining  
>their relationship.  The pseudo-code might look something like this:
>
>new file .
>input program .
>loop #i = 1 to 100.
>compute x, a binary variable
>compute y, a binary variable with some relationship to x
>end loop .
>...etc.
>crosstabs y by x /......
>
>The preceding occurs inside a !do loop in a macro, so that I can
>generate some large number of samples, say 10,000.  The catch
>is that I would like to write the crosstabs cell frequencies to
>an output file.  Here's where my difficulty lies: If I declare 
>a procedure output file, the output file is re-initialized for
>each execution of the crosstabs /write =   command, so that only
>the last sample's frequencies, rather than all the samples' cell
>frequencies, are in the output file.  I thought I would hack my
>way around this by declaring the procedure output file to be 
>'*', i.e., the output or draft output window, thus capturing
>the results of all the crosstabs/write =   commands, but no
>luck, since '*' turns out to be an unacceptable file name
>for the procedure output command.
>
>Anyone have any ideas for making this work?  
>
******************************************.

*ANSWER.

DEFINE !DOIT(size=!TOKENS(1)
		/nbrec=!TOKENS(1))

!DO !count=1 !TO !size
new file .
input program .
loop #i = 1 to !nbrec.
compute x=TRUNC(UNIFORM(1)*2).
compute y=TRUNC(UNIFORM(1)*2).
end case.
end loop .
end file.
end input program.

VECTOR cell(4F8).
COMPUTE cell(1+x*2+y)=1.
COMPUTE numb=!count.
AGGREGATE
  /OUTFILE=*
  /BREAK=numb
  /cell_00 = SUM(cell1) /cell_01 = SUM(cell2) 
  /cell_10 = SUM(cell3) /cell_11 = SUM(cell4).

/* Cell_00 contains number of cases where x=0 and y=0./
/* Cell_01 contains number of cases where x=0 and y=1./
/* Cell_10 contains number of cases where x=1 and y=0./
/* Cell_11 contains number of cases where x=1 and y=1./
LIST.

!IF (!count=1) !THEN
SAVE OUTFILE='sample.sav'   /COMPRESSED.
!ELSE
ADD FILES /FILE=*   /FILE='sample.sav'.
SAVE OUTFILE='sample.sav'   /COMPRESSED.
!IFEND.

!DOEND.
!ENDDEFINE.

!DOIT size=3 nbrec=200.

GET FILE='sample.sav'.