1
 2
 3
 4
 5
 6
 7
 8
 9
10
* Run macro only if there are cases.
* Raynald Levesque 2005/11/13  www.spsstools.net .

***************************.
*Step 1 (one time only):
***************************.

* Save next program block as "c:\temp\Run local if there are cases.SPS".
*---------------------------------.
BEGIN PROGRAM.
11
12
13
14
15
import spss
if spss.GetCaseCount() == 0:	# cannot run !local, print message & exit.
	spss.Submit("TITLE *File is empty, will not run the local macro")
else:
	spss.Submit("!local.")	# instruct SPSS to run !local
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
END PROGRAM.

*The above is called a "program block".

***************************.
* Step2 (one time only):
***************************.

* Add the following to the macro file . 
DEFINE !RunIfCase().
INSERT FILE="c:\temp\Run local if there are cases.SPS".
!ENDDEFINE.

*Note: Program blocks cannot be contained within a macro definition 
* but the above is acceptable.


*Suppose initial syntax is 
GET FILE="C:\Program Files\SPSS\employee data.sav".
SELECT IF jobcat=10.
GRAPH /BAR(SIMPLE)=COUNT BY jobcat .
FREQ VAR= gender.

* EXAMPLE #1.

*Modify syntax as follows.

GET FILE="C:\Program Files\SPSS\employee data.sav".
SELECT IF jobcat=3.
EXECUTE. 
/* the above EXE is necessary to update the number of cases */
DEFINE !local()
GRAPH /BAR(SIMPLE)=COUNT BY educ .
FREQ VAR= gender.
!ENDDEFINE .
!RunIfCase.


* EXAMPLE #2.

GET FILE='C:\Program Files\SPSS\employee data.sav'.
SET MPRINT=NO.
SELECT IF 1=0 /* To have an empty dataset and show that the macro works as expected */ .
EXE.

*Next macro contains the code to be executed when the active dataset is not empty.
DEFINE !local()
SET MPRINT=YES.
GRAPH /BAR(SIMPLE)=COUNT BY educ .
!ENDDEFINE.

!RunIfCase.