Professional Documents
Culture Documents
12
Chapter
SPSS provides a powerful command language that allows you to save and automate many common tasks. It also provides some functionality not found in the menus and dialog boxes. Most commands are accessible from the menus and dialog boxes. However, some commands and options are available only by using the command language. The command language also allows you to save your jobs in a syntax file so that you can repeat your analysis at a later date or run it in an automated job with the Production Facility. A syntax file is simply a text file that contains commands. While it is possible to open a syntax window and type in commands, it is often easier if you let the software help you build your syntax file using one of the following methods:
Pasting command syntax from dialog boxes Copying syntax from the output log Copying syntax from the journal file
In the online Help for a given procedure, click the command syntax link in the Related Topics list to access the syntax diagram for the relevant command. For complete documentation of the command language, refer to the SPSS Command Syntax Reference. Complete command syntax documentation is automatically installed when you install SPSS. To access the syntax documentation:
E From the menus choose: Help Command Syntax Reference
297
298 Chapter 12
Syntax Rules
Keep in mind the following simple rules when editing and writing command syntax:
Each command must begin on a new line and end with a period (.). Most subcommands are separated by slashes (/). The slash before the first subcommand on a command is usually optional. Variable names must be spelled out fully. Text included within apostrophes or quotation marks must be contained on a single line. Each line of command syntax cannot exceed 80 characters. A period (.) must be used to indicate decimals, regardless of your Windows regional settings. Variable names ending in a period can cause errors in commands created by the dialog boxes. You cannot create such variable names in the dialog boxes, and you should generally avoid them.
Command syntax is case insensitive, and three- or four-letter abbreviations can be used for many command specifications. You can use as many lines as you want to specify a single command. You can add space or break lines at almost any point where a single blank is allowed, such as around slashes, parentheses, arithmetic operators, or between variable names. For example,
FREQUENCIES VARIABLES=JOBCAT GENDER /PERCENTILES=25 50 75 /BARCHART.
and
freq var=jobcat gender /percent=25 50 75 /bar.
INCLUDE Files
For command files run via the INCLUDE command, the syntax rules are slightly different:
Each command must begin in the first column of a new line. Continuation lines must be indented at least one space. The period at the end of the command is optional.
Unless you have existing command files that already use the INCLUDE command, you should probably use the INSERT command instead since it can accommodate command files that conform to either set of rules. If you generate command syntax by pasting dialog box choices into a syntax window, the format of the commands is suitable for any mode of operation. See the Command Syntax Reference (available in PDF format from the Help menu) for more information.
The command syntax is pasted to the designated syntax window. If you do not have an open syntax window, a new syntax window opens automatically, and the syntax is pasted there.
300 Chapter 12 Figure 12-1 Command syntax pasted from a dialog box
Note: If you open a dialog box from the menus in a script window, code for running syntax from a script is pasted into the script window.
301 Working with Command Syntax Figure 12-2 Command syntax in the log
302 Chapter 12 E On the Viewer tab, select Display commands in the log.
As you run analyses, the commands for your dialog box selections are recorded in the log.
E Open a previously saved syntax file or create a new one. To create a new syntax
303 Working with Command Syntax Figure 12-3 Editing the journal file
Delete warnings and error messages before saving and running syntax from the journal file
Select All files (*.*) for Files of Type or enter *.jnl in the File Name text box to display journal files in the file list. If you have difficulty locating the file, use Options on the Edit menu to see where the journal is saved in your system.
E Edit the file to remove any error messages or warnings, indicated by the > sign. E Save the edited journal file using a different filename. (We recommend that you use a
filename with the extension .sps, the default extension for syntax files.)
304 Chapter 12 E Click the Run button (the right-pointing triangle) on the Syntax Editor toolbar.
or
E Select one of the choices from the Run menu.
All. Runs all commands in the syntax window. Selection. Runs the currently selected commands. This includes any commands
partially highlighted.
Current. Runs the command where the cursor is currently located. To End. Runs all commands from the current cursor location to the end of the
command syntax file. The Run button on the Syntax Editor toolbar runs the selected commands or the command where the cursor is located if there is no selection.
Figure 12-4 Syntax Editor toolbar
If the last command in the syntax file is a command that reads the data file (such as a statistical or graphing procedure), no EXECUTE commands are necessary and they can be deleted. If you are unsure if the last command reads the data file, in most cases you can delete all but the last EXECUTE command in the syntax file.
Lag Functions
One notable exception is transformation commands that contain lag functions. In a series of transformation commands without any intervening EXECUTE commands or other commands that read the data, lag functions are calculated after all other transformations, regardless of command order. For example:
COMPUTE lagvar=LAG(var1) COMPUTE var1=var1*2
and
COMPUTE lagvar=LAG(var1) EXECUTE COMPUTE var1=var1*2
yield very different results for the value of lagvar, since the former uses the transformed value of var1 while the latter uses the original value.