StatLab


<  July 2014 >
Su Mo Tu We Th Fr Sa
    1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

Reserve the classrooms


Schedule for 07/29/2014


Stat Consultants
CSSSI (219 Prospect St)
To see the Consultant's schedule, please see our calendar on the CSSSI website.
To see the room schedule for the CSSSI Classroom, please see our calendar on the schedule.yale.edu website.
To see the room schedule for the CSSSI Rosenkranz, please see our calendar on the schedule.yale.edu website.

INDEX

SPSS Syntax Basics

This Help Sheet is intended to provide a sampling of basic syntax in SPSS. Compilation of syntax is required for batch processing in SPSS for UNIX and for some data processing in SPSS for Windows.

Structure of Commands in SPSS

Commands in SPSS begin with a keyword that is the name of the command and often have additional specifications, such as subcommands and user specifications. The end of the command is marked by a period/full stop.
Refer to the SPSS Base System Syntax Guide for a discussion of available commands and options. In SPSS, commands must always be placed in the first column, and subcommands and user specifications must be indented at least one space.

Example: the FREQUENCIES command

FREQUENCIES produces tables of frequency counts and percentages of the values of individual variables. FREQUENCIES is used to obtain frequencies and statistics for categorical variables and to obtain statistics and graphical displays for continuous variables.

Command
FREQUENCIES VARIABLES=variable_names
/subcommands_if_applicable .

Subcommands
(always indented at least one space)
 
     
/ BARCHART produces a bar chart
/ HISTOGRAM produces a histogram
NOTE: FREQUENCIES can produce a bar chart or histogram but not both.
/ NTILES=n calculates the percentages that divide the distribution into the specified number of categories and displays the values for the percentiles requested
/ STATISTICS generates the mean, standard deviation, minimum and maximum
Refer to the SPSS Base System Syntax Reference Guide (p.334) for other available statistics or use the subcommand STATISTICS=ALL to request all available statistics.

NOTE: Variable names in SPSS are separated by SPACES.

Loading DATA into SPSS

ASCII data

NOTE: the following instructions apply to ASCII data files of which the logical record length length does not exceed 1024 columns. Click herefor SPSS Syntax help in loading ASCII data files with logical record length greater than 1024 columns.

Fixed-format ASCII data
DATA LIST FILE='path_and_filename.extension' FIXED
/ variable_names start_column-end_column (A_if_string_variable) .
EXECUTE.

Free-format ASCII data, one line of data per case
DATA LIST FILE='path_and_filename.extension' LIST
/ variable_names (A_if_string_variable) .
EXECUTE.

Fixed-format ASCII data, multiple lines of data per case
DATA LIST FILE='path_and_filename.extension' FIXED RECORDS=n
n=number of lines of data per case
/1 variable_names start_column-end_column (A_if_string_variable)
/2 variable_names start_column-end_column (A_if_string_variable)
.
.
.
/last_n variable_names start_column-end_column (A_if_string_variable) .
last_n=last line of data per case
EXECUTE.

Data are inline--included in the syntax file
DATA LIST FREE_or_FIXED
Use RECORDS=n for fixed-format data with multiple lines of data per case
/ variable_names column_locations_if_fixed_format_data (A_if_string_variable) .
BEGIN DATA
(data goes here)
END DATA.
EXECUTE.

SPSS and SAS data files

SPSS Data files
GET FILE='path_and_filename.extension'
/ KEEP_or_DROP=variable_names .
EXECUTE.
Use DROP or KEEP for retaining only some of the variables in the data file

SPSS Portable files
IMPORT FILE='path_and_filename.extension'
/ KEEP_or_DROP=variable_names .
EXECUTE.
See note on DROP or KEEP above

SAS datasets

SPSS builds a working data file from a dataset contained in a SAStransport file .

GET SAS DATA='path_and_filename.extension' .
EXECUTE.

