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
* Initial file has category variable cat1 which has n different values.
* Objective is to create n different files.

* This creates a data file for illustration purposes.
* The categorical variable cat1 has up to 10 different values.
* The syntax works for any different values of cat1.
* They do not have to be consecutive.
* Raynald Levesque.

NEW FILE.
INPUT PROGRAM.
LOOP id=1 TO 500.
	COMPUTE cat1=TRUNC(UNIFORM(10)).
	END CASE.
END LOOP.
END FILE.
END INPUT PROGRAM.
EXECUTE.
SAVE OUTFILE='c:\\data\\mydata.sav'.

* Now start the job.
set mprint=on.

*//////////////////////////////////////////.
DEFINE !SPLIT (var=!TOKENS(1) /value=!TOKENS(1) ).
TEMPORARY.
SELECT IF (!var=!value).
SAVE OUTFILE=!QUOTE(!CONCAT('c:\\data\\temp',!value,'.sav')).
EXECUTE.
!ENDDEFINE.
*//////////////////////////////////////////.

*Find all different value of cat1 which exists.
AGGREGATE  /OUTFILE=*
  /BREAK=cat1  /dummy = FIRST(id).
FORMAT cat1(F8.0).

* Write a syntax file which will call the above macro.
WRITE OUTFILE='c:\\data\\temp.sps'
/ '!SPLIT var=cat1 value=' cat1 '.'.
Execute.

* Get the original data file and do the macro calls.
GET FILE='c:\\data\\mydata.sav'.
INCLUDE 'C:\\data\\temp.sps'.