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
* (Q) Would anyone have a bit of syntax that would generate, say 1000 variables,
	each of which is a randomisation of a variable in the current file?  My file
	contains 4 variables.  3 identifiers and one count variable "countvar".  I
	need to generate 1000 randomisations of countvar.

* (A) By rlevesque@videotron.ca to SPSSX-L list on 2001/08/13.
* 	http://www.spsstools.net


SET MPRINT=no.
*///////////////////////////.
DEFINE !random (id=!TOKENS(1) /myvar=!TOKENS(1) /nb=!TOKENS(1) /others=!CMDEND)

/* id 	 must be a numeric variable, without duplicates.*/
/* myvar the variable to be randomized.*/
/* nb 	 the number of variables containing "myvar values" in the final file*/
/* others the other variables to be kept in addition to myvar and id */

LOOP cnt=1 TO !nb.
XSAVE OUTFILE='c:\\temp\\temp.sav' /KEEP=cnt !id !myvar !others.
END LOOP.
EXECUTE.
GET FILE='c:\\temp\\temp.sav'.

COMPUTE draw=UNIFORM(1).
RANK VARIABLES=draw BY cnt  /RANK INTO rdraw.
SORT CASES BY rdraw cnt.
DO IF $CASENUM>1 & rdraw=LAG(rdraw).
COMPUTE id=LAG(id).
END IF.

VECTOR v(!nb F8.0).
COMPUTE v(cnt)=!myvar.
AGGREGATE
  /OUTFILE=*
  /BREAK=id
  /!others=FIRST(!others) 
  /v1 TO !CONCAT('v',!nb) = FIRST(v1 TO !CONCAT('v',!nb)).

* Next command is just to show that the variables have the same sum and stddev.
* They each contain the same values but in different order.
SUMMARIZE
  /TABLES=v1 TO !CONCAT('v',!nb)
  /FORMAT=NOLIST TOTAL
  /TITLE='Case Summaries'
  /MISSING=VARIABLE
  /CELLS=COUNT SUM STDDEV .
!ENDDEFINE.
*///////////////////////////.

SET MPRINT=yes.
* Example 1.
GET FILE='C:\\Program Files\\SPSS\\Employee data.sav' /KEEP=id gender bdate salary.
!random id=id nb=15 myvar=salary others=gender bdate.

*Example 2.
GET FILE='C:\\Program Files\\SPSS\\World95.sav' /KEEP=country density populatn.
COMPUTE id=$CASENUM.
!random id=id nb=10 myvar=populatn others=country density.