If using a specific dataset in a SAS Library
GET SAS DATA='path_and_libraryname.extension' DSET(datasetname .
EXECUTE.

Missing Values, Variable and Value Labels

MISSING VALUES declares values for numeric and string variables as user-missing. User-missing values are then treated the same as the system-missing values.

Command for numeric variables
MISSING VALUES variable_name (missing_value) .
Command for string variables
MISSING VALUES variable_name ('missing_value') .

Multiple missing values are separated by commas, and a range of missing values may be declared using the keywords LO, LOWEST, HI, HIGHEST, and THRU.

To cancel previously declared missing values
MISSING VALUES variable_name ( ) .
To declare missing value(s) for all variables
MISSING VALUES ALL (missing_value) .

VARIABLE LABELS assigns descriptive labels to variables in the datafile.

Command
VARIABLE LABELS variable_name 'label'
/ variable_name 'label' .
Indent at least one space for continuing subcommands

To continue the label in the next line
VARIABLE LABELS variable_name 'label' +
'label_continued' .
Indent at least one space for continuing labels

NOTE: Each variable label can be up to 120 characters long, although most procedures print fewer than the 120 characters. All statistical procedures display at least 40 characters.

VALUE LABELS assigns descriptive labels to values of variables in the datafile.

Command
VALUE LABELS variable_name value 'label' value 'label'
/variable_name value 'label' value 'label' .

NOTE: VALUE LABELS deletes all existing value labels for the specified variable(s) and assigns new value labels. ADD VALUE LABELS can be used to add new labels or to alter labels for specified values without deleting other existing labels.

Data Management: COMPUTE, SELECT IF, SPLIT FILE and SORT CASES

COMPUTE creates new numeric variables or modifies the values of existing string or numeric variables.

Command:
COMPUTE new_variable = logical_expression .
Example:
COMPUTE NEWVAR = LOG(OLDVAR) .

For a list of functions available with the COMPUTE command, refer to the SPSS Base System Syntax Reference Guide Release 6.0 (pp. 143-147).

SELECT IF subsets cases for analysis based upon the logical_expression specified in the command. Refer to the SPSS Base System Syntax Reference Guide (p.723) for a list of operators and functions.

Command:
SELECT IF ( logical_expression ) .

NOTE: SELECT IF permanently selects cases.
For temporary case selection, specify a TEMPORARY commmand before SELECT IF:
TEMPORARY .
SELECT IF ( logical_expression ) .

SPLIT FILE divides the working data file into subgroups that can be analyzed separately. This command is useful when the same analysis has to repeated for several subgroups. Each value of each split variable is considered a break group, and cases within a break group must be grouped together in the working data file. If they are not, the SORT CASES command (see following section) must be used before SPLIT FILE to sort cases in the proper order.

Command:
SPLIT FILE BY variable(s)_with_which_to_divide_sample .
Command to turn off subgroup processing:
SPLIT FILE OFF .

NOTE: SPLIT FILE is in effect for all procedures in a session unless limited by a TEMPORARY command (see previous section on temporary selection of cases), turned OFF, or overridden by a new SPLIT FILE command.

SORT CASES reorders the sequence of cases in the working data file based on the values of one or more variables. Cases can be sorted in ascending ( A ) or descending ( D ) order of each variable, or use combinations of ascending and descending order for different variables.

Command:
SORT CASES BY variable_names ( A_or_D ) .
Default sort order is ascending ( A )

Saving Data in SPSS

Saving Data as SPSS data file
SAVE OUTFILE='path_and_filename.extension' .
/ KEEP_or_DROP=variable_names .
EXECUTE.
Use DROP or KEEP for retaining only some of the variables in the data file.

Saving Data as SPSS Portable file
EXPORT OUTFILE='path_and_filename.extension'.
/ KEEP_or_DROP=variable_names.
EXECUTE.
See note on DROP or KEEP above .

Note: SPSS portable data files are readable in SAS using the SPSS engine.

Saving data as free-format ASCII data
WRITE OUTFILE='path_and_filename.extension'
/ variable_names_or_ALL.
EXECUTE.
ALL writes out all variables in the active dataset.

Saving data as fixed-format ASCII data--one line of data per case
WRITE OUTFILE='path_and_filename.extension' FIXED
/ variable_names start_column-end_column (A_if_string_variable).
EXECUTE.

Saving data as fixed-format ASCII data--multiple lines of data per case
WRITE OUTFILE=' path_and_filename.extension ' RECORDS=n
n=number of lines of data per case
/1 variable_names start_column-end_column (A_if_string_variable)
/2 variable_names start_column-end_column (A_if_string_variable)
.
.
.
/last_n variable_names start_column-end_column (A_if_string_variable).
last_n=last line of data per case
EXECUTE.

Note: In saving data as ASCII data, SPSS writes out missing values as blanks (instead of '.'), unless missing values are previously declared as numeric values .

Moving SPSS Datasets between SPSS for Windows and SPSS for UNIX SPSS data files are the same for both platforms Windows and UNIX.
Make sure when using FTP you transfer them in binary mode.

If you have SPSS datasets on other systems like the DOS version of SPSS or the mainframe, you need to save them in portable format before you can use them in SPSS for Windows or UNIX.

Other Resources
SPSS INC Homepage

lm: June 28, 1999