In addition to using INSERT to create modular master command syntax files, you can define global settings that will enable you to use those same command files for different reports and analyses.
Example
You can create a separate command syntax file that contains a set of FILE HANDLE
commands that define file locations and a set of macros that define global variables for client name, output language, and so on. When you need to change any settings, you change them once in the global definition file, leaving the bulk of the command syntax files unchanged.
*define_globals.sps.
FILE HANDLE data /NAME='c:\examples\data'.
FILE HANDLE commands /NAME='c:\examples\commands'. FILE HANDLE spssdir /NAME='c:\program files\spss'. FILE HANDLE tempdir /NAME='d:\temp'.
DEFINE !enddate()DATE.DMY(1,1,2004)!ENDDEFINE. DEFINE !olang()English!ENDDEFINE.
DEFINE !client()"ABC Inc"!ENDDEFINE. DEFINE !title()TITLE !client.!ENDDEFINE.
The first two FILE HANDLE commands define the paths for the data and command syntax files. You can then use these file handles instead of the full paths in any file specifications.
The third FILE HANDLE command contains the path to the SPSS folder. This path can be useful if you use any of the command syntax or script files that are installed with SPSS.
The last FILE HANDLE command contains the path of a temporary folder. It is very useful to define a temporary folder path and use it to save any intermediary files
created by the various command syntax files making up the project. The main purpose of this is to avoid crowding the data folders with useless files, some of which might be very large. Note that here the temporary folder resides on the D drive. When possible, it is more efficient to keep the temporary and main folders on different hard drives.
The DEFINE–!ENDDEFINE structures define a series of macros. This example uses simple string substitution macros, where the defined strings will be substituted wherever the macro names appear in subsequent commands during the session. See Chapter 6 for more information.
!enddate contains the end date of the period covered by the data file. This can be useful to calculate ages or other duration variables as well as to add footnotes to tables or graphs.
!olang specifies the output language.
!client contains the client’s name. This can be used in titles of tables or graphs.
!title specifies a TITLE command, using the value of the macro !client as the title text.
The master command syntax file might then look something like this:
INSERT FILE = "c:\examples\commands\define_globals.sps". !title.
INSERT FILE = "data\prepare data.sps". INSERT FILE = "commands\combine data.sps". INSERT FILE = "commands\do tests.sps". INCLUDE FILE = "commands\report groups.sps".
The first INCLUDE runs the command syntax file that defines all of the global settings. This needs to be run before any commands that invoke the macros defined in that file.
!title will print the client’s name at the top of each page of output.
"data" and "commands" in the remaining INSERT commands will be expanded to
"c:\examples\data" and "c:\examples\commands", respectively.
Note: Using absolute paths or file handles that represent those paths is the most reliable
way to make sure that SPSS finds the necessary files. Relative paths may not work as you might expect, since they refer to the current working directory, which can change frequently. You can also use the CD command or the CD keyword on the INSERT
Global Subroutines
You can create much more sophisticated macros than these simple string substitution macros, including macros that take arguments that you specify in the macro calls. As a general rule, you may find it most useful to keep most macros in separate files, distinct from your regular command syntax files. You can then use the macro files as a library of global subroutines.
Example
This macro executes one set of commands for a list of categorical variables that you supply and another set of commands for a list of scale variables that you supply. This might be useful if you routinely generate the same descriptive and summary statistics as a preliminary step before further analysis, where the only thing that differs is the variables used in the summaries.
*macro_lib1.sps.
DEFINE !sumstat (catvars = !CHAREND('/') /scalevars = !CMDEND)
!IF (!catvars ~=!NULL) !THEN frequencies variables = !catvars /barchart.
!IFEND
!IF (!scalevars ~= !NULL) !THEN frequencies variables = !scalevars /format = notable
/statistics = mean median min max /histogram.
!IFEND !ENDDEFINE.
The macro contains two arguments: one for handling categorical variables and one for handling scale variables.
catvars = !CHAREND('/') specifies that any text in the macro call between catvars =
and the next forward slash encountered in the macro call will be used wherever
!catvars appears in the macro.
scalevars = !CMDEND specifies that any text in the macro call that appears after
scalevars = will be used wherever !scalevars appears in the macro.
Two FREQUENCIES commands are defined in the macro: one to use for categorical variables and one to use for scale variables.
The !IF statements make sure that the macro call includes a list of catvars and/or
scalevars before running the respective FREQUENCIES command. This provides more flexibility, since the macro call can then contain one list of either kind or both lists without generating any errors.
You could then invoke the macro in several ways:
***First run the file that defines the macro***. INCLUDE FILE="c:\examples\commands\macro_lib1.sps".
***now run the macro with both catvars and scalevars***. !sumstat catvars = marital gender jobcat
/scalevars = income age edyears.
***now run it with just catvars***.
!sumstat catvars = marital gender jobcat. ***and now just scalevars***.
!sumstat scalevars = income age edyears.
The first macro call would generate two FREQUENCIES commands; the other two would each generate one FREQUENCIES command. In each case, the variables listed in the macro call would be used in the VARIABLES subcommand. Macros are discussed in greater detail in Chapter 6.
33