* 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. 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 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.