Command Order

Command order is more often than not a matter of common sense and follows this logical sequence: variable definition, data transformation, and statistical analysis. For example, you cannot label, transform, analyze, or use a variable in any way before it exists. The following general rules apply:

  • Commands that define variables for a session (DATA LIST, GET, GET DATA, MATRIX DATA, etc.) must precede commands that assign labels or missing values to those variables; they must also precede transformation and procedure commands that use those variables.
  • Transformation commands (IF, COUNT, COMPUTE, etc.) that are used to create and modify variables must precede commands that assign labels or missing values to those variables, and they must also precede the procedures that use those variables.
  • Generally, the logical outcome of command processing determines command order. For example, a procedure that creates new variables in the active dataset must precede a procedure that uses those new variables.

In addition to observing the rules above, it is often important to distinguish between commands that cause the data to be read and those that do not, and between those that are stored pending execution with the next command that reads the data and those that take effect immediately without requiring that the data be read.

  • Commands that cause the data to be read, as well as execute pending transformations, include all statistical procedures (e.g., CROSSTABS, FREQUENCIES, REGRESSION); some commands that save/write the contents of the active dataset (e.g., DATASET COPY, SAVE TRANSLATE, SAVE); AGGREGATE; AUTORECODE; EXECUTE; RANK; and SORT CASES.
  • Commands that are stored, pending execution with the next command that reads the data, include transformation commands that modify or create new data values (e.g., COMPUTE, RECODE), commands that define conditional actions (e.g., DO IF, IF, SELECT IF), PRINT, WRITE, and XSAVE. For a comprehensive list of these commands, see Commands That Are Stored, Pending Execution .
  • Commands that take effect immediately without reading the data or executing pending commands include transformations that alter dictionary information without affecting the data values (e.g., MISSING VALUES, VALUE LABELS) and commands that don't require an active dataset (e.g., DISPLAY, HOST, INSERT, OMS, SET). In addition to taking effect immediately, these commands are also processed unconditionally. For example, when included within a DO IF structure, these commands run regardless of whether or not the condition is ever met. For a comprehensive list of these commands, see Commands That Take Effect Immediately .

Example

DO IF expense = 0.
- COMPUTE profit=-99.
- MISSING VALUES expense (0).
ELSE.
- COMPUTE profit=income-expense.
END IF.
LIST VARIABLES=expense profit.
  • COMPUTE precedes MISSING VALUES and is processed first; however, execution is delayed until the data are read.
  • MISSING VALUES takes effect as soon as it is encountered, even if the condition is never met (i.e., even if there are no cases where expense=0).
  • LIST causes the data to be read; thus, both COMPUTE and LIST are executed during the same data pass.
  • Because MISSING VALUES is already in effect by this time, the first condition in the DO IF structure will never be met, because an expense value of 0 is considered missing and so the condition evaluates to missing when expense is 0.