<< SPSS Syntax >>
(PSY206) Data Management and Analysis
Basic Rules of SPSS Syntax
SPSS syntax provides a clear and reproducible way to enter, manage, and analyse data. Commands are written in the Syntax Editor and may be run individually, as a selected group, or all at once.
1. End every command with a period
A period (.) tells SPSS where a command ends.
FREQUENCIES VARIABLES = age.
DESCRIPTIVES VARIABLES = score.A command may occupy one line or several lines, but it must end with a period.
2. Capitalisation
SPSS commands and variable names are not case-sensitive. Therefore, the following are equivalent:
age AGE Age
descriptives DESCRIPTIVES Descriptives
Using uppercase for commands is a common convention because it makes the syntax easier to read.
String values, however, are case-sensitive:
SELECT IF gender = "Male".This will not select observations recorded as "male" or "MALE".
3. Add comments
Comments explain the purpose of the syntax and are ignored by SPSS.
A full-line comment begins with * and ends with a period:
* Examine the distribution of age.
FREQUENCIES VARIABLES = age.A comment may also be enclosed within /* and */:
/* Create a total score from three questions. */
COMPUTE total = q1 + q2 + q3.4. Spaces and line breaks
Extra spaces and line breaks generally do not affect a command.
DESCRIPTIVES VARIABLES = age income.The same command can be written as:
DESCRIPTIVES
VARIABLES = age income
.For readability, place related parts on separate lines and indent subcommands.
5. Subcommands
A slash (/) begins a subcommand. Subcommands specify additional options within the main command.
FREQUENCIES VARIABLES = q1 q2 q3
/STATISTICS = MEAN MEDIAN STDDEV
/HISTOGRAM.Here:
FREQUENCIESis the main command./STATISTICSrequests summary statistics./HISTOGRAMrequests a histogram.
6. Transform variables
Transformation commands create new variables or modify existing ones.
Compute a new variable
COMPUTE total = q1 + q2 + q3.Functions can also be used:
COMPUTE average = MEAN(q1 TO q3).TO represents a consecutive range of variables.
Create a variable conditionally
COMPUTE female = 0.
IF (gender = 2) female = 1.The first command assigns 0 to everyone. The second changes the value to 1 when gender = 2.
Recode a variable
RECODE adopted (0=0) (1 THRU 3=1) INTO adopt_bin.THRU represents a range of values. Using INTO creates a new variable and preserves the original one.
Count selected responses
COUNT n_agree = q1 TO q10 (1).This counts how many variables from q1 to q10 contain the value 1 for each respondent.
7. Sort cases
Cases may be sorted in ascending (A) or descending (D) order.
SORT CASES BY age (A).To sort by more than one variable:
SORT CASES BY gender (A) age (D).This sorts cases by gender and then by age within each gender.
8. Filter cases
A filter temporarily excludes cases from analyses without deleting them from the dataset.
COMPUTE filter_$ = (adopted > 0).
FILTER BY filter_$.Only cases for which adopted > 0 will be included in subsequent analyses.
Turn off the filter when it is no longer needed:
FILTER OFF.Always check the status bar because an unnoticed active filter can produce incorrect results.
9. Split analyses by groups
SPLIT FILE performs the same analysis separately for different groups.
SORT CASES BY gender.
SPLIT FILE LAYERED BY gender.
DESCRIPTIVES VARIABLES = score.SPSS will report the descriptive statistics separately for each gender. The data should first be sorted by the grouping variable.
Turn off split-file processing when finished:
SPLIT FILE OFF.10. Enter data manually
Small datasets can be entered directly through syntax.
DATA LIST LIST /
id (F2.0)
score (F2.0).
BEGIN DATA
1 20
2 18
3 25
END DATA.Here, F2.0 defines a numeric variable with a maximum width of two digits and no decimal places.
11. Import an Excel file
GET DATA
/TYPE = XLSX
/FILE = "mydata.xlsx"
/READNAMES = ON.READNAMES = ON tells SPSS to use the first row of the Excel file as variable names.
If necessary, specify a worksheet:
GET DATA
/TYPE = XLSX
/FILE = "mydata.xlsx"
/SHEET = NAME "Sheet1"
/READNAMES = ON.12. Use EXECUTE. when necessary
Commands such as COMPUTE, IF, RECODE, and COUNT are transformation commands. They may remain pending until SPSS reads the data.
EXECUTE. forces SPSS to apply all pending transformations immediately:
COMPUTE score2 = score * 2.
EXECUTE.It is useful when:
- You want to see the changes immediately in Data View.
- No analysis command follows the transformations.
- You are checking or debugging syntax step by step.
It is usually unnecessary when an analysis procedure follows:
COMPUTE score2 = score * 2.
DESCRIPTIVES VARIABLES = score2.DESCRIPTIVES automatically applies the pending transformation. Avoid using EXECUTE. after every transformation because repeated execution can slow down large datasets.
13. Commands run from top to bottom
The order of commands matters. A variable must be created before it is used:
COMPUTE total = q1 + q2 + q3.
DESCRIPTIVES VARIABLES = total.The following order would produce an error because total has not yet been created:
DESCRIPTIVES VARIABLES = total.
COMPUTE total = q1 + q2 + q3.Good practices
- Keep related commands together.
- Write commands in uppercase for readability.
- Use meaningful variable names.
- Indent subcommands beginning with
/. - Preserve original variables when recoding.
- Add brief comments to explain important steps.
- Group transformations and use one
EXECUTE.when needed. - Turn off
FILTERandSPLIT FILEafter use. - Save the syntax file so the analysis can be reproduced later.