54
Getting Started with SPSS Syntax Course Notes November 2008 Edition 1

Getting Started With SPSS Syntax

Embed Size (px)

Citation preview

Page 1: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 154

Getting

Started with

SPSS Syntax

Course Notes

November 2008

Edition 1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 254

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 354

Geing

sared wih

SPSS Snax

Edition 1 November 2008

Document Number 3637-2008

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 454

v

PrefaceThese are the course notes for SPSS 5 Starting with SPSS Syntax Course Code

1305 The notes are designed for use with the SPSS 14 Brief Guide which comes

with SPSS It can be downloaded from

hpwwwcsedackecs_docenaionDocens_b_Nber3639

A full list of course notes SPSS manuals and details of the courses but can be

dowloaded from

hpwwwcsedacksdcscaaogeISCaDaah

including the SPSS course information leaet which contains details of other SPSS

courses offered by IS skills datasets used during the courses and resources

available at the university The course builds on courses SPSS 1 Getting Started

with SPSS course code 1301 and SPSS 3 Changing Data in SPSS course code

1303 It is assumed you have attended that course or worked through the notes for

that course

Conventions

Choosing an item from a menu is indicated by ain en gt sben gt ie

For example Fie gt New gt Daa means open the Fie Menu from that choose

New sub menu and then choose Daa from the sub menu (the choice will open a

new data window)

This type of paragraph is

an action paragraph That is to say it indicates

something you can do in SPSS if you are following the notes on your

computer

This sort of paragraph is an information paragraph and will Lcontain points of note or warnings

Copyright copy IS 2008

Permission is granted to any individual or institution to use copy or redistribute this

document whole or in part so long as it is not sold for prot and provided that theabove copyright notice and this permission notice appear in all copies

Where any part of this document is included in another document due

acknowledgement is required

Module 5 Starting with SPSS Syntax

P r e f a c e

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 554

v

Module 5 Starting with SPSS Syntax

C on t en t s

Contents

Using SPSS CoandsUsing SPSS commands 2

The syntax window 6

Running an SPSS command 9

Typing in a command 10

The CROSSTABS command 10Slide Content from presentation 12

SPSS Coand StructureCommands Subcommands amp keywords 16

Abbreviating keywords 16

Reserved keywords 17

Lists of Variables 17

Syntax Charts 18

Some SPSS Command examples (from slides) 19

Try yourself 23

Synta for CacuationsPresentation slides for calculations 26

RECODE command 27

COMPUTE command 28

COUNT command 30

IF command 31

Filter command 33

DO REPEAT command 34

DO IF - END IF commands 35

Running Synta in BatcesProduction Facility 38

Running a job with SPSS Production facility 39

Output from an SPSS job 43

Output in html format - a web page 44

Logging commands and SPSS Journal 46

Try for yourself 47

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 654

v

C o n t e n t s ( c o n t i n u e d )

Module 5 Starting with SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 754

Session A

Using SPSSCommands

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 854

Module 5 Starting with SPSS Syntax

2

S e s s i o n A U

s i n g S P S S C o m m a n d s Using SPSS commands

SPSS command language lies behind any work you do with SPSS although since

most analyses can be done using the menus in the normal course of events you

need never encounter any SPSS commands directly In the dim and distant past the

only way to use SPSS was via its command language (also called syntax) In the

present using a default installation you will only see command names very briey at

the bottom of the window in the status bar while the relevant procedures are being

run by SPSS

However it is easy to see the underlying commands and to generate SPSS

commands from the interactive menus and dialog boxes SPSS commands are

stored in a Synta window which is a plain text le usually with a sps extension

Keeping command syntax gives you the ability to perform the same tasks again and

again

SPSS commands in the output viewer

You can choose to have the underlying commands displayed in a log along with the

output in the output viewer using the Viewer tab in Options from the Edit menu

Start up SPSS and choose Type in data so the data editor is empty

Choose Edi gt Opions to open the Options dialog box

Figure 1 Options dialog box (Genera tab showing)

Click on the Viewer tab to look at the Viewer options

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 954

Module 5 Starting with SPSS Syntax

3

S e s s i onA U s i n g S P S

S C omm an d s

Figure 2 Options dialog box (Viewer tab showing)

Tick Dispay coands in te og box under Initia Output State heading

to display commands Click OK or Appy

Figure 3 Options detail Dispay coands ticked

Once this feature has been switched on the underlying SPSS commands for

anything you do in SPSS will be pasted in the output window before the output

itself For example in Figure 4 you should see a GET command followed by the

DATASET command in the output window

Open the 1991 US Genera Socia Surveysav data le from the SPSS

directory (using Fie gt Open gt Daa) and look at the output window

Figure 4 Output viewer with GET command

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1054

Module 5 Starting with SPSS Syntax

4

S e s s i o n A U

s i n g S P S S C o m m a n d s The full GET command in the output is

getfile=EProgramFilesSPSS141991USGeneralSocialSurveysav

The GET command reads an SPSS data le into the data editor Its general form is-

G E T F I L E = phfl

Earlier versions of SPSS needed to have an ExECUTE command after the GET command so SPSS would run the GET

command and read the data into the data editor

The other command the DATASET command is used to name the active data

window Its not absolutely necessary but it looks like

D A T A S E T N A M E D a t a s e t 1 W I N D O W = F R O N T

Now we will run a FREQUENCIES procedure

Select Anaze gt Descripive Saisics gt Freqencies and

choose the variables shown in Figure 5

Figure 5 Frequencies dialog box

Click OK to run the Frequencies and look at the output

Figure 6 Frequencies command preceding Frequencies output

The FREQUENCIES command can be seen after the GET and DATASET

commands and before the title at the start of the Frequencies output

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1154

Module 5 Starting with SPSS Syntax

5

S e s s i onA U s i n g S P S

S C omm an d s

The FREQUENCIES command is-

F R E Q U E N C I E S V A R I A B L E S = h a p p y l i f e o r d e r = a n a l y s i s

In its simplest form all you need is a list of the variables-

F r e q u e n c i e s v a r i a b l e s = h a p p y l i f e

The commands in the output window merely document the SPSS session and cant be run from here To run commands we will

need use a syntax window to be discussed soon

Notes in output viewer

The other place you can see a report of the underlying commands is in the Notes

object produced every time you run something a procedure In the Viewer outline

to the left of the output as you look at it you can see a Notes object as part of every

procedures output although by default its contents are hidden Above Figure 6

shows the Notes object as closed or hidden Below in Figure 8 shows the Notes

object open to view

Double click on the Notes icon in the outline to show its contents

Figure 7 Notes from the Frequencies procedure

Notes are quite useful to check details if an SPSS procedure hasnrsquot worked as

you expected it to From the notes you can check to make sure you havent any

weights split le routines or any selection lters switched on You can check that

you have the correct le and syntax and there is a date and time stamp so you can

check when the output was produced

Showing and hiding output objects is discussed in the SPSS 1Getting Started with SPSS course Course Code 1301

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1254

Module 5 Starting with SPSS Syntax

6

S e s s i o n A U

s i n g S P S S C o m m a n d s The syntax window

The syntax window is used to store and run SPSS commands A default installation 1

of SPSS will not open a syntax window Each time you do something with the menus

whether it is to read in data or perform an analysis you have issued a command or

series of commands for SPSS to obey It is possible to paste the SPSS commands

underlying any dialog box into a syntax window using the Paste button - available

on most SPSS dialog boxes It will automatically open up a new syntax2 window

and paste the underlying command(s) from the dialog box into the new window The

contents of a syntax window can be saved to a le so the analysis can be rerun

later without using menus

Figure 8 Frequencies dialog box again

For example if we go back to the Frequencies procedure run earlier we can paste

the underlying FREQUENCIES command into a syntax window

Choose Anaze gt Descripive Saisics gt Freqencies

Click the Carts button and select a bar chart Click Continue to get back

to the Frequencies dialog box

Instead of pressing OK to run Frequencies click on Paste

Figure 9 Frequencies command in the syntax window

SPSS will automatically open up a syntax window and bring it to the front of the

screen with the FREQUENCIES command pasted into it

1 You can change this default using the Edi gt Opionshellip menu under the Gen-

era tab by ticking Open synta window at start-up

2 If a syntax window is already open it will put the command in it without bringing it

to the front

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1354

Module 5 Starting with SPSS Syntax

7

S e s s i onA U s i n g S P S

S C omm an d s

This is the FREQUENCIES command underlying the analysis we ran at the

beginning of the rst exercise You can see the FREQUENCIES command keyword

three subcommand keywords VARIABlES BARChART and ORDER separated

by and two other keywords FREQ and ANAlYSIS And like all commands its

nished with a full stop

To break the command down

Thebull VARIABlES subcommand lists the variables for which we want frequencies

in this example the variables appy and ife

Thebull BARChART subcommand requests a bar chart to be plotted for each of

the variables in the list FREQ species frequencies raw counts rather than

percents on the scale axis

Thebull ORDER subcommand with keyword ANAlYSIS species all the charts after

the tables If the VARIABlE keyword had been used instead the chart for a

particular variable would be placed next to its frequency table

If you want to see all the possible subcommands and other keywords you can use

with any command click the Sow synta cart button on the syntax window

toolbar to look at its syntax chart

Figure 10 The Sow synta cart button

Select the Frequencies command and click on the synta cart button to

see its syntax chart

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1454

Module 5 Starting with SPSS Syntax

8

S e s s i o n A U

s i n g S P S S C o m m a n d s Figure 11 FREQUENCIES syntax chart

As you can see the chart is quite complicated but will give you all the elements you

can put in a FREQUENCIES command You maybe able to spot the subcommands

and keywords just described

Each element in square brackets [ ] indicates a optional element a list of elements

aligned above one another in curly brackets indicates a choice of one out of that

series of elements default choice is usually given in bold All SPSS command sub

command and other keywords are presented in uppercase in chart (although you

dont need to type them in uppercase) Check the syntax reference guide or on-line

help for examples of how each command is used

Once you are nished having a look at the help information close the Helpwindow and you should go back to the Syntax window

The SPSS Command Syntax Reference guide will give you moredetails about SPSS commands and how to run them Syntax

guides are available on the SPSS manual CD or within SPSS in

PDF format from Hep gt Coand Snax Reference

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1554

Module 5 Starting with SPSS Syntax

9

S e s s i onA U s i n g S P S

S C omm an d s

Running an SPSS commandThe Run Current button on the syntax window tool bar looks like an audio play

button Its used to run the command currently selected in the syntax window It is

easy to run an already pasted command either click somewhere on the command

or highlight the entire command (including the full stop) and click the run button

Figure 12 Running the FREQUENCIES command

Click somewhere on the FREQUENCIES command and click the Run

button to execute the command

You will see the procedure being run as you did when you used the menu to run

Frequencies When the processor has nished - it shouldnt take very long - the

results will be stored in the output window as usual

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1654

Module 5 Starting with SPSS Syntax

10

S e s s i o n A U

s i n g S P S S C o m m a n d s Typing in a command

The Syntax window can be used on its own to run SPSS commands if you know

what the commands are (or have generated them from the menus) A new syntax

window can be opened by choosing Fie gt New gt Synta or an existing one can be

opened using Fie gt Open gt Synta A syntax window can be brought to the front by

choosing it from the Windows menu if it is already open

Select the Syntax window from the Window menuWindow gt 3 Snax1 ndash SPSS Snax Edior

The rst syntax window is usually called Synta1 and the 3 before the name

indicates its the third open SPSS window after the data editor and output window If

you have more windows open then the number might be different

The CROSSTABS command

The following CROSSTABS command will cross-tabulate the appy variable by the

ife variable from the social survey dataset

c r o s s t a b s h a p p y b y l i f e

It is a simple form of the CROSSTABS command with no subcommands

Type in the CROSSTABS command crosstabs appy by ife into the

Syntax window

Figure 13 CROSSTABS command selected

A full stop is needed to indicate the end of each SPSS commandIf you forget to nish a command with a full stop the command

may not run properly

Select the whole CROSSTABS command as shown above and click on the

Run button to run it

If you switched on Dispay coands in te og demonstrated on page 2 then

the commands are displayed with the output then you will see the CROSSTABS

command just before the output from Crosstabs

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1754

Module 5 Starting with SPSS Syntax

11

S e s s i onA U s i n g S P S

S C omm an d s

Figure 14 Start of Output from the CROSSTABS command

The CROSSTABS command has several subcommands try the syntax chart button

to see what they are They include

CEllS used to change the content of the table

BARChART to add a clustered bar chart for each layer

Figure 15 CROSSTABS using CEllS and BARChART subcommands

Try typing in and running the new command in Figure 15

Layers can be added to the table by adding another BY keyword follwed by the layer

variable or variable list For example

c r o s s t a b s h a p p y b y l i f e b y r e g i o n

will produce a crosstabs table layer for each region Try adding a layer to the latest

crosstabs command and run the command to check the effect

Some command names such as FINISh BEGIN DATA and

END DATA are complete commands which need no further

specication However most commands (including all of the statistical subprograms) have to have a certain amount

of specication and each has its own particular form of

specication so check the syntax guide before you run

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1854

Module 5 Starting with SPSS Syntax

12

S e s s i o n A U

s i n g S P S S C o m m a n d s Slide Content from presentation

SPSS Coand language

Starting with commands

Wy use Coands

More exible

Extra features

Can be saved

And repeated

Te Synta Window

SPSS commands are entered in the Syntax Window

Create a Syntax Window

Either selecting File gt New gt Syntax from the menu bar

Or creating le with sps extension in a text editor

Use an existing syntax window

Either Selecting an open syntax window from the Window menu

Or selecting File gt Open gt Syntax from the menu bar

SPSS coands

Commands are entered in the Syntax Window by

Either opening the syntax Window and typing directly

Or building up a command using dialog boxes then clicking the

PASTE button

Or using a mixture of the two

Forat of Coands

Control Field - consists of SPSS keyword

Specication Field - names of variables etc on which command willoperate

Full stop - to terminate command

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1954

Module 5 Starting with SPSS Syntax

13

S e s s i onA U s i n g S P S

S C omm an d s

Synta Rues

Commands start with a command keyword

Commands are case insensitive

Except for text in quotes

A single command can go over many syntax lines

Each line of syntax must not exceed 256 bytes3

Do not break a syntax line

in a variable name

in between quotes lsquo lsquo or ldquo ldquo

For a syntax window

Each command must start on a new line

Each command must end with a full stop with no non-blanks after it

or end with a completely blank line afterwards

For batc coands (see ast session)

Each command begins on a new line in column 1

unless you use + or -

Continuation lines mustnrsquot start in column 1

Full stops command terminators are optional

Te FINISh coand

FINISH

For batch commands

Terminates a le of SPSS commands and closes SPSS tidily

NB When SPSS windows manager is running (eg during an SPSS

for Windows session) FINISH will generate an error

Terminate an interactive SPSS session using Exit from the File menu

3 256 bytes are characters in single byte languageshellip

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2054

Module 5 Starting with SPSS Syntax

14

S e s s i o n A U

s i n g S P S S C o m m a n d s

Session summaryIn this session we covered

How to display SPSS commands in output window as they are runbull

How using thebull Paste button in an SPSS dialog box pastes the underlying

command into a synta window

The Syntax Window where SPSS commands (also calledbull synta) are stored

Using the syntax chart button to get help about a command in the syntax windowbull

How to type in and run an SPSS command from thebull synta window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2154

Session B

SPSS CommandStructure

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2254

Module 5 Starting with SPSS Syntax

16

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Commands Subcommands amp keywords

In general an SPSS command will be a mixture of different elements like this -

v kyw kyw kyw

An SPSS command starts with a recognized command keyword which has to

be spelt correctly Each command has its own general format with its own set of

subcommands and keywords These are combined with lists of variables names

or combinations of variable names and keywords to specify the analysis or action

required by the computer

Subcommands and other keywords are words recognized by SPSS as part of

the commands specication and so appear in the specication eld Some

SPSS procedures have subcommands which can appear either on their own or in

association with certain keywords As with commands subcommands and keywords

have to appear exactly as they are spelt

Figure 17 Different keywords in the Commands example

get FILE=mspssworkCARDIACsav

crosstabs chd B Y famhist CELLS=C O U N T R O W C O L U M N STATISTICS=C H I S Q

requencies VARIABLES=height cigaret

FORMAT=L I M I T (12 ) HIST

graPH SCATTERPLOT weight W I T H height

inisH

The different types of keyword have been highlighted as follows-

command keywords SUBCOMMAND keywords

O T H E R keywords variable names and other specication

Abbreviating keywords

SPSS will recognise most of its command and subcommand keywords when

abbreviated to the rst three or four characters of the name as long as the keywordsare unique So for example-

freq vars=height cigaret form=lim(12) hist

can be used as an abbreviation of-

frequencies variables=height cigaret

format=limit(12)

histogram

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2354

Module 5 SPSS command Structure

17

S e s s i onB S P S S C om

m an d S t r u c t ur e

Reserved keywords

With most procedures it does not matter if there is a variable name the same as a

keyword since there are certain places in a command specication where a variable

or variable list is expected and others where keywords are expected However there

are some reserved keywords which cannot be used as names These are

ALL AND BY EQ GE GT LE LT NE NOT OR TO WITH

These words cannot be used as variable names in SPSS because the program

already recognizes them as having meaning For example

FREQUENCIES VARIABLES=ALL

calculates a frequency table for all the variables currently stored on SPSSs

active le To have a variable called A L L would make the sense of the instruction

ambiguous to SPSS

Lists of Variables

The variables are kept in the SPSS active le in the order they are given in the

DATA lIST command and any variables generated during the session in the order

they were created This order can be utilized when instead of typing a long list of

consecutive variables you type the name of the rst variable then the TO keyword

and then the name of the last variable in the list These inclusive ranges of variables

can be combined with the names of other variables to produce a larger variable list

where needed For example if we had the following variables in a system le-

LIST OF VARIABLES ON THE ACTIVE FILE

1 AGE 6 HHLDTYPE 11 OCPENAMT 16 SPSCLASS

2 CHRONILL 7 HOMEHELP 12 REBTOWN 17 TOTINCID

3 CLASS 8 KINNEAR 13 REBTRENT 18 UNEARNED

4 EMPLOYED 9 MARITAL 14 GENDER 19 CASWGT

5 HEALTH 10 NIRAMT 15 SPSAGE

In SPSS variable lists individual variable names andor variable ranges can

be separated by either spaces or commas However any keywords have to be

separated from variable names by spaces ie use spaces with TO or All

AGE TO CASWGT implies

AGE CHRONILL CLASS EMPLOYED HEALTH HHLDTYPEHOMEHELP KINNEAR MARITAL NIRAMT OCPENAMT REBTOWNREBTRENT GENDER SPSAGE SPSCLASS TOTINCID UNEARNEDCASWGT

This rst list is all the variables in the active le since AGE is the rst variable on the

list and CASWGT is the last

hhlDTYPE TO GENDER implies

HHLDTYPE HOMEHELP KINNEAR MARITAL NIRAMT OCPENAMTREBTOWN REBTRENT GENDER

Notice in this list 7 of the 9 variables are implied in the same order as the active le

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2454

Module 5 Starting with SPSS Syntax

18

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e ClASS hEAlTh TO NIRAmT implies

CLASS HEALTH HHLDTYPE HOMEHELP KINNEAR MARITALNIRAMT

A list can be combined with individual variables

AGE SPAGE ClASS SPClASS All implies

AGE SPSAGE CLASS SPSCLASS CHRONILLEMPLOYED HEALTH HHLDTYPE HOMEHELP KINNEARMARITAL NIRAMT OCPENAMT REBTOWN REBTRENT GENDERTOTINCID UNEARNED CASWGT

The reserved keyword ALL in this context means all the variables in the active le

It can be used in a variable list for the KEEP subcommand on a GET or SAVE

command to change the order the variables are saved in whilst making sure no

variables are missed Variables not specically listed will appear after all the listed

ones in active le order

If new variables are created during a session they will be added to the active le in

the order they were created after the variables already stored in the active le You

can always check the variable order in the active le using the DISPlAY command -

it may not be the same order they will appear in dialog boxes

Syntax Charts

The Sow synta cart button as seen in the previous session in Figure 11 will let

you look at command syntax charts If you have no command selected in the syntax

window then the button will give you an alphabetic list of all the command keywords

Look at the syntax chart for each of these commands to see what subcommands and keywords are available

Use the Search button to look for more information about each

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2554

Module 5 SPSS command Structure

19

S e s s i onB S P S S C om

m an d S t r u c t ur e

Some SPSS Command examples (from slides)

Te GET coand

The GET command reads a sav le into SPSS

GET FILE = cardiacsav

Te GET coand - KEEP and DROP subcoands

Use the KEEP subcommand to select only some of the variables

GET FILE=cardiacsavKEEP=famhist chd

Use the DROP subcommand to select all of the le except certain

variables

GET FILE=cardiacsavDROP=height weight

Te DATASET NAmE coand

The DATASET NAME command give the active dataset a short name

which can be used in later commands

GET FILE = mspssworkcardiacsav

DATASET NAME cardiac

Te SAVE coand

Use the SAVE command to save your data in a sav le

SAVE OUTFILE=myfilesav

Te SAVE coand wi

save all your cases together with variable names values

missing values variable and value labels and formats

Saved in the state they are in when the command is issued

Any further changes can only be saved with another SAVE command

It will NOT save the results of any analyses results are saved in an

output window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2654

Module 5 Starting with SPSS Syntax

20

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Te SAVE coand KEEP and DROP subcoands

The KEEP and DROP subcommands are available with the SAVE

command and perform the same function as with GET

Using KEEP will save only the named variables

SAVE OUTFILE=filesav KEEP=AGE HEIGHT

Using DROP will save all variables except named ones (here it means

all variables except WEIGHT2)

SAVE OUTFILE=filesav DROP=WEIGHT2

FREQUENCIES coand

FREQUENCIES VARIABLES=HEIGHT

FREQUENCIES - STATISTICS subcoand

FREQ VAR=HEIGHT AGE STATISTICS

Gives four default statistics

MEAN STDDEV MINIMUM MAXIMUM

FREQUENCIES - more statistics

MEDIAN MODE SUM RANGE SEMEAN VARIANCESKEWNESS SESKEW KURTOSIS SEKURT

STATISTICS=DEFAULT

Use to get the default four statistics MEAN STDDEV MINIMUM

MAXIMUM together with others you ask for

STATISTICS=ALL

Gives all available statistics - can be wasteful

STATISTICS=NONE

Equivalent to omitting STATISTICS altogether

FREQUENCIES - FORmAT subcoand

To suppress the frequency table use

FORMAT=NOTABLE

To suppress tables over a certain length

FORMAT=LIMIT(50)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2754

Module 5 SPSS command Structure

21

S e s s i onB S P S S C om

m an d S t r u c t ur e

FREQUENCIES - subcoands for carts

There are three subcommands to produce type of charts named

HISTOGRAM

BARCHART

PIECHART

Using ore tan one subcoand

To use more than one subcommand string them together and nish

the command with the terminating full stop

FREQUENCIES VARIABLES=AGESEX STATISTICS=RANGEBARCHART

CROSSTABS coand

CROSSTABS TABLES=AGE BY SEX

CROSSTABS AGE BY SEX

SEX BY WEIGHT

The next three crosstabs commands produce the same three tables

CROSSTABS AGE BY SEX

AGE BY PARTY

AGE BY INCOME

CROSSTABS AGE BY SEX PARTY INCOMECROSSTABS AGE BY SEX TO INCOME

CROSSTABS - STATISTICS subcoand

CROSSTABS HEIGHT BY AGE

STATISTICS

Gives the Chi-square statistic tests

CROSSTABS - more statistics

Others available

CHISQ PHI CC LAMBDA UC BTAU CTAU GAMMA DETA CORR ALL NONE

and KAPPA RISK MCNEMAR CMH(1)

Usage

CROSSTABS TABLES=AGE BY WEIGHT

STATISTICS=BTAU CORR

Only gives those statistics asked for

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 2: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 254

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 354

Geing

sared wih

SPSS Snax

Edition 1 November 2008

Document Number 3637-2008

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 454

v

PrefaceThese are the course notes for SPSS 5 Starting with SPSS Syntax Course Code

1305 The notes are designed for use with the SPSS 14 Brief Guide which comes

with SPSS It can be downloaded from

hpwwwcsedackecs_docenaionDocens_b_Nber3639

A full list of course notes SPSS manuals and details of the courses but can be

dowloaded from

hpwwwcsedacksdcscaaogeISCaDaah

including the SPSS course information leaet which contains details of other SPSS

courses offered by IS skills datasets used during the courses and resources

available at the university The course builds on courses SPSS 1 Getting Started

with SPSS course code 1301 and SPSS 3 Changing Data in SPSS course code

1303 It is assumed you have attended that course or worked through the notes for

that course

Conventions

Choosing an item from a menu is indicated by ain en gt sben gt ie

For example Fie gt New gt Daa means open the Fie Menu from that choose

New sub menu and then choose Daa from the sub menu (the choice will open a

new data window)

This type of paragraph is

an action paragraph That is to say it indicates

something you can do in SPSS if you are following the notes on your

computer

This sort of paragraph is an information paragraph and will Lcontain points of note or warnings

Copyright copy IS 2008

Permission is granted to any individual or institution to use copy or redistribute this

document whole or in part so long as it is not sold for prot and provided that theabove copyright notice and this permission notice appear in all copies

Where any part of this document is included in another document due

acknowledgement is required

Module 5 Starting with SPSS Syntax

P r e f a c e

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 554

v

Module 5 Starting with SPSS Syntax

C on t en t s

Contents

Using SPSS CoandsUsing SPSS commands 2

The syntax window 6

Running an SPSS command 9

Typing in a command 10

The CROSSTABS command 10Slide Content from presentation 12

SPSS Coand StructureCommands Subcommands amp keywords 16

Abbreviating keywords 16

Reserved keywords 17

Lists of Variables 17

Syntax Charts 18

Some SPSS Command examples (from slides) 19

Try yourself 23

Synta for CacuationsPresentation slides for calculations 26

RECODE command 27

COMPUTE command 28

COUNT command 30

IF command 31

Filter command 33

DO REPEAT command 34

DO IF - END IF commands 35

Running Synta in BatcesProduction Facility 38

Running a job with SPSS Production facility 39

Output from an SPSS job 43

Output in html format - a web page 44

Logging commands and SPSS Journal 46

Try for yourself 47

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 654

v

C o n t e n t s ( c o n t i n u e d )

Module 5 Starting with SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 754

Session A

Using SPSSCommands

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 854

Module 5 Starting with SPSS Syntax

2

S e s s i o n A U

s i n g S P S S C o m m a n d s Using SPSS commands

SPSS command language lies behind any work you do with SPSS although since

most analyses can be done using the menus in the normal course of events you

need never encounter any SPSS commands directly In the dim and distant past the

only way to use SPSS was via its command language (also called syntax) In the

present using a default installation you will only see command names very briey at

the bottom of the window in the status bar while the relevant procedures are being

run by SPSS

However it is easy to see the underlying commands and to generate SPSS

commands from the interactive menus and dialog boxes SPSS commands are

stored in a Synta window which is a plain text le usually with a sps extension

Keeping command syntax gives you the ability to perform the same tasks again and

again

SPSS commands in the output viewer

You can choose to have the underlying commands displayed in a log along with the

output in the output viewer using the Viewer tab in Options from the Edit menu

Start up SPSS and choose Type in data so the data editor is empty

Choose Edi gt Opions to open the Options dialog box

Figure 1 Options dialog box (Genera tab showing)

Click on the Viewer tab to look at the Viewer options

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 954

Module 5 Starting with SPSS Syntax

3

S e s s i onA U s i n g S P S

S C omm an d s

Figure 2 Options dialog box (Viewer tab showing)

Tick Dispay coands in te og box under Initia Output State heading

to display commands Click OK or Appy

Figure 3 Options detail Dispay coands ticked

Once this feature has been switched on the underlying SPSS commands for

anything you do in SPSS will be pasted in the output window before the output

itself For example in Figure 4 you should see a GET command followed by the

DATASET command in the output window

Open the 1991 US Genera Socia Surveysav data le from the SPSS

directory (using Fie gt Open gt Daa) and look at the output window

Figure 4 Output viewer with GET command

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1054

Module 5 Starting with SPSS Syntax

4

S e s s i o n A U

s i n g S P S S C o m m a n d s The full GET command in the output is

getfile=EProgramFilesSPSS141991USGeneralSocialSurveysav

The GET command reads an SPSS data le into the data editor Its general form is-

G E T F I L E = phfl

Earlier versions of SPSS needed to have an ExECUTE command after the GET command so SPSS would run the GET

command and read the data into the data editor

The other command the DATASET command is used to name the active data

window Its not absolutely necessary but it looks like

D A T A S E T N A M E D a t a s e t 1 W I N D O W = F R O N T

Now we will run a FREQUENCIES procedure

Select Anaze gt Descripive Saisics gt Freqencies and

choose the variables shown in Figure 5

Figure 5 Frequencies dialog box

Click OK to run the Frequencies and look at the output

Figure 6 Frequencies command preceding Frequencies output

The FREQUENCIES command can be seen after the GET and DATASET

commands and before the title at the start of the Frequencies output

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1154

Module 5 Starting with SPSS Syntax

5

S e s s i onA U s i n g S P S

S C omm an d s

The FREQUENCIES command is-

F R E Q U E N C I E S V A R I A B L E S = h a p p y l i f e o r d e r = a n a l y s i s

In its simplest form all you need is a list of the variables-

F r e q u e n c i e s v a r i a b l e s = h a p p y l i f e

The commands in the output window merely document the SPSS session and cant be run from here To run commands we will

need use a syntax window to be discussed soon

Notes in output viewer

The other place you can see a report of the underlying commands is in the Notes

object produced every time you run something a procedure In the Viewer outline

to the left of the output as you look at it you can see a Notes object as part of every

procedures output although by default its contents are hidden Above Figure 6

shows the Notes object as closed or hidden Below in Figure 8 shows the Notes

object open to view

Double click on the Notes icon in the outline to show its contents

Figure 7 Notes from the Frequencies procedure

Notes are quite useful to check details if an SPSS procedure hasnrsquot worked as

you expected it to From the notes you can check to make sure you havent any

weights split le routines or any selection lters switched on You can check that

you have the correct le and syntax and there is a date and time stamp so you can

check when the output was produced

Showing and hiding output objects is discussed in the SPSS 1Getting Started with SPSS course Course Code 1301

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1254

Module 5 Starting with SPSS Syntax

6

S e s s i o n A U

s i n g S P S S C o m m a n d s The syntax window

The syntax window is used to store and run SPSS commands A default installation 1

of SPSS will not open a syntax window Each time you do something with the menus

whether it is to read in data or perform an analysis you have issued a command or

series of commands for SPSS to obey It is possible to paste the SPSS commands

underlying any dialog box into a syntax window using the Paste button - available

on most SPSS dialog boxes It will automatically open up a new syntax2 window

and paste the underlying command(s) from the dialog box into the new window The

contents of a syntax window can be saved to a le so the analysis can be rerun

later without using menus

Figure 8 Frequencies dialog box again

For example if we go back to the Frequencies procedure run earlier we can paste

the underlying FREQUENCIES command into a syntax window

Choose Anaze gt Descripive Saisics gt Freqencies

Click the Carts button and select a bar chart Click Continue to get back

to the Frequencies dialog box

Instead of pressing OK to run Frequencies click on Paste

Figure 9 Frequencies command in the syntax window

SPSS will automatically open up a syntax window and bring it to the front of the

screen with the FREQUENCIES command pasted into it

1 You can change this default using the Edi gt Opionshellip menu under the Gen-

era tab by ticking Open synta window at start-up

2 If a syntax window is already open it will put the command in it without bringing it

to the front

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1354

Module 5 Starting with SPSS Syntax

7

S e s s i onA U s i n g S P S

S C omm an d s

This is the FREQUENCIES command underlying the analysis we ran at the

beginning of the rst exercise You can see the FREQUENCIES command keyword

three subcommand keywords VARIABlES BARChART and ORDER separated

by and two other keywords FREQ and ANAlYSIS And like all commands its

nished with a full stop

To break the command down

Thebull VARIABlES subcommand lists the variables for which we want frequencies

in this example the variables appy and ife

Thebull BARChART subcommand requests a bar chart to be plotted for each of

the variables in the list FREQ species frequencies raw counts rather than

percents on the scale axis

Thebull ORDER subcommand with keyword ANAlYSIS species all the charts after

the tables If the VARIABlE keyword had been used instead the chart for a

particular variable would be placed next to its frequency table

If you want to see all the possible subcommands and other keywords you can use

with any command click the Sow synta cart button on the syntax window

toolbar to look at its syntax chart

Figure 10 The Sow synta cart button

Select the Frequencies command and click on the synta cart button to

see its syntax chart

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1454

Module 5 Starting with SPSS Syntax

8

S e s s i o n A U

s i n g S P S S C o m m a n d s Figure 11 FREQUENCIES syntax chart

As you can see the chart is quite complicated but will give you all the elements you

can put in a FREQUENCIES command You maybe able to spot the subcommands

and keywords just described

Each element in square brackets [ ] indicates a optional element a list of elements

aligned above one another in curly brackets indicates a choice of one out of that

series of elements default choice is usually given in bold All SPSS command sub

command and other keywords are presented in uppercase in chart (although you

dont need to type them in uppercase) Check the syntax reference guide or on-line

help for examples of how each command is used

Once you are nished having a look at the help information close the Helpwindow and you should go back to the Syntax window

The SPSS Command Syntax Reference guide will give you moredetails about SPSS commands and how to run them Syntax

guides are available on the SPSS manual CD or within SPSS in

PDF format from Hep gt Coand Snax Reference

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1554

Module 5 Starting with SPSS Syntax

9

S e s s i onA U s i n g S P S

S C omm an d s

Running an SPSS commandThe Run Current button on the syntax window tool bar looks like an audio play

button Its used to run the command currently selected in the syntax window It is

easy to run an already pasted command either click somewhere on the command

or highlight the entire command (including the full stop) and click the run button

Figure 12 Running the FREQUENCIES command

Click somewhere on the FREQUENCIES command and click the Run

button to execute the command

You will see the procedure being run as you did when you used the menu to run

Frequencies When the processor has nished - it shouldnt take very long - the

results will be stored in the output window as usual

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1654

Module 5 Starting with SPSS Syntax

10

S e s s i o n A U

s i n g S P S S C o m m a n d s Typing in a command

The Syntax window can be used on its own to run SPSS commands if you know

what the commands are (or have generated them from the menus) A new syntax

window can be opened by choosing Fie gt New gt Synta or an existing one can be

opened using Fie gt Open gt Synta A syntax window can be brought to the front by

choosing it from the Windows menu if it is already open

Select the Syntax window from the Window menuWindow gt 3 Snax1 ndash SPSS Snax Edior

The rst syntax window is usually called Synta1 and the 3 before the name

indicates its the third open SPSS window after the data editor and output window If

you have more windows open then the number might be different

The CROSSTABS command

The following CROSSTABS command will cross-tabulate the appy variable by the

ife variable from the social survey dataset

c r o s s t a b s h a p p y b y l i f e

It is a simple form of the CROSSTABS command with no subcommands

Type in the CROSSTABS command crosstabs appy by ife into the

Syntax window

Figure 13 CROSSTABS command selected

A full stop is needed to indicate the end of each SPSS commandIf you forget to nish a command with a full stop the command

may not run properly

Select the whole CROSSTABS command as shown above and click on the

Run button to run it

If you switched on Dispay coands in te og demonstrated on page 2 then

the commands are displayed with the output then you will see the CROSSTABS

command just before the output from Crosstabs

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1754

Module 5 Starting with SPSS Syntax

11

S e s s i onA U s i n g S P S

S C omm an d s

Figure 14 Start of Output from the CROSSTABS command

The CROSSTABS command has several subcommands try the syntax chart button

to see what they are They include

CEllS used to change the content of the table

BARChART to add a clustered bar chart for each layer

Figure 15 CROSSTABS using CEllS and BARChART subcommands

Try typing in and running the new command in Figure 15

Layers can be added to the table by adding another BY keyword follwed by the layer

variable or variable list For example

c r o s s t a b s h a p p y b y l i f e b y r e g i o n

will produce a crosstabs table layer for each region Try adding a layer to the latest

crosstabs command and run the command to check the effect

Some command names such as FINISh BEGIN DATA and

END DATA are complete commands which need no further

specication However most commands (including all of the statistical subprograms) have to have a certain amount

of specication and each has its own particular form of

specication so check the syntax guide before you run

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1854

Module 5 Starting with SPSS Syntax

12

S e s s i o n A U

s i n g S P S S C o m m a n d s Slide Content from presentation

SPSS Coand language

Starting with commands

Wy use Coands

More exible

Extra features

Can be saved

And repeated

Te Synta Window

SPSS commands are entered in the Syntax Window

Create a Syntax Window

Either selecting File gt New gt Syntax from the menu bar

Or creating le with sps extension in a text editor

Use an existing syntax window

Either Selecting an open syntax window from the Window menu

Or selecting File gt Open gt Syntax from the menu bar

SPSS coands

Commands are entered in the Syntax Window by

Either opening the syntax Window and typing directly

Or building up a command using dialog boxes then clicking the

PASTE button

Or using a mixture of the two

Forat of Coands

Control Field - consists of SPSS keyword

Specication Field - names of variables etc on which command willoperate

Full stop - to terminate command

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1954

Module 5 Starting with SPSS Syntax

13

S e s s i onA U s i n g S P S

S C omm an d s

Synta Rues

Commands start with a command keyword

Commands are case insensitive

Except for text in quotes

A single command can go over many syntax lines

Each line of syntax must not exceed 256 bytes3

Do not break a syntax line

in a variable name

in between quotes lsquo lsquo or ldquo ldquo

For a syntax window

Each command must start on a new line

Each command must end with a full stop with no non-blanks after it

or end with a completely blank line afterwards

For batc coands (see ast session)

Each command begins on a new line in column 1

unless you use + or -

Continuation lines mustnrsquot start in column 1

Full stops command terminators are optional

Te FINISh coand

FINISH

For batch commands

Terminates a le of SPSS commands and closes SPSS tidily

NB When SPSS windows manager is running (eg during an SPSS

for Windows session) FINISH will generate an error

Terminate an interactive SPSS session using Exit from the File menu

3 256 bytes are characters in single byte languageshellip

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2054

Module 5 Starting with SPSS Syntax

14

S e s s i o n A U

s i n g S P S S C o m m a n d s

Session summaryIn this session we covered

How to display SPSS commands in output window as they are runbull

How using thebull Paste button in an SPSS dialog box pastes the underlying

command into a synta window

The Syntax Window where SPSS commands (also calledbull synta) are stored

Using the syntax chart button to get help about a command in the syntax windowbull

How to type in and run an SPSS command from thebull synta window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2154

Session B

SPSS CommandStructure

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2254

Module 5 Starting with SPSS Syntax

16

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Commands Subcommands amp keywords

In general an SPSS command will be a mixture of different elements like this -

v kyw kyw kyw

An SPSS command starts with a recognized command keyword which has to

be spelt correctly Each command has its own general format with its own set of

subcommands and keywords These are combined with lists of variables names

or combinations of variable names and keywords to specify the analysis or action

required by the computer

Subcommands and other keywords are words recognized by SPSS as part of

the commands specication and so appear in the specication eld Some

SPSS procedures have subcommands which can appear either on their own or in

association with certain keywords As with commands subcommands and keywords

have to appear exactly as they are spelt

Figure 17 Different keywords in the Commands example

get FILE=mspssworkCARDIACsav

crosstabs chd B Y famhist CELLS=C O U N T R O W C O L U M N STATISTICS=C H I S Q

requencies VARIABLES=height cigaret

FORMAT=L I M I T (12 ) HIST

graPH SCATTERPLOT weight W I T H height

inisH

The different types of keyword have been highlighted as follows-

command keywords SUBCOMMAND keywords

O T H E R keywords variable names and other specication

Abbreviating keywords

SPSS will recognise most of its command and subcommand keywords when

abbreviated to the rst three or four characters of the name as long as the keywordsare unique So for example-

freq vars=height cigaret form=lim(12) hist

can be used as an abbreviation of-

frequencies variables=height cigaret

format=limit(12)

histogram

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2354

Module 5 SPSS command Structure

17

S e s s i onB S P S S C om

m an d S t r u c t ur e

Reserved keywords

With most procedures it does not matter if there is a variable name the same as a

keyword since there are certain places in a command specication where a variable

or variable list is expected and others where keywords are expected However there

are some reserved keywords which cannot be used as names These are

ALL AND BY EQ GE GT LE LT NE NOT OR TO WITH

These words cannot be used as variable names in SPSS because the program

already recognizes them as having meaning For example

FREQUENCIES VARIABLES=ALL

calculates a frequency table for all the variables currently stored on SPSSs

active le To have a variable called A L L would make the sense of the instruction

ambiguous to SPSS

Lists of Variables

The variables are kept in the SPSS active le in the order they are given in the

DATA lIST command and any variables generated during the session in the order

they were created This order can be utilized when instead of typing a long list of

consecutive variables you type the name of the rst variable then the TO keyword

and then the name of the last variable in the list These inclusive ranges of variables

can be combined with the names of other variables to produce a larger variable list

where needed For example if we had the following variables in a system le-

LIST OF VARIABLES ON THE ACTIVE FILE

1 AGE 6 HHLDTYPE 11 OCPENAMT 16 SPSCLASS

2 CHRONILL 7 HOMEHELP 12 REBTOWN 17 TOTINCID

3 CLASS 8 KINNEAR 13 REBTRENT 18 UNEARNED

4 EMPLOYED 9 MARITAL 14 GENDER 19 CASWGT

5 HEALTH 10 NIRAMT 15 SPSAGE

In SPSS variable lists individual variable names andor variable ranges can

be separated by either spaces or commas However any keywords have to be

separated from variable names by spaces ie use spaces with TO or All

AGE TO CASWGT implies

AGE CHRONILL CLASS EMPLOYED HEALTH HHLDTYPEHOMEHELP KINNEAR MARITAL NIRAMT OCPENAMT REBTOWNREBTRENT GENDER SPSAGE SPSCLASS TOTINCID UNEARNEDCASWGT

This rst list is all the variables in the active le since AGE is the rst variable on the

list and CASWGT is the last

hhlDTYPE TO GENDER implies

HHLDTYPE HOMEHELP KINNEAR MARITAL NIRAMT OCPENAMTREBTOWN REBTRENT GENDER

Notice in this list 7 of the 9 variables are implied in the same order as the active le

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2454

Module 5 Starting with SPSS Syntax

18

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e ClASS hEAlTh TO NIRAmT implies

CLASS HEALTH HHLDTYPE HOMEHELP KINNEAR MARITALNIRAMT

A list can be combined with individual variables

AGE SPAGE ClASS SPClASS All implies

AGE SPSAGE CLASS SPSCLASS CHRONILLEMPLOYED HEALTH HHLDTYPE HOMEHELP KINNEARMARITAL NIRAMT OCPENAMT REBTOWN REBTRENT GENDERTOTINCID UNEARNED CASWGT

The reserved keyword ALL in this context means all the variables in the active le

It can be used in a variable list for the KEEP subcommand on a GET or SAVE

command to change the order the variables are saved in whilst making sure no

variables are missed Variables not specically listed will appear after all the listed

ones in active le order

If new variables are created during a session they will be added to the active le in

the order they were created after the variables already stored in the active le You

can always check the variable order in the active le using the DISPlAY command -

it may not be the same order they will appear in dialog boxes

Syntax Charts

The Sow synta cart button as seen in the previous session in Figure 11 will let

you look at command syntax charts If you have no command selected in the syntax

window then the button will give you an alphabetic list of all the command keywords

Look at the syntax chart for each of these commands to see what subcommands and keywords are available

Use the Search button to look for more information about each

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2554

Module 5 SPSS command Structure

19

S e s s i onB S P S S C om

m an d S t r u c t ur e

Some SPSS Command examples (from slides)

Te GET coand

The GET command reads a sav le into SPSS

GET FILE = cardiacsav

Te GET coand - KEEP and DROP subcoands

Use the KEEP subcommand to select only some of the variables

GET FILE=cardiacsavKEEP=famhist chd

Use the DROP subcommand to select all of the le except certain

variables

GET FILE=cardiacsavDROP=height weight

Te DATASET NAmE coand

The DATASET NAME command give the active dataset a short name

which can be used in later commands

GET FILE = mspssworkcardiacsav

DATASET NAME cardiac

Te SAVE coand

Use the SAVE command to save your data in a sav le

SAVE OUTFILE=myfilesav

Te SAVE coand wi

save all your cases together with variable names values

missing values variable and value labels and formats

Saved in the state they are in when the command is issued

Any further changes can only be saved with another SAVE command

It will NOT save the results of any analyses results are saved in an

output window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2654

Module 5 Starting with SPSS Syntax

20

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Te SAVE coand KEEP and DROP subcoands

The KEEP and DROP subcommands are available with the SAVE

command and perform the same function as with GET

Using KEEP will save only the named variables

SAVE OUTFILE=filesav KEEP=AGE HEIGHT

Using DROP will save all variables except named ones (here it means

all variables except WEIGHT2)

SAVE OUTFILE=filesav DROP=WEIGHT2

FREQUENCIES coand

FREQUENCIES VARIABLES=HEIGHT

FREQUENCIES - STATISTICS subcoand

FREQ VAR=HEIGHT AGE STATISTICS

Gives four default statistics

MEAN STDDEV MINIMUM MAXIMUM

FREQUENCIES - more statistics

MEDIAN MODE SUM RANGE SEMEAN VARIANCESKEWNESS SESKEW KURTOSIS SEKURT

STATISTICS=DEFAULT

Use to get the default four statistics MEAN STDDEV MINIMUM

MAXIMUM together with others you ask for

STATISTICS=ALL

Gives all available statistics - can be wasteful

STATISTICS=NONE

Equivalent to omitting STATISTICS altogether

FREQUENCIES - FORmAT subcoand

To suppress the frequency table use

FORMAT=NOTABLE

To suppress tables over a certain length

FORMAT=LIMIT(50)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2754

Module 5 SPSS command Structure

21

S e s s i onB S P S S C om

m an d S t r u c t ur e

FREQUENCIES - subcoands for carts

There are three subcommands to produce type of charts named

HISTOGRAM

BARCHART

PIECHART

Using ore tan one subcoand

To use more than one subcommand string them together and nish

the command with the terminating full stop

FREQUENCIES VARIABLES=AGESEX STATISTICS=RANGEBARCHART

CROSSTABS coand

CROSSTABS TABLES=AGE BY SEX

CROSSTABS AGE BY SEX

SEX BY WEIGHT

The next three crosstabs commands produce the same three tables

CROSSTABS AGE BY SEX

AGE BY PARTY

AGE BY INCOME

CROSSTABS AGE BY SEX PARTY INCOMECROSSTABS AGE BY SEX TO INCOME

CROSSTABS - STATISTICS subcoand

CROSSTABS HEIGHT BY AGE

STATISTICS

Gives the Chi-square statistic tests

CROSSTABS - more statistics

Others available

CHISQ PHI CC LAMBDA UC BTAU CTAU GAMMA DETA CORR ALL NONE

and KAPPA RISK MCNEMAR CMH(1)

Usage

CROSSTABS TABLES=AGE BY WEIGHT

STATISTICS=BTAU CORR

Only gives those statistics asked for

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 3: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 354

Geing

sared wih

SPSS Snax

Edition 1 November 2008

Document Number 3637-2008

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 454

v

PrefaceThese are the course notes for SPSS 5 Starting with SPSS Syntax Course Code

1305 The notes are designed for use with the SPSS 14 Brief Guide which comes

with SPSS It can be downloaded from

hpwwwcsedackecs_docenaionDocens_b_Nber3639

A full list of course notes SPSS manuals and details of the courses but can be

dowloaded from

hpwwwcsedacksdcscaaogeISCaDaah

including the SPSS course information leaet which contains details of other SPSS

courses offered by IS skills datasets used during the courses and resources

available at the university The course builds on courses SPSS 1 Getting Started

with SPSS course code 1301 and SPSS 3 Changing Data in SPSS course code

1303 It is assumed you have attended that course or worked through the notes for

that course

Conventions

Choosing an item from a menu is indicated by ain en gt sben gt ie

For example Fie gt New gt Daa means open the Fie Menu from that choose

New sub menu and then choose Daa from the sub menu (the choice will open a

new data window)

This type of paragraph is

an action paragraph That is to say it indicates

something you can do in SPSS if you are following the notes on your

computer

This sort of paragraph is an information paragraph and will Lcontain points of note or warnings

Copyright copy IS 2008

Permission is granted to any individual or institution to use copy or redistribute this

document whole or in part so long as it is not sold for prot and provided that theabove copyright notice and this permission notice appear in all copies

Where any part of this document is included in another document due

acknowledgement is required

Module 5 Starting with SPSS Syntax

P r e f a c e

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 554

v

Module 5 Starting with SPSS Syntax

C on t en t s

Contents

Using SPSS CoandsUsing SPSS commands 2

The syntax window 6

Running an SPSS command 9

Typing in a command 10

The CROSSTABS command 10Slide Content from presentation 12

SPSS Coand StructureCommands Subcommands amp keywords 16

Abbreviating keywords 16

Reserved keywords 17

Lists of Variables 17

Syntax Charts 18

Some SPSS Command examples (from slides) 19

Try yourself 23

Synta for CacuationsPresentation slides for calculations 26

RECODE command 27

COMPUTE command 28

COUNT command 30

IF command 31

Filter command 33

DO REPEAT command 34

DO IF - END IF commands 35

Running Synta in BatcesProduction Facility 38

Running a job with SPSS Production facility 39

Output from an SPSS job 43

Output in html format - a web page 44

Logging commands and SPSS Journal 46

Try for yourself 47

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 654

v

C o n t e n t s ( c o n t i n u e d )

Module 5 Starting with SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 754

Session A

Using SPSSCommands

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 854

Module 5 Starting with SPSS Syntax

2

S e s s i o n A U

s i n g S P S S C o m m a n d s Using SPSS commands

SPSS command language lies behind any work you do with SPSS although since

most analyses can be done using the menus in the normal course of events you

need never encounter any SPSS commands directly In the dim and distant past the

only way to use SPSS was via its command language (also called syntax) In the

present using a default installation you will only see command names very briey at

the bottom of the window in the status bar while the relevant procedures are being

run by SPSS

However it is easy to see the underlying commands and to generate SPSS

commands from the interactive menus and dialog boxes SPSS commands are

stored in a Synta window which is a plain text le usually with a sps extension

Keeping command syntax gives you the ability to perform the same tasks again and

again

SPSS commands in the output viewer

You can choose to have the underlying commands displayed in a log along with the

output in the output viewer using the Viewer tab in Options from the Edit menu

Start up SPSS and choose Type in data so the data editor is empty

Choose Edi gt Opions to open the Options dialog box

Figure 1 Options dialog box (Genera tab showing)

Click on the Viewer tab to look at the Viewer options

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 954

Module 5 Starting with SPSS Syntax

3

S e s s i onA U s i n g S P S

S C omm an d s

Figure 2 Options dialog box (Viewer tab showing)

Tick Dispay coands in te og box under Initia Output State heading

to display commands Click OK or Appy

Figure 3 Options detail Dispay coands ticked

Once this feature has been switched on the underlying SPSS commands for

anything you do in SPSS will be pasted in the output window before the output

itself For example in Figure 4 you should see a GET command followed by the

DATASET command in the output window

Open the 1991 US Genera Socia Surveysav data le from the SPSS

directory (using Fie gt Open gt Daa) and look at the output window

Figure 4 Output viewer with GET command

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1054

Module 5 Starting with SPSS Syntax

4

S e s s i o n A U

s i n g S P S S C o m m a n d s The full GET command in the output is

getfile=EProgramFilesSPSS141991USGeneralSocialSurveysav

The GET command reads an SPSS data le into the data editor Its general form is-

G E T F I L E = phfl

Earlier versions of SPSS needed to have an ExECUTE command after the GET command so SPSS would run the GET

command and read the data into the data editor

The other command the DATASET command is used to name the active data

window Its not absolutely necessary but it looks like

D A T A S E T N A M E D a t a s e t 1 W I N D O W = F R O N T

Now we will run a FREQUENCIES procedure

Select Anaze gt Descripive Saisics gt Freqencies and

choose the variables shown in Figure 5

Figure 5 Frequencies dialog box

Click OK to run the Frequencies and look at the output

Figure 6 Frequencies command preceding Frequencies output

The FREQUENCIES command can be seen after the GET and DATASET

commands and before the title at the start of the Frequencies output

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1154

Module 5 Starting with SPSS Syntax

5

S e s s i onA U s i n g S P S

S C omm an d s

The FREQUENCIES command is-

F R E Q U E N C I E S V A R I A B L E S = h a p p y l i f e o r d e r = a n a l y s i s

In its simplest form all you need is a list of the variables-

F r e q u e n c i e s v a r i a b l e s = h a p p y l i f e

The commands in the output window merely document the SPSS session and cant be run from here To run commands we will

need use a syntax window to be discussed soon

Notes in output viewer

The other place you can see a report of the underlying commands is in the Notes

object produced every time you run something a procedure In the Viewer outline

to the left of the output as you look at it you can see a Notes object as part of every

procedures output although by default its contents are hidden Above Figure 6

shows the Notes object as closed or hidden Below in Figure 8 shows the Notes

object open to view

Double click on the Notes icon in the outline to show its contents

Figure 7 Notes from the Frequencies procedure

Notes are quite useful to check details if an SPSS procedure hasnrsquot worked as

you expected it to From the notes you can check to make sure you havent any

weights split le routines or any selection lters switched on You can check that

you have the correct le and syntax and there is a date and time stamp so you can

check when the output was produced

Showing and hiding output objects is discussed in the SPSS 1Getting Started with SPSS course Course Code 1301

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1254

Module 5 Starting with SPSS Syntax

6

S e s s i o n A U

s i n g S P S S C o m m a n d s The syntax window

The syntax window is used to store and run SPSS commands A default installation 1

of SPSS will not open a syntax window Each time you do something with the menus

whether it is to read in data or perform an analysis you have issued a command or

series of commands for SPSS to obey It is possible to paste the SPSS commands

underlying any dialog box into a syntax window using the Paste button - available

on most SPSS dialog boxes It will automatically open up a new syntax2 window

and paste the underlying command(s) from the dialog box into the new window The

contents of a syntax window can be saved to a le so the analysis can be rerun

later without using menus

Figure 8 Frequencies dialog box again

For example if we go back to the Frequencies procedure run earlier we can paste

the underlying FREQUENCIES command into a syntax window

Choose Anaze gt Descripive Saisics gt Freqencies

Click the Carts button and select a bar chart Click Continue to get back

to the Frequencies dialog box

Instead of pressing OK to run Frequencies click on Paste

Figure 9 Frequencies command in the syntax window

SPSS will automatically open up a syntax window and bring it to the front of the

screen with the FREQUENCIES command pasted into it

1 You can change this default using the Edi gt Opionshellip menu under the Gen-

era tab by ticking Open synta window at start-up

2 If a syntax window is already open it will put the command in it without bringing it

to the front

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1354

Module 5 Starting with SPSS Syntax

7

S e s s i onA U s i n g S P S

S C omm an d s

This is the FREQUENCIES command underlying the analysis we ran at the

beginning of the rst exercise You can see the FREQUENCIES command keyword

three subcommand keywords VARIABlES BARChART and ORDER separated

by and two other keywords FREQ and ANAlYSIS And like all commands its

nished with a full stop

To break the command down

Thebull VARIABlES subcommand lists the variables for which we want frequencies

in this example the variables appy and ife

Thebull BARChART subcommand requests a bar chart to be plotted for each of

the variables in the list FREQ species frequencies raw counts rather than

percents on the scale axis

Thebull ORDER subcommand with keyword ANAlYSIS species all the charts after

the tables If the VARIABlE keyword had been used instead the chart for a

particular variable would be placed next to its frequency table

If you want to see all the possible subcommands and other keywords you can use

with any command click the Sow synta cart button on the syntax window

toolbar to look at its syntax chart

Figure 10 The Sow synta cart button

Select the Frequencies command and click on the synta cart button to

see its syntax chart

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1454

Module 5 Starting with SPSS Syntax

8

S e s s i o n A U

s i n g S P S S C o m m a n d s Figure 11 FREQUENCIES syntax chart

As you can see the chart is quite complicated but will give you all the elements you

can put in a FREQUENCIES command You maybe able to spot the subcommands

and keywords just described

Each element in square brackets [ ] indicates a optional element a list of elements

aligned above one another in curly brackets indicates a choice of one out of that

series of elements default choice is usually given in bold All SPSS command sub

command and other keywords are presented in uppercase in chart (although you

dont need to type them in uppercase) Check the syntax reference guide or on-line

help for examples of how each command is used

Once you are nished having a look at the help information close the Helpwindow and you should go back to the Syntax window

The SPSS Command Syntax Reference guide will give you moredetails about SPSS commands and how to run them Syntax

guides are available on the SPSS manual CD or within SPSS in

PDF format from Hep gt Coand Snax Reference

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1554

Module 5 Starting with SPSS Syntax

9

S e s s i onA U s i n g S P S

S C omm an d s

Running an SPSS commandThe Run Current button on the syntax window tool bar looks like an audio play

button Its used to run the command currently selected in the syntax window It is

easy to run an already pasted command either click somewhere on the command

or highlight the entire command (including the full stop) and click the run button

Figure 12 Running the FREQUENCIES command

Click somewhere on the FREQUENCIES command and click the Run

button to execute the command

You will see the procedure being run as you did when you used the menu to run

Frequencies When the processor has nished - it shouldnt take very long - the

results will be stored in the output window as usual

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1654

Module 5 Starting with SPSS Syntax

10

S e s s i o n A U

s i n g S P S S C o m m a n d s Typing in a command

The Syntax window can be used on its own to run SPSS commands if you know

what the commands are (or have generated them from the menus) A new syntax

window can be opened by choosing Fie gt New gt Synta or an existing one can be

opened using Fie gt Open gt Synta A syntax window can be brought to the front by

choosing it from the Windows menu if it is already open

Select the Syntax window from the Window menuWindow gt 3 Snax1 ndash SPSS Snax Edior

The rst syntax window is usually called Synta1 and the 3 before the name

indicates its the third open SPSS window after the data editor and output window If

you have more windows open then the number might be different

The CROSSTABS command

The following CROSSTABS command will cross-tabulate the appy variable by the

ife variable from the social survey dataset

c r o s s t a b s h a p p y b y l i f e

It is a simple form of the CROSSTABS command with no subcommands

Type in the CROSSTABS command crosstabs appy by ife into the

Syntax window

Figure 13 CROSSTABS command selected

A full stop is needed to indicate the end of each SPSS commandIf you forget to nish a command with a full stop the command

may not run properly

Select the whole CROSSTABS command as shown above and click on the

Run button to run it

If you switched on Dispay coands in te og demonstrated on page 2 then

the commands are displayed with the output then you will see the CROSSTABS

command just before the output from Crosstabs

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1754

Module 5 Starting with SPSS Syntax

11

S e s s i onA U s i n g S P S

S C omm an d s

Figure 14 Start of Output from the CROSSTABS command

The CROSSTABS command has several subcommands try the syntax chart button

to see what they are They include

CEllS used to change the content of the table

BARChART to add a clustered bar chart for each layer

Figure 15 CROSSTABS using CEllS and BARChART subcommands

Try typing in and running the new command in Figure 15

Layers can be added to the table by adding another BY keyword follwed by the layer

variable or variable list For example

c r o s s t a b s h a p p y b y l i f e b y r e g i o n

will produce a crosstabs table layer for each region Try adding a layer to the latest

crosstabs command and run the command to check the effect

Some command names such as FINISh BEGIN DATA and

END DATA are complete commands which need no further

specication However most commands (including all of the statistical subprograms) have to have a certain amount

of specication and each has its own particular form of

specication so check the syntax guide before you run

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1854

Module 5 Starting with SPSS Syntax

12

S e s s i o n A U

s i n g S P S S C o m m a n d s Slide Content from presentation

SPSS Coand language

Starting with commands

Wy use Coands

More exible

Extra features

Can be saved

And repeated

Te Synta Window

SPSS commands are entered in the Syntax Window

Create a Syntax Window

Either selecting File gt New gt Syntax from the menu bar

Or creating le with sps extension in a text editor

Use an existing syntax window

Either Selecting an open syntax window from the Window menu

Or selecting File gt Open gt Syntax from the menu bar

SPSS coands

Commands are entered in the Syntax Window by

Either opening the syntax Window and typing directly

Or building up a command using dialog boxes then clicking the

PASTE button

Or using a mixture of the two

Forat of Coands

Control Field - consists of SPSS keyword

Specication Field - names of variables etc on which command willoperate

Full stop - to terminate command

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1954

Module 5 Starting with SPSS Syntax

13

S e s s i onA U s i n g S P S

S C omm an d s

Synta Rues

Commands start with a command keyword

Commands are case insensitive

Except for text in quotes

A single command can go over many syntax lines

Each line of syntax must not exceed 256 bytes3

Do not break a syntax line

in a variable name

in between quotes lsquo lsquo or ldquo ldquo

For a syntax window

Each command must start on a new line

Each command must end with a full stop with no non-blanks after it

or end with a completely blank line afterwards

For batc coands (see ast session)

Each command begins on a new line in column 1

unless you use + or -

Continuation lines mustnrsquot start in column 1

Full stops command terminators are optional

Te FINISh coand

FINISH

For batch commands

Terminates a le of SPSS commands and closes SPSS tidily

NB When SPSS windows manager is running (eg during an SPSS

for Windows session) FINISH will generate an error

Terminate an interactive SPSS session using Exit from the File menu

3 256 bytes are characters in single byte languageshellip

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2054

Module 5 Starting with SPSS Syntax

14

S e s s i o n A U

s i n g S P S S C o m m a n d s

Session summaryIn this session we covered

How to display SPSS commands in output window as they are runbull

How using thebull Paste button in an SPSS dialog box pastes the underlying

command into a synta window

The Syntax Window where SPSS commands (also calledbull synta) are stored

Using the syntax chart button to get help about a command in the syntax windowbull

How to type in and run an SPSS command from thebull synta window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2154

Session B

SPSS CommandStructure

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2254

Module 5 Starting with SPSS Syntax

16

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Commands Subcommands amp keywords

In general an SPSS command will be a mixture of different elements like this -

v kyw kyw kyw

An SPSS command starts with a recognized command keyword which has to

be spelt correctly Each command has its own general format with its own set of

subcommands and keywords These are combined with lists of variables names

or combinations of variable names and keywords to specify the analysis or action

required by the computer

Subcommands and other keywords are words recognized by SPSS as part of

the commands specication and so appear in the specication eld Some

SPSS procedures have subcommands which can appear either on their own or in

association with certain keywords As with commands subcommands and keywords

have to appear exactly as they are spelt

Figure 17 Different keywords in the Commands example

get FILE=mspssworkCARDIACsav

crosstabs chd B Y famhist CELLS=C O U N T R O W C O L U M N STATISTICS=C H I S Q

requencies VARIABLES=height cigaret

FORMAT=L I M I T (12 ) HIST

graPH SCATTERPLOT weight W I T H height

inisH

The different types of keyword have been highlighted as follows-

command keywords SUBCOMMAND keywords

O T H E R keywords variable names and other specication

Abbreviating keywords

SPSS will recognise most of its command and subcommand keywords when

abbreviated to the rst three or four characters of the name as long as the keywordsare unique So for example-

freq vars=height cigaret form=lim(12) hist

can be used as an abbreviation of-

frequencies variables=height cigaret

format=limit(12)

histogram

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2354

Module 5 SPSS command Structure

17

S e s s i onB S P S S C om

m an d S t r u c t ur e

Reserved keywords

With most procedures it does not matter if there is a variable name the same as a

keyword since there are certain places in a command specication where a variable

or variable list is expected and others where keywords are expected However there

are some reserved keywords which cannot be used as names These are

ALL AND BY EQ GE GT LE LT NE NOT OR TO WITH

These words cannot be used as variable names in SPSS because the program

already recognizes them as having meaning For example

FREQUENCIES VARIABLES=ALL

calculates a frequency table for all the variables currently stored on SPSSs

active le To have a variable called A L L would make the sense of the instruction

ambiguous to SPSS

Lists of Variables

The variables are kept in the SPSS active le in the order they are given in the

DATA lIST command and any variables generated during the session in the order

they were created This order can be utilized when instead of typing a long list of

consecutive variables you type the name of the rst variable then the TO keyword

and then the name of the last variable in the list These inclusive ranges of variables

can be combined with the names of other variables to produce a larger variable list

where needed For example if we had the following variables in a system le-

LIST OF VARIABLES ON THE ACTIVE FILE

1 AGE 6 HHLDTYPE 11 OCPENAMT 16 SPSCLASS

2 CHRONILL 7 HOMEHELP 12 REBTOWN 17 TOTINCID

3 CLASS 8 KINNEAR 13 REBTRENT 18 UNEARNED

4 EMPLOYED 9 MARITAL 14 GENDER 19 CASWGT

5 HEALTH 10 NIRAMT 15 SPSAGE

In SPSS variable lists individual variable names andor variable ranges can

be separated by either spaces or commas However any keywords have to be

separated from variable names by spaces ie use spaces with TO or All

AGE TO CASWGT implies

AGE CHRONILL CLASS EMPLOYED HEALTH HHLDTYPEHOMEHELP KINNEAR MARITAL NIRAMT OCPENAMT REBTOWNREBTRENT GENDER SPSAGE SPSCLASS TOTINCID UNEARNEDCASWGT

This rst list is all the variables in the active le since AGE is the rst variable on the

list and CASWGT is the last

hhlDTYPE TO GENDER implies

HHLDTYPE HOMEHELP KINNEAR MARITAL NIRAMT OCPENAMTREBTOWN REBTRENT GENDER

Notice in this list 7 of the 9 variables are implied in the same order as the active le

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2454

Module 5 Starting with SPSS Syntax

18

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e ClASS hEAlTh TO NIRAmT implies

CLASS HEALTH HHLDTYPE HOMEHELP KINNEAR MARITALNIRAMT

A list can be combined with individual variables

AGE SPAGE ClASS SPClASS All implies

AGE SPSAGE CLASS SPSCLASS CHRONILLEMPLOYED HEALTH HHLDTYPE HOMEHELP KINNEARMARITAL NIRAMT OCPENAMT REBTOWN REBTRENT GENDERTOTINCID UNEARNED CASWGT

The reserved keyword ALL in this context means all the variables in the active le

It can be used in a variable list for the KEEP subcommand on a GET or SAVE

command to change the order the variables are saved in whilst making sure no

variables are missed Variables not specically listed will appear after all the listed

ones in active le order

If new variables are created during a session they will be added to the active le in

the order they were created after the variables already stored in the active le You

can always check the variable order in the active le using the DISPlAY command -

it may not be the same order they will appear in dialog boxes

Syntax Charts

The Sow synta cart button as seen in the previous session in Figure 11 will let

you look at command syntax charts If you have no command selected in the syntax

window then the button will give you an alphabetic list of all the command keywords

Look at the syntax chart for each of these commands to see what subcommands and keywords are available

Use the Search button to look for more information about each

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2554

Module 5 SPSS command Structure

19

S e s s i onB S P S S C om

m an d S t r u c t ur e

Some SPSS Command examples (from slides)

Te GET coand

The GET command reads a sav le into SPSS

GET FILE = cardiacsav

Te GET coand - KEEP and DROP subcoands

Use the KEEP subcommand to select only some of the variables

GET FILE=cardiacsavKEEP=famhist chd

Use the DROP subcommand to select all of the le except certain

variables

GET FILE=cardiacsavDROP=height weight

Te DATASET NAmE coand

The DATASET NAME command give the active dataset a short name

which can be used in later commands

GET FILE = mspssworkcardiacsav

DATASET NAME cardiac

Te SAVE coand

Use the SAVE command to save your data in a sav le

SAVE OUTFILE=myfilesav

Te SAVE coand wi

save all your cases together with variable names values

missing values variable and value labels and formats

Saved in the state they are in when the command is issued

Any further changes can only be saved with another SAVE command

It will NOT save the results of any analyses results are saved in an

output window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2654

Module 5 Starting with SPSS Syntax

20

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Te SAVE coand KEEP and DROP subcoands

The KEEP and DROP subcommands are available with the SAVE

command and perform the same function as with GET

Using KEEP will save only the named variables

SAVE OUTFILE=filesav KEEP=AGE HEIGHT

Using DROP will save all variables except named ones (here it means

all variables except WEIGHT2)

SAVE OUTFILE=filesav DROP=WEIGHT2

FREQUENCIES coand

FREQUENCIES VARIABLES=HEIGHT

FREQUENCIES - STATISTICS subcoand

FREQ VAR=HEIGHT AGE STATISTICS

Gives four default statistics

MEAN STDDEV MINIMUM MAXIMUM

FREQUENCIES - more statistics

MEDIAN MODE SUM RANGE SEMEAN VARIANCESKEWNESS SESKEW KURTOSIS SEKURT

STATISTICS=DEFAULT

Use to get the default four statistics MEAN STDDEV MINIMUM

MAXIMUM together with others you ask for

STATISTICS=ALL

Gives all available statistics - can be wasteful

STATISTICS=NONE

Equivalent to omitting STATISTICS altogether

FREQUENCIES - FORmAT subcoand

To suppress the frequency table use

FORMAT=NOTABLE

To suppress tables over a certain length

FORMAT=LIMIT(50)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2754

Module 5 SPSS command Structure

21

S e s s i onB S P S S C om

m an d S t r u c t ur e

FREQUENCIES - subcoands for carts

There are three subcommands to produce type of charts named

HISTOGRAM

BARCHART

PIECHART

Using ore tan one subcoand

To use more than one subcommand string them together and nish

the command with the terminating full stop

FREQUENCIES VARIABLES=AGESEX STATISTICS=RANGEBARCHART

CROSSTABS coand

CROSSTABS TABLES=AGE BY SEX

CROSSTABS AGE BY SEX

SEX BY WEIGHT

The next three crosstabs commands produce the same three tables

CROSSTABS AGE BY SEX

AGE BY PARTY

AGE BY INCOME

CROSSTABS AGE BY SEX PARTY INCOMECROSSTABS AGE BY SEX TO INCOME

CROSSTABS - STATISTICS subcoand

CROSSTABS HEIGHT BY AGE

STATISTICS

Gives the Chi-square statistic tests

CROSSTABS - more statistics

Others available

CHISQ PHI CC LAMBDA UC BTAU CTAU GAMMA DETA CORR ALL NONE

and KAPPA RISK MCNEMAR CMH(1)

Usage

CROSSTABS TABLES=AGE BY WEIGHT

STATISTICS=BTAU CORR

Only gives those statistics asked for

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 4: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 454

v

PrefaceThese are the course notes for SPSS 5 Starting with SPSS Syntax Course Code

1305 The notes are designed for use with the SPSS 14 Brief Guide which comes

with SPSS It can be downloaded from

hpwwwcsedackecs_docenaionDocens_b_Nber3639

A full list of course notes SPSS manuals and details of the courses but can be

dowloaded from

hpwwwcsedacksdcscaaogeISCaDaah

including the SPSS course information leaet which contains details of other SPSS

courses offered by IS skills datasets used during the courses and resources

available at the university The course builds on courses SPSS 1 Getting Started

with SPSS course code 1301 and SPSS 3 Changing Data in SPSS course code

1303 It is assumed you have attended that course or worked through the notes for

that course

Conventions

Choosing an item from a menu is indicated by ain en gt sben gt ie

For example Fie gt New gt Daa means open the Fie Menu from that choose

New sub menu and then choose Daa from the sub menu (the choice will open a

new data window)

This type of paragraph is

an action paragraph That is to say it indicates

something you can do in SPSS if you are following the notes on your

computer

This sort of paragraph is an information paragraph and will Lcontain points of note or warnings

Copyright copy IS 2008

Permission is granted to any individual or institution to use copy or redistribute this

document whole or in part so long as it is not sold for prot and provided that theabove copyright notice and this permission notice appear in all copies

Where any part of this document is included in another document due

acknowledgement is required

Module 5 Starting with SPSS Syntax

P r e f a c e

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 554

v

Module 5 Starting with SPSS Syntax

C on t en t s

Contents

Using SPSS CoandsUsing SPSS commands 2

The syntax window 6

Running an SPSS command 9

Typing in a command 10

The CROSSTABS command 10Slide Content from presentation 12

SPSS Coand StructureCommands Subcommands amp keywords 16

Abbreviating keywords 16

Reserved keywords 17

Lists of Variables 17

Syntax Charts 18

Some SPSS Command examples (from slides) 19

Try yourself 23

Synta for CacuationsPresentation slides for calculations 26

RECODE command 27

COMPUTE command 28

COUNT command 30

IF command 31

Filter command 33

DO REPEAT command 34

DO IF - END IF commands 35

Running Synta in BatcesProduction Facility 38

Running a job with SPSS Production facility 39

Output from an SPSS job 43

Output in html format - a web page 44

Logging commands and SPSS Journal 46

Try for yourself 47

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 654

v

C o n t e n t s ( c o n t i n u e d )

Module 5 Starting with SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 754

Session A

Using SPSSCommands

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 854

Module 5 Starting with SPSS Syntax

2

S e s s i o n A U

s i n g S P S S C o m m a n d s Using SPSS commands

SPSS command language lies behind any work you do with SPSS although since

most analyses can be done using the menus in the normal course of events you

need never encounter any SPSS commands directly In the dim and distant past the

only way to use SPSS was via its command language (also called syntax) In the

present using a default installation you will only see command names very briey at

the bottom of the window in the status bar while the relevant procedures are being

run by SPSS

However it is easy to see the underlying commands and to generate SPSS

commands from the interactive menus and dialog boxes SPSS commands are

stored in a Synta window which is a plain text le usually with a sps extension

Keeping command syntax gives you the ability to perform the same tasks again and

again

SPSS commands in the output viewer

You can choose to have the underlying commands displayed in a log along with the

output in the output viewer using the Viewer tab in Options from the Edit menu

Start up SPSS and choose Type in data so the data editor is empty

Choose Edi gt Opions to open the Options dialog box

Figure 1 Options dialog box (Genera tab showing)

Click on the Viewer tab to look at the Viewer options

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 954

Module 5 Starting with SPSS Syntax

3

S e s s i onA U s i n g S P S

S C omm an d s

Figure 2 Options dialog box (Viewer tab showing)

Tick Dispay coands in te og box under Initia Output State heading

to display commands Click OK or Appy

Figure 3 Options detail Dispay coands ticked

Once this feature has been switched on the underlying SPSS commands for

anything you do in SPSS will be pasted in the output window before the output

itself For example in Figure 4 you should see a GET command followed by the

DATASET command in the output window

Open the 1991 US Genera Socia Surveysav data le from the SPSS

directory (using Fie gt Open gt Daa) and look at the output window

Figure 4 Output viewer with GET command

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1054

Module 5 Starting with SPSS Syntax

4

S e s s i o n A U

s i n g S P S S C o m m a n d s The full GET command in the output is

getfile=EProgramFilesSPSS141991USGeneralSocialSurveysav

The GET command reads an SPSS data le into the data editor Its general form is-

G E T F I L E = phfl

Earlier versions of SPSS needed to have an ExECUTE command after the GET command so SPSS would run the GET

command and read the data into the data editor

The other command the DATASET command is used to name the active data

window Its not absolutely necessary but it looks like

D A T A S E T N A M E D a t a s e t 1 W I N D O W = F R O N T

Now we will run a FREQUENCIES procedure

Select Anaze gt Descripive Saisics gt Freqencies and

choose the variables shown in Figure 5

Figure 5 Frequencies dialog box

Click OK to run the Frequencies and look at the output

Figure 6 Frequencies command preceding Frequencies output

The FREQUENCIES command can be seen after the GET and DATASET

commands and before the title at the start of the Frequencies output

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1154

Module 5 Starting with SPSS Syntax

5

S e s s i onA U s i n g S P S

S C omm an d s

The FREQUENCIES command is-

F R E Q U E N C I E S V A R I A B L E S = h a p p y l i f e o r d e r = a n a l y s i s

In its simplest form all you need is a list of the variables-

F r e q u e n c i e s v a r i a b l e s = h a p p y l i f e

The commands in the output window merely document the SPSS session and cant be run from here To run commands we will

need use a syntax window to be discussed soon

Notes in output viewer

The other place you can see a report of the underlying commands is in the Notes

object produced every time you run something a procedure In the Viewer outline

to the left of the output as you look at it you can see a Notes object as part of every

procedures output although by default its contents are hidden Above Figure 6

shows the Notes object as closed or hidden Below in Figure 8 shows the Notes

object open to view

Double click on the Notes icon in the outline to show its contents

Figure 7 Notes from the Frequencies procedure

Notes are quite useful to check details if an SPSS procedure hasnrsquot worked as

you expected it to From the notes you can check to make sure you havent any

weights split le routines or any selection lters switched on You can check that

you have the correct le and syntax and there is a date and time stamp so you can

check when the output was produced

Showing and hiding output objects is discussed in the SPSS 1Getting Started with SPSS course Course Code 1301

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1254

Module 5 Starting with SPSS Syntax

6

S e s s i o n A U

s i n g S P S S C o m m a n d s The syntax window

The syntax window is used to store and run SPSS commands A default installation 1

of SPSS will not open a syntax window Each time you do something with the menus

whether it is to read in data or perform an analysis you have issued a command or

series of commands for SPSS to obey It is possible to paste the SPSS commands

underlying any dialog box into a syntax window using the Paste button - available

on most SPSS dialog boxes It will automatically open up a new syntax2 window

and paste the underlying command(s) from the dialog box into the new window The

contents of a syntax window can be saved to a le so the analysis can be rerun

later without using menus

Figure 8 Frequencies dialog box again

For example if we go back to the Frequencies procedure run earlier we can paste

the underlying FREQUENCIES command into a syntax window

Choose Anaze gt Descripive Saisics gt Freqencies

Click the Carts button and select a bar chart Click Continue to get back

to the Frequencies dialog box

Instead of pressing OK to run Frequencies click on Paste

Figure 9 Frequencies command in the syntax window

SPSS will automatically open up a syntax window and bring it to the front of the

screen with the FREQUENCIES command pasted into it

1 You can change this default using the Edi gt Opionshellip menu under the Gen-

era tab by ticking Open synta window at start-up

2 If a syntax window is already open it will put the command in it without bringing it

to the front

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1354

Module 5 Starting with SPSS Syntax

7

S e s s i onA U s i n g S P S

S C omm an d s

This is the FREQUENCIES command underlying the analysis we ran at the

beginning of the rst exercise You can see the FREQUENCIES command keyword

three subcommand keywords VARIABlES BARChART and ORDER separated

by and two other keywords FREQ and ANAlYSIS And like all commands its

nished with a full stop

To break the command down

Thebull VARIABlES subcommand lists the variables for which we want frequencies

in this example the variables appy and ife

Thebull BARChART subcommand requests a bar chart to be plotted for each of

the variables in the list FREQ species frequencies raw counts rather than

percents on the scale axis

Thebull ORDER subcommand with keyword ANAlYSIS species all the charts after

the tables If the VARIABlE keyword had been used instead the chart for a

particular variable would be placed next to its frequency table

If you want to see all the possible subcommands and other keywords you can use

with any command click the Sow synta cart button on the syntax window

toolbar to look at its syntax chart

Figure 10 The Sow synta cart button

Select the Frequencies command and click on the synta cart button to

see its syntax chart

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1454

Module 5 Starting with SPSS Syntax

8

S e s s i o n A U

s i n g S P S S C o m m a n d s Figure 11 FREQUENCIES syntax chart

As you can see the chart is quite complicated but will give you all the elements you

can put in a FREQUENCIES command You maybe able to spot the subcommands

and keywords just described

Each element in square brackets [ ] indicates a optional element a list of elements

aligned above one another in curly brackets indicates a choice of one out of that

series of elements default choice is usually given in bold All SPSS command sub

command and other keywords are presented in uppercase in chart (although you

dont need to type them in uppercase) Check the syntax reference guide or on-line

help for examples of how each command is used

Once you are nished having a look at the help information close the Helpwindow and you should go back to the Syntax window

The SPSS Command Syntax Reference guide will give you moredetails about SPSS commands and how to run them Syntax

guides are available on the SPSS manual CD or within SPSS in

PDF format from Hep gt Coand Snax Reference

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1554

Module 5 Starting with SPSS Syntax

9

S e s s i onA U s i n g S P S

S C omm an d s

Running an SPSS commandThe Run Current button on the syntax window tool bar looks like an audio play

button Its used to run the command currently selected in the syntax window It is

easy to run an already pasted command either click somewhere on the command

or highlight the entire command (including the full stop) and click the run button

Figure 12 Running the FREQUENCIES command

Click somewhere on the FREQUENCIES command and click the Run

button to execute the command

You will see the procedure being run as you did when you used the menu to run

Frequencies When the processor has nished - it shouldnt take very long - the

results will be stored in the output window as usual

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1654

Module 5 Starting with SPSS Syntax

10

S e s s i o n A U

s i n g S P S S C o m m a n d s Typing in a command

The Syntax window can be used on its own to run SPSS commands if you know

what the commands are (or have generated them from the menus) A new syntax

window can be opened by choosing Fie gt New gt Synta or an existing one can be

opened using Fie gt Open gt Synta A syntax window can be brought to the front by

choosing it from the Windows menu if it is already open

Select the Syntax window from the Window menuWindow gt 3 Snax1 ndash SPSS Snax Edior

The rst syntax window is usually called Synta1 and the 3 before the name

indicates its the third open SPSS window after the data editor and output window If

you have more windows open then the number might be different

The CROSSTABS command

The following CROSSTABS command will cross-tabulate the appy variable by the

ife variable from the social survey dataset

c r o s s t a b s h a p p y b y l i f e

It is a simple form of the CROSSTABS command with no subcommands

Type in the CROSSTABS command crosstabs appy by ife into the

Syntax window

Figure 13 CROSSTABS command selected

A full stop is needed to indicate the end of each SPSS commandIf you forget to nish a command with a full stop the command

may not run properly

Select the whole CROSSTABS command as shown above and click on the

Run button to run it

If you switched on Dispay coands in te og demonstrated on page 2 then

the commands are displayed with the output then you will see the CROSSTABS

command just before the output from Crosstabs

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1754

Module 5 Starting with SPSS Syntax

11

S e s s i onA U s i n g S P S

S C omm an d s

Figure 14 Start of Output from the CROSSTABS command

The CROSSTABS command has several subcommands try the syntax chart button

to see what they are They include

CEllS used to change the content of the table

BARChART to add a clustered bar chart for each layer

Figure 15 CROSSTABS using CEllS and BARChART subcommands

Try typing in and running the new command in Figure 15

Layers can be added to the table by adding another BY keyword follwed by the layer

variable or variable list For example

c r o s s t a b s h a p p y b y l i f e b y r e g i o n

will produce a crosstabs table layer for each region Try adding a layer to the latest

crosstabs command and run the command to check the effect

Some command names such as FINISh BEGIN DATA and

END DATA are complete commands which need no further

specication However most commands (including all of the statistical subprograms) have to have a certain amount

of specication and each has its own particular form of

specication so check the syntax guide before you run

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1854

Module 5 Starting with SPSS Syntax

12

S e s s i o n A U

s i n g S P S S C o m m a n d s Slide Content from presentation

SPSS Coand language

Starting with commands

Wy use Coands

More exible

Extra features

Can be saved

And repeated

Te Synta Window

SPSS commands are entered in the Syntax Window

Create a Syntax Window

Either selecting File gt New gt Syntax from the menu bar

Or creating le with sps extension in a text editor

Use an existing syntax window

Either Selecting an open syntax window from the Window menu

Or selecting File gt Open gt Syntax from the menu bar

SPSS coands

Commands are entered in the Syntax Window by

Either opening the syntax Window and typing directly

Or building up a command using dialog boxes then clicking the

PASTE button

Or using a mixture of the two

Forat of Coands

Control Field - consists of SPSS keyword

Specication Field - names of variables etc on which command willoperate

Full stop - to terminate command

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1954

Module 5 Starting with SPSS Syntax

13

S e s s i onA U s i n g S P S

S C omm an d s

Synta Rues

Commands start with a command keyword

Commands are case insensitive

Except for text in quotes

A single command can go over many syntax lines

Each line of syntax must not exceed 256 bytes3

Do not break a syntax line

in a variable name

in between quotes lsquo lsquo or ldquo ldquo

For a syntax window

Each command must start on a new line

Each command must end with a full stop with no non-blanks after it

or end with a completely blank line afterwards

For batc coands (see ast session)

Each command begins on a new line in column 1

unless you use + or -

Continuation lines mustnrsquot start in column 1

Full stops command terminators are optional

Te FINISh coand

FINISH

For batch commands

Terminates a le of SPSS commands and closes SPSS tidily

NB When SPSS windows manager is running (eg during an SPSS

for Windows session) FINISH will generate an error

Terminate an interactive SPSS session using Exit from the File menu

3 256 bytes are characters in single byte languageshellip

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2054

Module 5 Starting with SPSS Syntax

14

S e s s i o n A U

s i n g S P S S C o m m a n d s

Session summaryIn this session we covered

How to display SPSS commands in output window as they are runbull

How using thebull Paste button in an SPSS dialog box pastes the underlying

command into a synta window

The Syntax Window where SPSS commands (also calledbull synta) are stored

Using the syntax chart button to get help about a command in the syntax windowbull

How to type in and run an SPSS command from thebull synta window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2154

Session B

SPSS CommandStructure

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2254

Module 5 Starting with SPSS Syntax

16

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Commands Subcommands amp keywords

In general an SPSS command will be a mixture of different elements like this -

v kyw kyw kyw

An SPSS command starts with a recognized command keyword which has to

be spelt correctly Each command has its own general format with its own set of

subcommands and keywords These are combined with lists of variables names

or combinations of variable names and keywords to specify the analysis or action

required by the computer

Subcommands and other keywords are words recognized by SPSS as part of

the commands specication and so appear in the specication eld Some

SPSS procedures have subcommands which can appear either on their own or in

association with certain keywords As with commands subcommands and keywords

have to appear exactly as they are spelt

Figure 17 Different keywords in the Commands example

get FILE=mspssworkCARDIACsav

crosstabs chd B Y famhist CELLS=C O U N T R O W C O L U M N STATISTICS=C H I S Q

requencies VARIABLES=height cigaret

FORMAT=L I M I T (12 ) HIST

graPH SCATTERPLOT weight W I T H height

inisH

The different types of keyword have been highlighted as follows-

command keywords SUBCOMMAND keywords

O T H E R keywords variable names and other specication

Abbreviating keywords

SPSS will recognise most of its command and subcommand keywords when

abbreviated to the rst three or four characters of the name as long as the keywordsare unique So for example-

freq vars=height cigaret form=lim(12) hist

can be used as an abbreviation of-

frequencies variables=height cigaret

format=limit(12)

histogram

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2354

Module 5 SPSS command Structure

17

S e s s i onB S P S S C om

m an d S t r u c t ur e

Reserved keywords

With most procedures it does not matter if there is a variable name the same as a

keyword since there are certain places in a command specication where a variable

or variable list is expected and others where keywords are expected However there

are some reserved keywords which cannot be used as names These are

ALL AND BY EQ GE GT LE LT NE NOT OR TO WITH

These words cannot be used as variable names in SPSS because the program

already recognizes them as having meaning For example

FREQUENCIES VARIABLES=ALL

calculates a frequency table for all the variables currently stored on SPSSs

active le To have a variable called A L L would make the sense of the instruction

ambiguous to SPSS

Lists of Variables

The variables are kept in the SPSS active le in the order they are given in the

DATA lIST command and any variables generated during the session in the order

they were created This order can be utilized when instead of typing a long list of

consecutive variables you type the name of the rst variable then the TO keyword

and then the name of the last variable in the list These inclusive ranges of variables

can be combined with the names of other variables to produce a larger variable list

where needed For example if we had the following variables in a system le-

LIST OF VARIABLES ON THE ACTIVE FILE

1 AGE 6 HHLDTYPE 11 OCPENAMT 16 SPSCLASS

2 CHRONILL 7 HOMEHELP 12 REBTOWN 17 TOTINCID

3 CLASS 8 KINNEAR 13 REBTRENT 18 UNEARNED

4 EMPLOYED 9 MARITAL 14 GENDER 19 CASWGT

5 HEALTH 10 NIRAMT 15 SPSAGE

In SPSS variable lists individual variable names andor variable ranges can

be separated by either spaces or commas However any keywords have to be

separated from variable names by spaces ie use spaces with TO or All

AGE TO CASWGT implies

AGE CHRONILL CLASS EMPLOYED HEALTH HHLDTYPEHOMEHELP KINNEAR MARITAL NIRAMT OCPENAMT REBTOWNREBTRENT GENDER SPSAGE SPSCLASS TOTINCID UNEARNEDCASWGT

This rst list is all the variables in the active le since AGE is the rst variable on the

list and CASWGT is the last

hhlDTYPE TO GENDER implies

HHLDTYPE HOMEHELP KINNEAR MARITAL NIRAMT OCPENAMTREBTOWN REBTRENT GENDER

Notice in this list 7 of the 9 variables are implied in the same order as the active le

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2454

Module 5 Starting with SPSS Syntax

18

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e ClASS hEAlTh TO NIRAmT implies

CLASS HEALTH HHLDTYPE HOMEHELP KINNEAR MARITALNIRAMT

A list can be combined with individual variables

AGE SPAGE ClASS SPClASS All implies

AGE SPSAGE CLASS SPSCLASS CHRONILLEMPLOYED HEALTH HHLDTYPE HOMEHELP KINNEARMARITAL NIRAMT OCPENAMT REBTOWN REBTRENT GENDERTOTINCID UNEARNED CASWGT

The reserved keyword ALL in this context means all the variables in the active le

It can be used in a variable list for the KEEP subcommand on a GET or SAVE

command to change the order the variables are saved in whilst making sure no

variables are missed Variables not specically listed will appear after all the listed

ones in active le order

If new variables are created during a session they will be added to the active le in

the order they were created after the variables already stored in the active le You

can always check the variable order in the active le using the DISPlAY command -

it may not be the same order they will appear in dialog boxes

Syntax Charts

The Sow synta cart button as seen in the previous session in Figure 11 will let

you look at command syntax charts If you have no command selected in the syntax

window then the button will give you an alphabetic list of all the command keywords

Look at the syntax chart for each of these commands to see what subcommands and keywords are available

Use the Search button to look for more information about each

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2554

Module 5 SPSS command Structure

19

S e s s i onB S P S S C om

m an d S t r u c t ur e

Some SPSS Command examples (from slides)

Te GET coand

The GET command reads a sav le into SPSS

GET FILE = cardiacsav

Te GET coand - KEEP and DROP subcoands

Use the KEEP subcommand to select only some of the variables

GET FILE=cardiacsavKEEP=famhist chd

Use the DROP subcommand to select all of the le except certain

variables

GET FILE=cardiacsavDROP=height weight

Te DATASET NAmE coand

The DATASET NAME command give the active dataset a short name

which can be used in later commands

GET FILE = mspssworkcardiacsav

DATASET NAME cardiac

Te SAVE coand

Use the SAVE command to save your data in a sav le

SAVE OUTFILE=myfilesav

Te SAVE coand wi

save all your cases together with variable names values

missing values variable and value labels and formats

Saved in the state they are in when the command is issued

Any further changes can only be saved with another SAVE command

It will NOT save the results of any analyses results are saved in an

output window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2654

Module 5 Starting with SPSS Syntax

20

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Te SAVE coand KEEP and DROP subcoands

The KEEP and DROP subcommands are available with the SAVE

command and perform the same function as with GET

Using KEEP will save only the named variables

SAVE OUTFILE=filesav KEEP=AGE HEIGHT

Using DROP will save all variables except named ones (here it means

all variables except WEIGHT2)

SAVE OUTFILE=filesav DROP=WEIGHT2

FREQUENCIES coand

FREQUENCIES VARIABLES=HEIGHT

FREQUENCIES - STATISTICS subcoand

FREQ VAR=HEIGHT AGE STATISTICS

Gives four default statistics

MEAN STDDEV MINIMUM MAXIMUM

FREQUENCIES - more statistics

MEDIAN MODE SUM RANGE SEMEAN VARIANCESKEWNESS SESKEW KURTOSIS SEKURT

STATISTICS=DEFAULT

Use to get the default four statistics MEAN STDDEV MINIMUM

MAXIMUM together with others you ask for

STATISTICS=ALL

Gives all available statistics - can be wasteful

STATISTICS=NONE

Equivalent to omitting STATISTICS altogether

FREQUENCIES - FORmAT subcoand

To suppress the frequency table use

FORMAT=NOTABLE

To suppress tables over a certain length

FORMAT=LIMIT(50)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2754

Module 5 SPSS command Structure

21

S e s s i onB S P S S C om

m an d S t r u c t ur e

FREQUENCIES - subcoands for carts

There are three subcommands to produce type of charts named

HISTOGRAM

BARCHART

PIECHART

Using ore tan one subcoand

To use more than one subcommand string them together and nish

the command with the terminating full stop

FREQUENCIES VARIABLES=AGESEX STATISTICS=RANGEBARCHART

CROSSTABS coand

CROSSTABS TABLES=AGE BY SEX

CROSSTABS AGE BY SEX

SEX BY WEIGHT

The next three crosstabs commands produce the same three tables

CROSSTABS AGE BY SEX

AGE BY PARTY

AGE BY INCOME

CROSSTABS AGE BY SEX PARTY INCOMECROSSTABS AGE BY SEX TO INCOME

CROSSTABS - STATISTICS subcoand

CROSSTABS HEIGHT BY AGE

STATISTICS

Gives the Chi-square statistic tests

CROSSTABS - more statistics

Others available

CHISQ PHI CC LAMBDA UC BTAU CTAU GAMMA DETA CORR ALL NONE

and KAPPA RISK MCNEMAR CMH(1)

Usage

CROSSTABS TABLES=AGE BY WEIGHT

STATISTICS=BTAU CORR

Only gives those statistics asked for

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 5: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 554

v

Module 5 Starting with SPSS Syntax

C on t en t s

Contents

Using SPSS CoandsUsing SPSS commands 2

The syntax window 6

Running an SPSS command 9

Typing in a command 10

The CROSSTABS command 10Slide Content from presentation 12

SPSS Coand StructureCommands Subcommands amp keywords 16

Abbreviating keywords 16

Reserved keywords 17

Lists of Variables 17

Syntax Charts 18

Some SPSS Command examples (from slides) 19

Try yourself 23

Synta for CacuationsPresentation slides for calculations 26

RECODE command 27

COMPUTE command 28

COUNT command 30

IF command 31

Filter command 33

DO REPEAT command 34

DO IF - END IF commands 35

Running Synta in BatcesProduction Facility 38

Running a job with SPSS Production facility 39

Output from an SPSS job 43

Output in html format - a web page 44

Logging commands and SPSS Journal 46

Try for yourself 47

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 654

v

C o n t e n t s ( c o n t i n u e d )

Module 5 Starting with SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 754

Session A

Using SPSSCommands

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 854

Module 5 Starting with SPSS Syntax

2

S e s s i o n A U

s i n g S P S S C o m m a n d s Using SPSS commands

SPSS command language lies behind any work you do with SPSS although since

most analyses can be done using the menus in the normal course of events you

need never encounter any SPSS commands directly In the dim and distant past the

only way to use SPSS was via its command language (also called syntax) In the

present using a default installation you will only see command names very briey at

the bottom of the window in the status bar while the relevant procedures are being

run by SPSS

However it is easy to see the underlying commands and to generate SPSS

commands from the interactive menus and dialog boxes SPSS commands are

stored in a Synta window which is a plain text le usually with a sps extension

Keeping command syntax gives you the ability to perform the same tasks again and

again

SPSS commands in the output viewer

You can choose to have the underlying commands displayed in a log along with the

output in the output viewer using the Viewer tab in Options from the Edit menu

Start up SPSS and choose Type in data so the data editor is empty

Choose Edi gt Opions to open the Options dialog box

Figure 1 Options dialog box (Genera tab showing)

Click on the Viewer tab to look at the Viewer options

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 954

Module 5 Starting with SPSS Syntax

3

S e s s i onA U s i n g S P S

S C omm an d s

Figure 2 Options dialog box (Viewer tab showing)

Tick Dispay coands in te og box under Initia Output State heading

to display commands Click OK or Appy

Figure 3 Options detail Dispay coands ticked

Once this feature has been switched on the underlying SPSS commands for

anything you do in SPSS will be pasted in the output window before the output

itself For example in Figure 4 you should see a GET command followed by the

DATASET command in the output window

Open the 1991 US Genera Socia Surveysav data le from the SPSS

directory (using Fie gt Open gt Daa) and look at the output window

Figure 4 Output viewer with GET command

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1054

Module 5 Starting with SPSS Syntax

4

S e s s i o n A U

s i n g S P S S C o m m a n d s The full GET command in the output is

getfile=EProgramFilesSPSS141991USGeneralSocialSurveysav

The GET command reads an SPSS data le into the data editor Its general form is-

G E T F I L E = phfl

Earlier versions of SPSS needed to have an ExECUTE command after the GET command so SPSS would run the GET

command and read the data into the data editor

The other command the DATASET command is used to name the active data

window Its not absolutely necessary but it looks like

D A T A S E T N A M E D a t a s e t 1 W I N D O W = F R O N T

Now we will run a FREQUENCIES procedure

Select Anaze gt Descripive Saisics gt Freqencies and

choose the variables shown in Figure 5

Figure 5 Frequencies dialog box

Click OK to run the Frequencies and look at the output

Figure 6 Frequencies command preceding Frequencies output

The FREQUENCIES command can be seen after the GET and DATASET

commands and before the title at the start of the Frequencies output

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1154

Module 5 Starting with SPSS Syntax

5

S e s s i onA U s i n g S P S

S C omm an d s

The FREQUENCIES command is-

F R E Q U E N C I E S V A R I A B L E S = h a p p y l i f e o r d e r = a n a l y s i s

In its simplest form all you need is a list of the variables-

F r e q u e n c i e s v a r i a b l e s = h a p p y l i f e

The commands in the output window merely document the SPSS session and cant be run from here To run commands we will

need use a syntax window to be discussed soon

Notes in output viewer

The other place you can see a report of the underlying commands is in the Notes

object produced every time you run something a procedure In the Viewer outline

to the left of the output as you look at it you can see a Notes object as part of every

procedures output although by default its contents are hidden Above Figure 6

shows the Notes object as closed or hidden Below in Figure 8 shows the Notes

object open to view

Double click on the Notes icon in the outline to show its contents

Figure 7 Notes from the Frequencies procedure

Notes are quite useful to check details if an SPSS procedure hasnrsquot worked as

you expected it to From the notes you can check to make sure you havent any

weights split le routines or any selection lters switched on You can check that

you have the correct le and syntax and there is a date and time stamp so you can

check when the output was produced

Showing and hiding output objects is discussed in the SPSS 1Getting Started with SPSS course Course Code 1301

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1254

Module 5 Starting with SPSS Syntax

6

S e s s i o n A U

s i n g S P S S C o m m a n d s The syntax window

The syntax window is used to store and run SPSS commands A default installation 1

of SPSS will not open a syntax window Each time you do something with the menus

whether it is to read in data or perform an analysis you have issued a command or

series of commands for SPSS to obey It is possible to paste the SPSS commands

underlying any dialog box into a syntax window using the Paste button - available

on most SPSS dialog boxes It will automatically open up a new syntax2 window

and paste the underlying command(s) from the dialog box into the new window The

contents of a syntax window can be saved to a le so the analysis can be rerun

later without using menus

Figure 8 Frequencies dialog box again

For example if we go back to the Frequencies procedure run earlier we can paste

the underlying FREQUENCIES command into a syntax window

Choose Anaze gt Descripive Saisics gt Freqencies

Click the Carts button and select a bar chart Click Continue to get back

to the Frequencies dialog box

Instead of pressing OK to run Frequencies click on Paste

Figure 9 Frequencies command in the syntax window

SPSS will automatically open up a syntax window and bring it to the front of the

screen with the FREQUENCIES command pasted into it

1 You can change this default using the Edi gt Opionshellip menu under the Gen-

era tab by ticking Open synta window at start-up

2 If a syntax window is already open it will put the command in it without bringing it

to the front

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1354

Module 5 Starting with SPSS Syntax

7

S e s s i onA U s i n g S P S

S C omm an d s

This is the FREQUENCIES command underlying the analysis we ran at the

beginning of the rst exercise You can see the FREQUENCIES command keyword

three subcommand keywords VARIABlES BARChART and ORDER separated

by and two other keywords FREQ and ANAlYSIS And like all commands its

nished with a full stop

To break the command down

Thebull VARIABlES subcommand lists the variables for which we want frequencies

in this example the variables appy and ife

Thebull BARChART subcommand requests a bar chart to be plotted for each of

the variables in the list FREQ species frequencies raw counts rather than

percents on the scale axis

Thebull ORDER subcommand with keyword ANAlYSIS species all the charts after

the tables If the VARIABlE keyword had been used instead the chart for a

particular variable would be placed next to its frequency table

If you want to see all the possible subcommands and other keywords you can use

with any command click the Sow synta cart button on the syntax window

toolbar to look at its syntax chart

Figure 10 The Sow synta cart button

Select the Frequencies command and click on the synta cart button to

see its syntax chart

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1454

Module 5 Starting with SPSS Syntax

8

S e s s i o n A U

s i n g S P S S C o m m a n d s Figure 11 FREQUENCIES syntax chart

As you can see the chart is quite complicated but will give you all the elements you

can put in a FREQUENCIES command You maybe able to spot the subcommands

and keywords just described

Each element in square brackets [ ] indicates a optional element a list of elements

aligned above one another in curly brackets indicates a choice of one out of that

series of elements default choice is usually given in bold All SPSS command sub

command and other keywords are presented in uppercase in chart (although you

dont need to type them in uppercase) Check the syntax reference guide or on-line

help for examples of how each command is used

Once you are nished having a look at the help information close the Helpwindow and you should go back to the Syntax window

The SPSS Command Syntax Reference guide will give you moredetails about SPSS commands and how to run them Syntax

guides are available on the SPSS manual CD or within SPSS in

PDF format from Hep gt Coand Snax Reference

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1554

Module 5 Starting with SPSS Syntax

9

S e s s i onA U s i n g S P S

S C omm an d s

Running an SPSS commandThe Run Current button on the syntax window tool bar looks like an audio play

button Its used to run the command currently selected in the syntax window It is

easy to run an already pasted command either click somewhere on the command

or highlight the entire command (including the full stop) and click the run button

Figure 12 Running the FREQUENCIES command

Click somewhere on the FREQUENCIES command and click the Run

button to execute the command

You will see the procedure being run as you did when you used the menu to run

Frequencies When the processor has nished - it shouldnt take very long - the

results will be stored in the output window as usual

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1654

Module 5 Starting with SPSS Syntax

10

S e s s i o n A U

s i n g S P S S C o m m a n d s Typing in a command

The Syntax window can be used on its own to run SPSS commands if you know

what the commands are (or have generated them from the menus) A new syntax

window can be opened by choosing Fie gt New gt Synta or an existing one can be

opened using Fie gt Open gt Synta A syntax window can be brought to the front by

choosing it from the Windows menu if it is already open

Select the Syntax window from the Window menuWindow gt 3 Snax1 ndash SPSS Snax Edior

The rst syntax window is usually called Synta1 and the 3 before the name

indicates its the third open SPSS window after the data editor and output window If

you have more windows open then the number might be different

The CROSSTABS command

The following CROSSTABS command will cross-tabulate the appy variable by the

ife variable from the social survey dataset

c r o s s t a b s h a p p y b y l i f e

It is a simple form of the CROSSTABS command with no subcommands

Type in the CROSSTABS command crosstabs appy by ife into the

Syntax window

Figure 13 CROSSTABS command selected

A full stop is needed to indicate the end of each SPSS commandIf you forget to nish a command with a full stop the command

may not run properly

Select the whole CROSSTABS command as shown above and click on the

Run button to run it

If you switched on Dispay coands in te og demonstrated on page 2 then

the commands are displayed with the output then you will see the CROSSTABS

command just before the output from Crosstabs

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1754

Module 5 Starting with SPSS Syntax

11

S e s s i onA U s i n g S P S

S C omm an d s

Figure 14 Start of Output from the CROSSTABS command

The CROSSTABS command has several subcommands try the syntax chart button

to see what they are They include

CEllS used to change the content of the table

BARChART to add a clustered bar chart for each layer

Figure 15 CROSSTABS using CEllS and BARChART subcommands

Try typing in and running the new command in Figure 15

Layers can be added to the table by adding another BY keyword follwed by the layer

variable or variable list For example

c r o s s t a b s h a p p y b y l i f e b y r e g i o n

will produce a crosstabs table layer for each region Try adding a layer to the latest

crosstabs command and run the command to check the effect

Some command names such as FINISh BEGIN DATA and

END DATA are complete commands which need no further

specication However most commands (including all of the statistical subprograms) have to have a certain amount

of specication and each has its own particular form of

specication so check the syntax guide before you run

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1854

Module 5 Starting with SPSS Syntax

12

S e s s i o n A U

s i n g S P S S C o m m a n d s Slide Content from presentation

SPSS Coand language

Starting with commands

Wy use Coands

More exible

Extra features

Can be saved

And repeated

Te Synta Window

SPSS commands are entered in the Syntax Window

Create a Syntax Window

Either selecting File gt New gt Syntax from the menu bar

Or creating le with sps extension in a text editor

Use an existing syntax window

Either Selecting an open syntax window from the Window menu

Or selecting File gt Open gt Syntax from the menu bar

SPSS coands

Commands are entered in the Syntax Window by

Either opening the syntax Window and typing directly

Or building up a command using dialog boxes then clicking the

PASTE button

Or using a mixture of the two

Forat of Coands

Control Field - consists of SPSS keyword

Specication Field - names of variables etc on which command willoperate

Full stop - to terminate command

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1954

Module 5 Starting with SPSS Syntax

13

S e s s i onA U s i n g S P S

S C omm an d s

Synta Rues

Commands start with a command keyword

Commands are case insensitive

Except for text in quotes

A single command can go over many syntax lines

Each line of syntax must not exceed 256 bytes3

Do not break a syntax line

in a variable name

in between quotes lsquo lsquo or ldquo ldquo

For a syntax window

Each command must start on a new line

Each command must end with a full stop with no non-blanks after it

or end with a completely blank line afterwards

For batc coands (see ast session)

Each command begins on a new line in column 1

unless you use + or -

Continuation lines mustnrsquot start in column 1

Full stops command terminators are optional

Te FINISh coand

FINISH

For batch commands

Terminates a le of SPSS commands and closes SPSS tidily

NB When SPSS windows manager is running (eg during an SPSS

for Windows session) FINISH will generate an error

Terminate an interactive SPSS session using Exit from the File menu

3 256 bytes are characters in single byte languageshellip

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2054

Module 5 Starting with SPSS Syntax

14

S e s s i o n A U

s i n g S P S S C o m m a n d s

Session summaryIn this session we covered

How to display SPSS commands in output window as they are runbull

How using thebull Paste button in an SPSS dialog box pastes the underlying

command into a synta window

The Syntax Window where SPSS commands (also calledbull synta) are stored

Using the syntax chart button to get help about a command in the syntax windowbull

How to type in and run an SPSS command from thebull synta window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2154

Session B

SPSS CommandStructure

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2254

Module 5 Starting with SPSS Syntax

16

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Commands Subcommands amp keywords

In general an SPSS command will be a mixture of different elements like this -

v kyw kyw kyw

An SPSS command starts with a recognized command keyword which has to

be spelt correctly Each command has its own general format with its own set of

subcommands and keywords These are combined with lists of variables names

or combinations of variable names and keywords to specify the analysis or action

required by the computer

Subcommands and other keywords are words recognized by SPSS as part of

the commands specication and so appear in the specication eld Some

SPSS procedures have subcommands which can appear either on their own or in

association with certain keywords As with commands subcommands and keywords

have to appear exactly as they are spelt

Figure 17 Different keywords in the Commands example

get FILE=mspssworkCARDIACsav

crosstabs chd B Y famhist CELLS=C O U N T R O W C O L U M N STATISTICS=C H I S Q

requencies VARIABLES=height cigaret

FORMAT=L I M I T (12 ) HIST

graPH SCATTERPLOT weight W I T H height

inisH

The different types of keyword have been highlighted as follows-

command keywords SUBCOMMAND keywords

O T H E R keywords variable names and other specication

Abbreviating keywords

SPSS will recognise most of its command and subcommand keywords when

abbreviated to the rst three or four characters of the name as long as the keywordsare unique So for example-

freq vars=height cigaret form=lim(12) hist

can be used as an abbreviation of-

frequencies variables=height cigaret

format=limit(12)

histogram

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2354

Module 5 SPSS command Structure

17

S e s s i onB S P S S C om

m an d S t r u c t ur e

Reserved keywords

With most procedures it does not matter if there is a variable name the same as a

keyword since there are certain places in a command specication where a variable

or variable list is expected and others where keywords are expected However there

are some reserved keywords which cannot be used as names These are

ALL AND BY EQ GE GT LE LT NE NOT OR TO WITH

These words cannot be used as variable names in SPSS because the program

already recognizes them as having meaning For example

FREQUENCIES VARIABLES=ALL

calculates a frequency table for all the variables currently stored on SPSSs

active le To have a variable called A L L would make the sense of the instruction

ambiguous to SPSS

Lists of Variables

The variables are kept in the SPSS active le in the order they are given in the

DATA lIST command and any variables generated during the session in the order

they were created This order can be utilized when instead of typing a long list of

consecutive variables you type the name of the rst variable then the TO keyword

and then the name of the last variable in the list These inclusive ranges of variables

can be combined with the names of other variables to produce a larger variable list

where needed For example if we had the following variables in a system le-

LIST OF VARIABLES ON THE ACTIVE FILE

1 AGE 6 HHLDTYPE 11 OCPENAMT 16 SPSCLASS

2 CHRONILL 7 HOMEHELP 12 REBTOWN 17 TOTINCID

3 CLASS 8 KINNEAR 13 REBTRENT 18 UNEARNED

4 EMPLOYED 9 MARITAL 14 GENDER 19 CASWGT

5 HEALTH 10 NIRAMT 15 SPSAGE

In SPSS variable lists individual variable names andor variable ranges can

be separated by either spaces or commas However any keywords have to be

separated from variable names by spaces ie use spaces with TO or All

AGE TO CASWGT implies

AGE CHRONILL CLASS EMPLOYED HEALTH HHLDTYPEHOMEHELP KINNEAR MARITAL NIRAMT OCPENAMT REBTOWNREBTRENT GENDER SPSAGE SPSCLASS TOTINCID UNEARNEDCASWGT

This rst list is all the variables in the active le since AGE is the rst variable on the

list and CASWGT is the last

hhlDTYPE TO GENDER implies

HHLDTYPE HOMEHELP KINNEAR MARITAL NIRAMT OCPENAMTREBTOWN REBTRENT GENDER

Notice in this list 7 of the 9 variables are implied in the same order as the active le

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2454

Module 5 Starting with SPSS Syntax

18

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e ClASS hEAlTh TO NIRAmT implies

CLASS HEALTH HHLDTYPE HOMEHELP KINNEAR MARITALNIRAMT

A list can be combined with individual variables

AGE SPAGE ClASS SPClASS All implies

AGE SPSAGE CLASS SPSCLASS CHRONILLEMPLOYED HEALTH HHLDTYPE HOMEHELP KINNEARMARITAL NIRAMT OCPENAMT REBTOWN REBTRENT GENDERTOTINCID UNEARNED CASWGT

The reserved keyword ALL in this context means all the variables in the active le

It can be used in a variable list for the KEEP subcommand on a GET or SAVE

command to change the order the variables are saved in whilst making sure no

variables are missed Variables not specically listed will appear after all the listed

ones in active le order

If new variables are created during a session they will be added to the active le in

the order they were created after the variables already stored in the active le You

can always check the variable order in the active le using the DISPlAY command -

it may not be the same order they will appear in dialog boxes

Syntax Charts

The Sow synta cart button as seen in the previous session in Figure 11 will let

you look at command syntax charts If you have no command selected in the syntax

window then the button will give you an alphabetic list of all the command keywords

Look at the syntax chart for each of these commands to see what subcommands and keywords are available

Use the Search button to look for more information about each

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2554

Module 5 SPSS command Structure

19

S e s s i onB S P S S C om

m an d S t r u c t ur e

Some SPSS Command examples (from slides)

Te GET coand

The GET command reads a sav le into SPSS

GET FILE = cardiacsav

Te GET coand - KEEP and DROP subcoands

Use the KEEP subcommand to select only some of the variables

GET FILE=cardiacsavKEEP=famhist chd

Use the DROP subcommand to select all of the le except certain

variables

GET FILE=cardiacsavDROP=height weight

Te DATASET NAmE coand

The DATASET NAME command give the active dataset a short name

which can be used in later commands

GET FILE = mspssworkcardiacsav

DATASET NAME cardiac

Te SAVE coand

Use the SAVE command to save your data in a sav le

SAVE OUTFILE=myfilesav

Te SAVE coand wi

save all your cases together with variable names values

missing values variable and value labels and formats

Saved in the state they are in when the command is issued

Any further changes can only be saved with another SAVE command

It will NOT save the results of any analyses results are saved in an

output window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2654

Module 5 Starting with SPSS Syntax

20

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Te SAVE coand KEEP and DROP subcoands

The KEEP and DROP subcommands are available with the SAVE

command and perform the same function as with GET

Using KEEP will save only the named variables

SAVE OUTFILE=filesav KEEP=AGE HEIGHT

Using DROP will save all variables except named ones (here it means

all variables except WEIGHT2)

SAVE OUTFILE=filesav DROP=WEIGHT2

FREQUENCIES coand

FREQUENCIES VARIABLES=HEIGHT

FREQUENCIES - STATISTICS subcoand

FREQ VAR=HEIGHT AGE STATISTICS

Gives four default statistics

MEAN STDDEV MINIMUM MAXIMUM

FREQUENCIES - more statistics

MEDIAN MODE SUM RANGE SEMEAN VARIANCESKEWNESS SESKEW KURTOSIS SEKURT

STATISTICS=DEFAULT

Use to get the default four statistics MEAN STDDEV MINIMUM

MAXIMUM together with others you ask for

STATISTICS=ALL

Gives all available statistics - can be wasteful

STATISTICS=NONE

Equivalent to omitting STATISTICS altogether

FREQUENCIES - FORmAT subcoand

To suppress the frequency table use

FORMAT=NOTABLE

To suppress tables over a certain length

FORMAT=LIMIT(50)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2754

Module 5 SPSS command Structure

21

S e s s i onB S P S S C om

m an d S t r u c t ur e

FREQUENCIES - subcoands for carts

There are three subcommands to produce type of charts named

HISTOGRAM

BARCHART

PIECHART

Using ore tan one subcoand

To use more than one subcommand string them together and nish

the command with the terminating full stop

FREQUENCIES VARIABLES=AGESEX STATISTICS=RANGEBARCHART

CROSSTABS coand

CROSSTABS TABLES=AGE BY SEX

CROSSTABS AGE BY SEX

SEX BY WEIGHT

The next three crosstabs commands produce the same three tables

CROSSTABS AGE BY SEX

AGE BY PARTY

AGE BY INCOME

CROSSTABS AGE BY SEX PARTY INCOMECROSSTABS AGE BY SEX TO INCOME

CROSSTABS - STATISTICS subcoand

CROSSTABS HEIGHT BY AGE

STATISTICS

Gives the Chi-square statistic tests

CROSSTABS - more statistics

Others available

CHISQ PHI CC LAMBDA UC BTAU CTAU GAMMA DETA CORR ALL NONE

and KAPPA RISK MCNEMAR CMH(1)

Usage

CROSSTABS TABLES=AGE BY WEIGHT

STATISTICS=BTAU CORR

Only gives those statistics asked for

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 6: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 654

v

C o n t e n t s ( c o n t i n u e d )

Module 5 Starting with SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 754

Session A

Using SPSSCommands

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 854

Module 5 Starting with SPSS Syntax

2

S e s s i o n A U

s i n g S P S S C o m m a n d s Using SPSS commands

SPSS command language lies behind any work you do with SPSS although since

most analyses can be done using the menus in the normal course of events you

need never encounter any SPSS commands directly In the dim and distant past the

only way to use SPSS was via its command language (also called syntax) In the

present using a default installation you will only see command names very briey at

the bottom of the window in the status bar while the relevant procedures are being

run by SPSS

However it is easy to see the underlying commands and to generate SPSS

commands from the interactive menus and dialog boxes SPSS commands are

stored in a Synta window which is a plain text le usually with a sps extension

Keeping command syntax gives you the ability to perform the same tasks again and

again

SPSS commands in the output viewer

You can choose to have the underlying commands displayed in a log along with the

output in the output viewer using the Viewer tab in Options from the Edit menu

Start up SPSS and choose Type in data so the data editor is empty

Choose Edi gt Opions to open the Options dialog box

Figure 1 Options dialog box (Genera tab showing)

Click on the Viewer tab to look at the Viewer options

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 954

Module 5 Starting with SPSS Syntax

3

S e s s i onA U s i n g S P S

S C omm an d s

Figure 2 Options dialog box (Viewer tab showing)

Tick Dispay coands in te og box under Initia Output State heading

to display commands Click OK or Appy

Figure 3 Options detail Dispay coands ticked

Once this feature has been switched on the underlying SPSS commands for

anything you do in SPSS will be pasted in the output window before the output

itself For example in Figure 4 you should see a GET command followed by the

DATASET command in the output window

Open the 1991 US Genera Socia Surveysav data le from the SPSS

directory (using Fie gt Open gt Daa) and look at the output window

Figure 4 Output viewer with GET command

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1054

Module 5 Starting with SPSS Syntax

4

S e s s i o n A U

s i n g S P S S C o m m a n d s The full GET command in the output is

getfile=EProgramFilesSPSS141991USGeneralSocialSurveysav

The GET command reads an SPSS data le into the data editor Its general form is-

G E T F I L E = phfl

Earlier versions of SPSS needed to have an ExECUTE command after the GET command so SPSS would run the GET

command and read the data into the data editor

The other command the DATASET command is used to name the active data

window Its not absolutely necessary but it looks like

D A T A S E T N A M E D a t a s e t 1 W I N D O W = F R O N T

Now we will run a FREQUENCIES procedure

Select Anaze gt Descripive Saisics gt Freqencies and

choose the variables shown in Figure 5

Figure 5 Frequencies dialog box

Click OK to run the Frequencies and look at the output

Figure 6 Frequencies command preceding Frequencies output

The FREQUENCIES command can be seen after the GET and DATASET

commands and before the title at the start of the Frequencies output

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1154

Module 5 Starting with SPSS Syntax

5

S e s s i onA U s i n g S P S

S C omm an d s

The FREQUENCIES command is-

F R E Q U E N C I E S V A R I A B L E S = h a p p y l i f e o r d e r = a n a l y s i s

In its simplest form all you need is a list of the variables-

F r e q u e n c i e s v a r i a b l e s = h a p p y l i f e

The commands in the output window merely document the SPSS session and cant be run from here To run commands we will

need use a syntax window to be discussed soon

Notes in output viewer

The other place you can see a report of the underlying commands is in the Notes

object produced every time you run something a procedure In the Viewer outline

to the left of the output as you look at it you can see a Notes object as part of every

procedures output although by default its contents are hidden Above Figure 6

shows the Notes object as closed or hidden Below in Figure 8 shows the Notes

object open to view

Double click on the Notes icon in the outline to show its contents

Figure 7 Notes from the Frequencies procedure

Notes are quite useful to check details if an SPSS procedure hasnrsquot worked as

you expected it to From the notes you can check to make sure you havent any

weights split le routines or any selection lters switched on You can check that

you have the correct le and syntax and there is a date and time stamp so you can

check when the output was produced

Showing and hiding output objects is discussed in the SPSS 1Getting Started with SPSS course Course Code 1301

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1254

Module 5 Starting with SPSS Syntax

6

S e s s i o n A U

s i n g S P S S C o m m a n d s The syntax window

The syntax window is used to store and run SPSS commands A default installation 1

of SPSS will not open a syntax window Each time you do something with the menus

whether it is to read in data or perform an analysis you have issued a command or

series of commands for SPSS to obey It is possible to paste the SPSS commands

underlying any dialog box into a syntax window using the Paste button - available

on most SPSS dialog boxes It will automatically open up a new syntax2 window

and paste the underlying command(s) from the dialog box into the new window The

contents of a syntax window can be saved to a le so the analysis can be rerun

later without using menus

Figure 8 Frequencies dialog box again

For example if we go back to the Frequencies procedure run earlier we can paste

the underlying FREQUENCIES command into a syntax window

Choose Anaze gt Descripive Saisics gt Freqencies

Click the Carts button and select a bar chart Click Continue to get back

to the Frequencies dialog box

Instead of pressing OK to run Frequencies click on Paste

Figure 9 Frequencies command in the syntax window

SPSS will automatically open up a syntax window and bring it to the front of the

screen with the FREQUENCIES command pasted into it

1 You can change this default using the Edi gt Opionshellip menu under the Gen-

era tab by ticking Open synta window at start-up

2 If a syntax window is already open it will put the command in it without bringing it

to the front

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1354

Module 5 Starting with SPSS Syntax

7

S e s s i onA U s i n g S P S

S C omm an d s

This is the FREQUENCIES command underlying the analysis we ran at the

beginning of the rst exercise You can see the FREQUENCIES command keyword

three subcommand keywords VARIABlES BARChART and ORDER separated

by and two other keywords FREQ and ANAlYSIS And like all commands its

nished with a full stop

To break the command down

Thebull VARIABlES subcommand lists the variables for which we want frequencies

in this example the variables appy and ife

Thebull BARChART subcommand requests a bar chart to be plotted for each of

the variables in the list FREQ species frequencies raw counts rather than

percents on the scale axis

Thebull ORDER subcommand with keyword ANAlYSIS species all the charts after

the tables If the VARIABlE keyword had been used instead the chart for a

particular variable would be placed next to its frequency table

If you want to see all the possible subcommands and other keywords you can use

with any command click the Sow synta cart button on the syntax window

toolbar to look at its syntax chart

Figure 10 The Sow synta cart button

Select the Frequencies command and click on the synta cart button to

see its syntax chart

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1454

Module 5 Starting with SPSS Syntax

8

S e s s i o n A U

s i n g S P S S C o m m a n d s Figure 11 FREQUENCIES syntax chart

As you can see the chart is quite complicated but will give you all the elements you

can put in a FREQUENCIES command You maybe able to spot the subcommands

and keywords just described

Each element in square brackets [ ] indicates a optional element a list of elements

aligned above one another in curly brackets indicates a choice of one out of that

series of elements default choice is usually given in bold All SPSS command sub

command and other keywords are presented in uppercase in chart (although you

dont need to type them in uppercase) Check the syntax reference guide or on-line

help for examples of how each command is used

Once you are nished having a look at the help information close the Helpwindow and you should go back to the Syntax window

The SPSS Command Syntax Reference guide will give you moredetails about SPSS commands and how to run them Syntax

guides are available on the SPSS manual CD or within SPSS in

PDF format from Hep gt Coand Snax Reference

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1554

Module 5 Starting with SPSS Syntax

9

S e s s i onA U s i n g S P S

S C omm an d s

Running an SPSS commandThe Run Current button on the syntax window tool bar looks like an audio play

button Its used to run the command currently selected in the syntax window It is

easy to run an already pasted command either click somewhere on the command

or highlight the entire command (including the full stop) and click the run button

Figure 12 Running the FREQUENCIES command

Click somewhere on the FREQUENCIES command and click the Run

button to execute the command

You will see the procedure being run as you did when you used the menu to run

Frequencies When the processor has nished - it shouldnt take very long - the

results will be stored in the output window as usual

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1654

Module 5 Starting with SPSS Syntax

10

S e s s i o n A U

s i n g S P S S C o m m a n d s Typing in a command

The Syntax window can be used on its own to run SPSS commands if you know

what the commands are (or have generated them from the menus) A new syntax

window can be opened by choosing Fie gt New gt Synta or an existing one can be

opened using Fie gt Open gt Synta A syntax window can be brought to the front by

choosing it from the Windows menu if it is already open

Select the Syntax window from the Window menuWindow gt 3 Snax1 ndash SPSS Snax Edior

The rst syntax window is usually called Synta1 and the 3 before the name

indicates its the third open SPSS window after the data editor and output window If

you have more windows open then the number might be different

The CROSSTABS command

The following CROSSTABS command will cross-tabulate the appy variable by the

ife variable from the social survey dataset

c r o s s t a b s h a p p y b y l i f e

It is a simple form of the CROSSTABS command with no subcommands

Type in the CROSSTABS command crosstabs appy by ife into the

Syntax window

Figure 13 CROSSTABS command selected

A full stop is needed to indicate the end of each SPSS commandIf you forget to nish a command with a full stop the command

may not run properly

Select the whole CROSSTABS command as shown above and click on the

Run button to run it

If you switched on Dispay coands in te og demonstrated on page 2 then

the commands are displayed with the output then you will see the CROSSTABS

command just before the output from Crosstabs

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1754

Module 5 Starting with SPSS Syntax

11

S e s s i onA U s i n g S P S

S C omm an d s

Figure 14 Start of Output from the CROSSTABS command

The CROSSTABS command has several subcommands try the syntax chart button

to see what they are They include

CEllS used to change the content of the table

BARChART to add a clustered bar chart for each layer

Figure 15 CROSSTABS using CEllS and BARChART subcommands

Try typing in and running the new command in Figure 15

Layers can be added to the table by adding another BY keyword follwed by the layer

variable or variable list For example

c r o s s t a b s h a p p y b y l i f e b y r e g i o n

will produce a crosstabs table layer for each region Try adding a layer to the latest

crosstabs command and run the command to check the effect

Some command names such as FINISh BEGIN DATA and

END DATA are complete commands which need no further

specication However most commands (including all of the statistical subprograms) have to have a certain amount

of specication and each has its own particular form of

specication so check the syntax guide before you run

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1854

Module 5 Starting with SPSS Syntax

12

S e s s i o n A U

s i n g S P S S C o m m a n d s Slide Content from presentation

SPSS Coand language

Starting with commands

Wy use Coands

More exible

Extra features

Can be saved

And repeated

Te Synta Window

SPSS commands are entered in the Syntax Window

Create a Syntax Window

Either selecting File gt New gt Syntax from the menu bar

Or creating le with sps extension in a text editor

Use an existing syntax window

Either Selecting an open syntax window from the Window menu

Or selecting File gt Open gt Syntax from the menu bar

SPSS coands

Commands are entered in the Syntax Window by

Either opening the syntax Window and typing directly

Or building up a command using dialog boxes then clicking the

PASTE button

Or using a mixture of the two

Forat of Coands

Control Field - consists of SPSS keyword

Specication Field - names of variables etc on which command willoperate

Full stop - to terminate command

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1954

Module 5 Starting with SPSS Syntax

13

S e s s i onA U s i n g S P S

S C omm an d s

Synta Rues

Commands start with a command keyword

Commands are case insensitive

Except for text in quotes

A single command can go over many syntax lines

Each line of syntax must not exceed 256 bytes3

Do not break a syntax line

in a variable name

in between quotes lsquo lsquo or ldquo ldquo

For a syntax window

Each command must start on a new line

Each command must end with a full stop with no non-blanks after it

or end with a completely blank line afterwards

For batc coands (see ast session)

Each command begins on a new line in column 1

unless you use + or -

Continuation lines mustnrsquot start in column 1

Full stops command terminators are optional

Te FINISh coand

FINISH

For batch commands

Terminates a le of SPSS commands and closes SPSS tidily

NB When SPSS windows manager is running (eg during an SPSS

for Windows session) FINISH will generate an error

Terminate an interactive SPSS session using Exit from the File menu

3 256 bytes are characters in single byte languageshellip

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2054

Module 5 Starting with SPSS Syntax

14

S e s s i o n A U

s i n g S P S S C o m m a n d s

Session summaryIn this session we covered

How to display SPSS commands in output window as they are runbull

How using thebull Paste button in an SPSS dialog box pastes the underlying

command into a synta window

The Syntax Window where SPSS commands (also calledbull synta) are stored

Using the syntax chart button to get help about a command in the syntax windowbull

How to type in and run an SPSS command from thebull synta window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2154

Session B

SPSS CommandStructure

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2254

Module 5 Starting with SPSS Syntax

16

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Commands Subcommands amp keywords

In general an SPSS command will be a mixture of different elements like this -

v kyw kyw kyw

An SPSS command starts with a recognized command keyword which has to

be spelt correctly Each command has its own general format with its own set of

subcommands and keywords These are combined with lists of variables names

or combinations of variable names and keywords to specify the analysis or action

required by the computer

Subcommands and other keywords are words recognized by SPSS as part of

the commands specication and so appear in the specication eld Some

SPSS procedures have subcommands which can appear either on their own or in

association with certain keywords As with commands subcommands and keywords

have to appear exactly as they are spelt

Figure 17 Different keywords in the Commands example

get FILE=mspssworkCARDIACsav

crosstabs chd B Y famhist CELLS=C O U N T R O W C O L U M N STATISTICS=C H I S Q

requencies VARIABLES=height cigaret

FORMAT=L I M I T (12 ) HIST

graPH SCATTERPLOT weight W I T H height

inisH

The different types of keyword have been highlighted as follows-

command keywords SUBCOMMAND keywords

O T H E R keywords variable names and other specication

Abbreviating keywords

SPSS will recognise most of its command and subcommand keywords when

abbreviated to the rst three or four characters of the name as long as the keywordsare unique So for example-

freq vars=height cigaret form=lim(12) hist

can be used as an abbreviation of-

frequencies variables=height cigaret

format=limit(12)

histogram

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2354

Module 5 SPSS command Structure

17

S e s s i onB S P S S C om

m an d S t r u c t ur e

Reserved keywords

With most procedures it does not matter if there is a variable name the same as a

keyword since there are certain places in a command specication where a variable

or variable list is expected and others where keywords are expected However there

are some reserved keywords which cannot be used as names These are

ALL AND BY EQ GE GT LE LT NE NOT OR TO WITH

These words cannot be used as variable names in SPSS because the program

already recognizes them as having meaning For example

FREQUENCIES VARIABLES=ALL

calculates a frequency table for all the variables currently stored on SPSSs

active le To have a variable called A L L would make the sense of the instruction

ambiguous to SPSS

Lists of Variables

The variables are kept in the SPSS active le in the order they are given in the

DATA lIST command and any variables generated during the session in the order

they were created This order can be utilized when instead of typing a long list of

consecutive variables you type the name of the rst variable then the TO keyword

and then the name of the last variable in the list These inclusive ranges of variables

can be combined with the names of other variables to produce a larger variable list

where needed For example if we had the following variables in a system le-

LIST OF VARIABLES ON THE ACTIVE FILE

1 AGE 6 HHLDTYPE 11 OCPENAMT 16 SPSCLASS

2 CHRONILL 7 HOMEHELP 12 REBTOWN 17 TOTINCID

3 CLASS 8 KINNEAR 13 REBTRENT 18 UNEARNED

4 EMPLOYED 9 MARITAL 14 GENDER 19 CASWGT

5 HEALTH 10 NIRAMT 15 SPSAGE

In SPSS variable lists individual variable names andor variable ranges can

be separated by either spaces or commas However any keywords have to be

separated from variable names by spaces ie use spaces with TO or All

AGE TO CASWGT implies

AGE CHRONILL CLASS EMPLOYED HEALTH HHLDTYPEHOMEHELP KINNEAR MARITAL NIRAMT OCPENAMT REBTOWNREBTRENT GENDER SPSAGE SPSCLASS TOTINCID UNEARNEDCASWGT

This rst list is all the variables in the active le since AGE is the rst variable on the

list and CASWGT is the last

hhlDTYPE TO GENDER implies

HHLDTYPE HOMEHELP KINNEAR MARITAL NIRAMT OCPENAMTREBTOWN REBTRENT GENDER

Notice in this list 7 of the 9 variables are implied in the same order as the active le

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2454

Module 5 Starting with SPSS Syntax

18

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e ClASS hEAlTh TO NIRAmT implies

CLASS HEALTH HHLDTYPE HOMEHELP KINNEAR MARITALNIRAMT

A list can be combined with individual variables

AGE SPAGE ClASS SPClASS All implies

AGE SPSAGE CLASS SPSCLASS CHRONILLEMPLOYED HEALTH HHLDTYPE HOMEHELP KINNEARMARITAL NIRAMT OCPENAMT REBTOWN REBTRENT GENDERTOTINCID UNEARNED CASWGT

The reserved keyword ALL in this context means all the variables in the active le

It can be used in a variable list for the KEEP subcommand on a GET or SAVE

command to change the order the variables are saved in whilst making sure no

variables are missed Variables not specically listed will appear after all the listed

ones in active le order

If new variables are created during a session they will be added to the active le in

the order they were created after the variables already stored in the active le You

can always check the variable order in the active le using the DISPlAY command -

it may not be the same order they will appear in dialog boxes

Syntax Charts

The Sow synta cart button as seen in the previous session in Figure 11 will let

you look at command syntax charts If you have no command selected in the syntax

window then the button will give you an alphabetic list of all the command keywords

Look at the syntax chart for each of these commands to see what subcommands and keywords are available

Use the Search button to look for more information about each

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2554

Module 5 SPSS command Structure

19

S e s s i onB S P S S C om

m an d S t r u c t ur e

Some SPSS Command examples (from slides)

Te GET coand

The GET command reads a sav le into SPSS

GET FILE = cardiacsav

Te GET coand - KEEP and DROP subcoands

Use the KEEP subcommand to select only some of the variables

GET FILE=cardiacsavKEEP=famhist chd

Use the DROP subcommand to select all of the le except certain

variables

GET FILE=cardiacsavDROP=height weight

Te DATASET NAmE coand

The DATASET NAME command give the active dataset a short name

which can be used in later commands

GET FILE = mspssworkcardiacsav

DATASET NAME cardiac

Te SAVE coand

Use the SAVE command to save your data in a sav le

SAVE OUTFILE=myfilesav

Te SAVE coand wi

save all your cases together with variable names values

missing values variable and value labels and formats

Saved in the state they are in when the command is issued

Any further changes can only be saved with another SAVE command

It will NOT save the results of any analyses results are saved in an

output window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2654

Module 5 Starting with SPSS Syntax

20

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Te SAVE coand KEEP and DROP subcoands

The KEEP and DROP subcommands are available with the SAVE

command and perform the same function as with GET

Using KEEP will save only the named variables

SAVE OUTFILE=filesav KEEP=AGE HEIGHT

Using DROP will save all variables except named ones (here it means

all variables except WEIGHT2)

SAVE OUTFILE=filesav DROP=WEIGHT2

FREQUENCIES coand

FREQUENCIES VARIABLES=HEIGHT

FREQUENCIES - STATISTICS subcoand

FREQ VAR=HEIGHT AGE STATISTICS

Gives four default statistics

MEAN STDDEV MINIMUM MAXIMUM

FREQUENCIES - more statistics

MEDIAN MODE SUM RANGE SEMEAN VARIANCESKEWNESS SESKEW KURTOSIS SEKURT

STATISTICS=DEFAULT

Use to get the default four statistics MEAN STDDEV MINIMUM

MAXIMUM together with others you ask for

STATISTICS=ALL

Gives all available statistics - can be wasteful

STATISTICS=NONE

Equivalent to omitting STATISTICS altogether

FREQUENCIES - FORmAT subcoand

To suppress the frequency table use

FORMAT=NOTABLE

To suppress tables over a certain length

FORMAT=LIMIT(50)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2754

Module 5 SPSS command Structure

21

S e s s i onB S P S S C om

m an d S t r u c t ur e

FREQUENCIES - subcoands for carts

There are three subcommands to produce type of charts named

HISTOGRAM

BARCHART

PIECHART

Using ore tan one subcoand

To use more than one subcommand string them together and nish

the command with the terminating full stop

FREQUENCIES VARIABLES=AGESEX STATISTICS=RANGEBARCHART

CROSSTABS coand

CROSSTABS TABLES=AGE BY SEX

CROSSTABS AGE BY SEX

SEX BY WEIGHT

The next three crosstabs commands produce the same three tables

CROSSTABS AGE BY SEX

AGE BY PARTY

AGE BY INCOME

CROSSTABS AGE BY SEX PARTY INCOMECROSSTABS AGE BY SEX TO INCOME

CROSSTABS - STATISTICS subcoand

CROSSTABS HEIGHT BY AGE

STATISTICS

Gives the Chi-square statistic tests

CROSSTABS - more statistics

Others available

CHISQ PHI CC LAMBDA UC BTAU CTAU GAMMA DETA CORR ALL NONE

and KAPPA RISK MCNEMAR CMH(1)

Usage

CROSSTABS TABLES=AGE BY WEIGHT

STATISTICS=BTAU CORR

Only gives those statistics asked for

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 7: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 754

Session A

Using SPSSCommands

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 854

Module 5 Starting with SPSS Syntax

2

S e s s i o n A U

s i n g S P S S C o m m a n d s Using SPSS commands

SPSS command language lies behind any work you do with SPSS although since

most analyses can be done using the menus in the normal course of events you

need never encounter any SPSS commands directly In the dim and distant past the

only way to use SPSS was via its command language (also called syntax) In the

present using a default installation you will only see command names very briey at

the bottom of the window in the status bar while the relevant procedures are being

run by SPSS

However it is easy to see the underlying commands and to generate SPSS

commands from the interactive menus and dialog boxes SPSS commands are

stored in a Synta window which is a plain text le usually with a sps extension

Keeping command syntax gives you the ability to perform the same tasks again and

again

SPSS commands in the output viewer

You can choose to have the underlying commands displayed in a log along with the

output in the output viewer using the Viewer tab in Options from the Edit menu

Start up SPSS and choose Type in data so the data editor is empty

Choose Edi gt Opions to open the Options dialog box

Figure 1 Options dialog box (Genera tab showing)

Click on the Viewer tab to look at the Viewer options

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 954

Module 5 Starting with SPSS Syntax

3

S e s s i onA U s i n g S P S

S C omm an d s

Figure 2 Options dialog box (Viewer tab showing)

Tick Dispay coands in te og box under Initia Output State heading

to display commands Click OK or Appy

Figure 3 Options detail Dispay coands ticked

Once this feature has been switched on the underlying SPSS commands for

anything you do in SPSS will be pasted in the output window before the output

itself For example in Figure 4 you should see a GET command followed by the

DATASET command in the output window

Open the 1991 US Genera Socia Surveysav data le from the SPSS

directory (using Fie gt Open gt Daa) and look at the output window

Figure 4 Output viewer with GET command

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1054

Module 5 Starting with SPSS Syntax

4

S e s s i o n A U

s i n g S P S S C o m m a n d s The full GET command in the output is

getfile=EProgramFilesSPSS141991USGeneralSocialSurveysav

The GET command reads an SPSS data le into the data editor Its general form is-

G E T F I L E = phfl

Earlier versions of SPSS needed to have an ExECUTE command after the GET command so SPSS would run the GET

command and read the data into the data editor

The other command the DATASET command is used to name the active data

window Its not absolutely necessary but it looks like

D A T A S E T N A M E D a t a s e t 1 W I N D O W = F R O N T

Now we will run a FREQUENCIES procedure

Select Anaze gt Descripive Saisics gt Freqencies and

choose the variables shown in Figure 5

Figure 5 Frequencies dialog box

Click OK to run the Frequencies and look at the output

Figure 6 Frequencies command preceding Frequencies output

The FREQUENCIES command can be seen after the GET and DATASET

commands and before the title at the start of the Frequencies output

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1154

Module 5 Starting with SPSS Syntax

5

S e s s i onA U s i n g S P S

S C omm an d s

The FREQUENCIES command is-

F R E Q U E N C I E S V A R I A B L E S = h a p p y l i f e o r d e r = a n a l y s i s

In its simplest form all you need is a list of the variables-

F r e q u e n c i e s v a r i a b l e s = h a p p y l i f e

The commands in the output window merely document the SPSS session and cant be run from here To run commands we will

need use a syntax window to be discussed soon

Notes in output viewer

The other place you can see a report of the underlying commands is in the Notes

object produced every time you run something a procedure In the Viewer outline

to the left of the output as you look at it you can see a Notes object as part of every

procedures output although by default its contents are hidden Above Figure 6

shows the Notes object as closed or hidden Below in Figure 8 shows the Notes

object open to view

Double click on the Notes icon in the outline to show its contents

Figure 7 Notes from the Frequencies procedure

Notes are quite useful to check details if an SPSS procedure hasnrsquot worked as

you expected it to From the notes you can check to make sure you havent any

weights split le routines or any selection lters switched on You can check that

you have the correct le and syntax and there is a date and time stamp so you can

check when the output was produced

Showing and hiding output objects is discussed in the SPSS 1Getting Started with SPSS course Course Code 1301

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1254

Module 5 Starting with SPSS Syntax

6

S e s s i o n A U

s i n g S P S S C o m m a n d s The syntax window

The syntax window is used to store and run SPSS commands A default installation 1

of SPSS will not open a syntax window Each time you do something with the menus

whether it is to read in data or perform an analysis you have issued a command or

series of commands for SPSS to obey It is possible to paste the SPSS commands

underlying any dialog box into a syntax window using the Paste button - available

on most SPSS dialog boxes It will automatically open up a new syntax2 window

and paste the underlying command(s) from the dialog box into the new window The

contents of a syntax window can be saved to a le so the analysis can be rerun

later without using menus

Figure 8 Frequencies dialog box again

For example if we go back to the Frequencies procedure run earlier we can paste

the underlying FREQUENCIES command into a syntax window

Choose Anaze gt Descripive Saisics gt Freqencies

Click the Carts button and select a bar chart Click Continue to get back

to the Frequencies dialog box

Instead of pressing OK to run Frequencies click on Paste

Figure 9 Frequencies command in the syntax window

SPSS will automatically open up a syntax window and bring it to the front of the

screen with the FREQUENCIES command pasted into it

1 You can change this default using the Edi gt Opionshellip menu under the Gen-

era tab by ticking Open synta window at start-up

2 If a syntax window is already open it will put the command in it without bringing it

to the front

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1354

Module 5 Starting with SPSS Syntax

7

S e s s i onA U s i n g S P S

S C omm an d s

This is the FREQUENCIES command underlying the analysis we ran at the

beginning of the rst exercise You can see the FREQUENCIES command keyword

three subcommand keywords VARIABlES BARChART and ORDER separated

by and two other keywords FREQ and ANAlYSIS And like all commands its

nished with a full stop

To break the command down

Thebull VARIABlES subcommand lists the variables for which we want frequencies

in this example the variables appy and ife

Thebull BARChART subcommand requests a bar chart to be plotted for each of

the variables in the list FREQ species frequencies raw counts rather than

percents on the scale axis

Thebull ORDER subcommand with keyword ANAlYSIS species all the charts after

the tables If the VARIABlE keyword had been used instead the chart for a

particular variable would be placed next to its frequency table

If you want to see all the possible subcommands and other keywords you can use

with any command click the Sow synta cart button on the syntax window

toolbar to look at its syntax chart

Figure 10 The Sow synta cart button

Select the Frequencies command and click on the synta cart button to

see its syntax chart

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1454

Module 5 Starting with SPSS Syntax

8

S e s s i o n A U

s i n g S P S S C o m m a n d s Figure 11 FREQUENCIES syntax chart

As you can see the chart is quite complicated but will give you all the elements you

can put in a FREQUENCIES command You maybe able to spot the subcommands

and keywords just described

Each element in square brackets [ ] indicates a optional element a list of elements

aligned above one another in curly brackets indicates a choice of one out of that

series of elements default choice is usually given in bold All SPSS command sub

command and other keywords are presented in uppercase in chart (although you

dont need to type them in uppercase) Check the syntax reference guide or on-line

help for examples of how each command is used

Once you are nished having a look at the help information close the Helpwindow and you should go back to the Syntax window

The SPSS Command Syntax Reference guide will give you moredetails about SPSS commands and how to run them Syntax

guides are available on the SPSS manual CD or within SPSS in

PDF format from Hep gt Coand Snax Reference

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1554

Module 5 Starting with SPSS Syntax

9

S e s s i onA U s i n g S P S

S C omm an d s

Running an SPSS commandThe Run Current button on the syntax window tool bar looks like an audio play

button Its used to run the command currently selected in the syntax window It is

easy to run an already pasted command either click somewhere on the command

or highlight the entire command (including the full stop) and click the run button

Figure 12 Running the FREQUENCIES command

Click somewhere on the FREQUENCIES command and click the Run

button to execute the command

You will see the procedure being run as you did when you used the menu to run

Frequencies When the processor has nished - it shouldnt take very long - the

results will be stored in the output window as usual

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1654

Module 5 Starting with SPSS Syntax

10

S e s s i o n A U

s i n g S P S S C o m m a n d s Typing in a command

The Syntax window can be used on its own to run SPSS commands if you know

what the commands are (or have generated them from the menus) A new syntax

window can be opened by choosing Fie gt New gt Synta or an existing one can be

opened using Fie gt Open gt Synta A syntax window can be brought to the front by

choosing it from the Windows menu if it is already open

Select the Syntax window from the Window menuWindow gt 3 Snax1 ndash SPSS Snax Edior

The rst syntax window is usually called Synta1 and the 3 before the name

indicates its the third open SPSS window after the data editor and output window If

you have more windows open then the number might be different

The CROSSTABS command

The following CROSSTABS command will cross-tabulate the appy variable by the

ife variable from the social survey dataset

c r o s s t a b s h a p p y b y l i f e

It is a simple form of the CROSSTABS command with no subcommands

Type in the CROSSTABS command crosstabs appy by ife into the

Syntax window

Figure 13 CROSSTABS command selected

A full stop is needed to indicate the end of each SPSS commandIf you forget to nish a command with a full stop the command

may not run properly

Select the whole CROSSTABS command as shown above and click on the

Run button to run it

If you switched on Dispay coands in te og demonstrated on page 2 then

the commands are displayed with the output then you will see the CROSSTABS

command just before the output from Crosstabs

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1754

Module 5 Starting with SPSS Syntax

11

S e s s i onA U s i n g S P S

S C omm an d s

Figure 14 Start of Output from the CROSSTABS command

The CROSSTABS command has several subcommands try the syntax chart button

to see what they are They include

CEllS used to change the content of the table

BARChART to add a clustered bar chart for each layer

Figure 15 CROSSTABS using CEllS and BARChART subcommands

Try typing in and running the new command in Figure 15

Layers can be added to the table by adding another BY keyword follwed by the layer

variable or variable list For example

c r o s s t a b s h a p p y b y l i f e b y r e g i o n

will produce a crosstabs table layer for each region Try adding a layer to the latest

crosstabs command and run the command to check the effect

Some command names such as FINISh BEGIN DATA and

END DATA are complete commands which need no further

specication However most commands (including all of the statistical subprograms) have to have a certain amount

of specication and each has its own particular form of

specication so check the syntax guide before you run

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1854

Module 5 Starting with SPSS Syntax

12

S e s s i o n A U

s i n g S P S S C o m m a n d s Slide Content from presentation

SPSS Coand language

Starting with commands

Wy use Coands

More exible

Extra features

Can be saved

And repeated

Te Synta Window

SPSS commands are entered in the Syntax Window

Create a Syntax Window

Either selecting File gt New gt Syntax from the menu bar

Or creating le with sps extension in a text editor

Use an existing syntax window

Either Selecting an open syntax window from the Window menu

Or selecting File gt Open gt Syntax from the menu bar

SPSS coands

Commands are entered in the Syntax Window by

Either opening the syntax Window and typing directly

Or building up a command using dialog boxes then clicking the

PASTE button

Or using a mixture of the two

Forat of Coands

Control Field - consists of SPSS keyword

Specication Field - names of variables etc on which command willoperate

Full stop - to terminate command

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1954

Module 5 Starting with SPSS Syntax

13

S e s s i onA U s i n g S P S

S C omm an d s

Synta Rues

Commands start with a command keyword

Commands are case insensitive

Except for text in quotes

A single command can go over many syntax lines

Each line of syntax must not exceed 256 bytes3

Do not break a syntax line

in a variable name

in between quotes lsquo lsquo or ldquo ldquo

For a syntax window

Each command must start on a new line

Each command must end with a full stop with no non-blanks after it

or end with a completely blank line afterwards

For batc coands (see ast session)

Each command begins on a new line in column 1

unless you use + or -

Continuation lines mustnrsquot start in column 1

Full stops command terminators are optional

Te FINISh coand

FINISH

For batch commands

Terminates a le of SPSS commands and closes SPSS tidily

NB When SPSS windows manager is running (eg during an SPSS

for Windows session) FINISH will generate an error

Terminate an interactive SPSS session using Exit from the File menu

3 256 bytes are characters in single byte languageshellip

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2054

Module 5 Starting with SPSS Syntax

14

S e s s i o n A U

s i n g S P S S C o m m a n d s

Session summaryIn this session we covered

How to display SPSS commands in output window as they are runbull

How using thebull Paste button in an SPSS dialog box pastes the underlying

command into a synta window

The Syntax Window where SPSS commands (also calledbull synta) are stored

Using the syntax chart button to get help about a command in the syntax windowbull

How to type in and run an SPSS command from thebull synta window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2154

Session B

SPSS CommandStructure

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2254

Module 5 Starting with SPSS Syntax

16

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Commands Subcommands amp keywords

In general an SPSS command will be a mixture of different elements like this -

v kyw kyw kyw

An SPSS command starts with a recognized command keyword which has to

be spelt correctly Each command has its own general format with its own set of

subcommands and keywords These are combined with lists of variables names

or combinations of variable names and keywords to specify the analysis or action

required by the computer

Subcommands and other keywords are words recognized by SPSS as part of

the commands specication and so appear in the specication eld Some

SPSS procedures have subcommands which can appear either on their own or in

association with certain keywords As with commands subcommands and keywords

have to appear exactly as they are spelt

Figure 17 Different keywords in the Commands example

get FILE=mspssworkCARDIACsav

crosstabs chd B Y famhist CELLS=C O U N T R O W C O L U M N STATISTICS=C H I S Q

requencies VARIABLES=height cigaret

FORMAT=L I M I T (12 ) HIST

graPH SCATTERPLOT weight W I T H height

inisH

The different types of keyword have been highlighted as follows-

command keywords SUBCOMMAND keywords

O T H E R keywords variable names and other specication

Abbreviating keywords

SPSS will recognise most of its command and subcommand keywords when

abbreviated to the rst three or four characters of the name as long as the keywordsare unique So for example-

freq vars=height cigaret form=lim(12) hist

can be used as an abbreviation of-

frequencies variables=height cigaret

format=limit(12)

histogram

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2354

Module 5 SPSS command Structure

17

S e s s i onB S P S S C om

m an d S t r u c t ur e

Reserved keywords

With most procedures it does not matter if there is a variable name the same as a

keyword since there are certain places in a command specication where a variable

or variable list is expected and others where keywords are expected However there

are some reserved keywords which cannot be used as names These are

ALL AND BY EQ GE GT LE LT NE NOT OR TO WITH

These words cannot be used as variable names in SPSS because the program

already recognizes them as having meaning For example

FREQUENCIES VARIABLES=ALL

calculates a frequency table for all the variables currently stored on SPSSs

active le To have a variable called A L L would make the sense of the instruction

ambiguous to SPSS

Lists of Variables

The variables are kept in the SPSS active le in the order they are given in the

DATA lIST command and any variables generated during the session in the order

they were created This order can be utilized when instead of typing a long list of

consecutive variables you type the name of the rst variable then the TO keyword

and then the name of the last variable in the list These inclusive ranges of variables

can be combined with the names of other variables to produce a larger variable list

where needed For example if we had the following variables in a system le-

LIST OF VARIABLES ON THE ACTIVE FILE

1 AGE 6 HHLDTYPE 11 OCPENAMT 16 SPSCLASS

2 CHRONILL 7 HOMEHELP 12 REBTOWN 17 TOTINCID

3 CLASS 8 KINNEAR 13 REBTRENT 18 UNEARNED

4 EMPLOYED 9 MARITAL 14 GENDER 19 CASWGT

5 HEALTH 10 NIRAMT 15 SPSAGE

In SPSS variable lists individual variable names andor variable ranges can

be separated by either spaces or commas However any keywords have to be

separated from variable names by spaces ie use spaces with TO or All

AGE TO CASWGT implies

AGE CHRONILL CLASS EMPLOYED HEALTH HHLDTYPEHOMEHELP KINNEAR MARITAL NIRAMT OCPENAMT REBTOWNREBTRENT GENDER SPSAGE SPSCLASS TOTINCID UNEARNEDCASWGT

This rst list is all the variables in the active le since AGE is the rst variable on the

list and CASWGT is the last

hhlDTYPE TO GENDER implies

HHLDTYPE HOMEHELP KINNEAR MARITAL NIRAMT OCPENAMTREBTOWN REBTRENT GENDER

Notice in this list 7 of the 9 variables are implied in the same order as the active le

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2454

Module 5 Starting with SPSS Syntax

18

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e ClASS hEAlTh TO NIRAmT implies

CLASS HEALTH HHLDTYPE HOMEHELP KINNEAR MARITALNIRAMT

A list can be combined with individual variables

AGE SPAGE ClASS SPClASS All implies

AGE SPSAGE CLASS SPSCLASS CHRONILLEMPLOYED HEALTH HHLDTYPE HOMEHELP KINNEARMARITAL NIRAMT OCPENAMT REBTOWN REBTRENT GENDERTOTINCID UNEARNED CASWGT

The reserved keyword ALL in this context means all the variables in the active le

It can be used in a variable list for the KEEP subcommand on a GET or SAVE

command to change the order the variables are saved in whilst making sure no

variables are missed Variables not specically listed will appear after all the listed

ones in active le order

If new variables are created during a session they will be added to the active le in

the order they were created after the variables already stored in the active le You

can always check the variable order in the active le using the DISPlAY command -

it may not be the same order they will appear in dialog boxes

Syntax Charts

The Sow synta cart button as seen in the previous session in Figure 11 will let

you look at command syntax charts If you have no command selected in the syntax

window then the button will give you an alphabetic list of all the command keywords

Look at the syntax chart for each of these commands to see what subcommands and keywords are available

Use the Search button to look for more information about each

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2554

Module 5 SPSS command Structure

19

S e s s i onB S P S S C om

m an d S t r u c t ur e

Some SPSS Command examples (from slides)

Te GET coand

The GET command reads a sav le into SPSS

GET FILE = cardiacsav

Te GET coand - KEEP and DROP subcoands

Use the KEEP subcommand to select only some of the variables

GET FILE=cardiacsavKEEP=famhist chd

Use the DROP subcommand to select all of the le except certain

variables

GET FILE=cardiacsavDROP=height weight

Te DATASET NAmE coand

The DATASET NAME command give the active dataset a short name

which can be used in later commands

GET FILE = mspssworkcardiacsav

DATASET NAME cardiac

Te SAVE coand

Use the SAVE command to save your data in a sav le

SAVE OUTFILE=myfilesav

Te SAVE coand wi

save all your cases together with variable names values

missing values variable and value labels and formats

Saved in the state they are in when the command is issued

Any further changes can only be saved with another SAVE command

It will NOT save the results of any analyses results are saved in an

output window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2654

Module 5 Starting with SPSS Syntax

20

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Te SAVE coand KEEP and DROP subcoands

The KEEP and DROP subcommands are available with the SAVE

command and perform the same function as with GET

Using KEEP will save only the named variables

SAVE OUTFILE=filesav KEEP=AGE HEIGHT

Using DROP will save all variables except named ones (here it means

all variables except WEIGHT2)

SAVE OUTFILE=filesav DROP=WEIGHT2

FREQUENCIES coand

FREQUENCIES VARIABLES=HEIGHT

FREQUENCIES - STATISTICS subcoand

FREQ VAR=HEIGHT AGE STATISTICS

Gives four default statistics

MEAN STDDEV MINIMUM MAXIMUM

FREQUENCIES - more statistics

MEDIAN MODE SUM RANGE SEMEAN VARIANCESKEWNESS SESKEW KURTOSIS SEKURT

STATISTICS=DEFAULT

Use to get the default four statistics MEAN STDDEV MINIMUM

MAXIMUM together with others you ask for

STATISTICS=ALL

Gives all available statistics - can be wasteful

STATISTICS=NONE

Equivalent to omitting STATISTICS altogether

FREQUENCIES - FORmAT subcoand

To suppress the frequency table use

FORMAT=NOTABLE

To suppress tables over a certain length

FORMAT=LIMIT(50)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2754

Module 5 SPSS command Structure

21

S e s s i onB S P S S C om

m an d S t r u c t ur e

FREQUENCIES - subcoands for carts

There are three subcommands to produce type of charts named

HISTOGRAM

BARCHART

PIECHART

Using ore tan one subcoand

To use more than one subcommand string them together and nish

the command with the terminating full stop

FREQUENCIES VARIABLES=AGESEX STATISTICS=RANGEBARCHART

CROSSTABS coand

CROSSTABS TABLES=AGE BY SEX

CROSSTABS AGE BY SEX

SEX BY WEIGHT

The next three crosstabs commands produce the same three tables

CROSSTABS AGE BY SEX

AGE BY PARTY

AGE BY INCOME

CROSSTABS AGE BY SEX PARTY INCOMECROSSTABS AGE BY SEX TO INCOME

CROSSTABS - STATISTICS subcoand

CROSSTABS HEIGHT BY AGE

STATISTICS

Gives the Chi-square statistic tests

CROSSTABS - more statistics

Others available

CHISQ PHI CC LAMBDA UC BTAU CTAU GAMMA DETA CORR ALL NONE

and KAPPA RISK MCNEMAR CMH(1)

Usage

CROSSTABS TABLES=AGE BY WEIGHT

STATISTICS=BTAU CORR

Only gives those statistics asked for

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 8: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 854

Module 5 Starting with SPSS Syntax

2

S e s s i o n A U

s i n g S P S S C o m m a n d s Using SPSS commands

SPSS command language lies behind any work you do with SPSS although since

most analyses can be done using the menus in the normal course of events you

need never encounter any SPSS commands directly In the dim and distant past the

only way to use SPSS was via its command language (also called syntax) In the

present using a default installation you will only see command names very briey at

the bottom of the window in the status bar while the relevant procedures are being

run by SPSS

However it is easy to see the underlying commands and to generate SPSS

commands from the interactive menus and dialog boxes SPSS commands are

stored in a Synta window which is a plain text le usually with a sps extension

Keeping command syntax gives you the ability to perform the same tasks again and

again

SPSS commands in the output viewer

You can choose to have the underlying commands displayed in a log along with the

output in the output viewer using the Viewer tab in Options from the Edit menu

Start up SPSS and choose Type in data so the data editor is empty

Choose Edi gt Opions to open the Options dialog box

Figure 1 Options dialog box (Genera tab showing)

Click on the Viewer tab to look at the Viewer options

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 954

Module 5 Starting with SPSS Syntax

3

S e s s i onA U s i n g S P S

S C omm an d s

Figure 2 Options dialog box (Viewer tab showing)

Tick Dispay coands in te og box under Initia Output State heading

to display commands Click OK or Appy

Figure 3 Options detail Dispay coands ticked

Once this feature has been switched on the underlying SPSS commands for

anything you do in SPSS will be pasted in the output window before the output

itself For example in Figure 4 you should see a GET command followed by the

DATASET command in the output window

Open the 1991 US Genera Socia Surveysav data le from the SPSS

directory (using Fie gt Open gt Daa) and look at the output window

Figure 4 Output viewer with GET command

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1054

Module 5 Starting with SPSS Syntax

4

S e s s i o n A U

s i n g S P S S C o m m a n d s The full GET command in the output is

getfile=EProgramFilesSPSS141991USGeneralSocialSurveysav

The GET command reads an SPSS data le into the data editor Its general form is-

G E T F I L E = phfl

Earlier versions of SPSS needed to have an ExECUTE command after the GET command so SPSS would run the GET

command and read the data into the data editor

The other command the DATASET command is used to name the active data

window Its not absolutely necessary but it looks like

D A T A S E T N A M E D a t a s e t 1 W I N D O W = F R O N T

Now we will run a FREQUENCIES procedure

Select Anaze gt Descripive Saisics gt Freqencies and

choose the variables shown in Figure 5

Figure 5 Frequencies dialog box

Click OK to run the Frequencies and look at the output

Figure 6 Frequencies command preceding Frequencies output

The FREQUENCIES command can be seen after the GET and DATASET

commands and before the title at the start of the Frequencies output

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1154

Module 5 Starting with SPSS Syntax

5

S e s s i onA U s i n g S P S

S C omm an d s

The FREQUENCIES command is-

F R E Q U E N C I E S V A R I A B L E S = h a p p y l i f e o r d e r = a n a l y s i s

In its simplest form all you need is a list of the variables-

F r e q u e n c i e s v a r i a b l e s = h a p p y l i f e

The commands in the output window merely document the SPSS session and cant be run from here To run commands we will

need use a syntax window to be discussed soon

Notes in output viewer

The other place you can see a report of the underlying commands is in the Notes

object produced every time you run something a procedure In the Viewer outline

to the left of the output as you look at it you can see a Notes object as part of every

procedures output although by default its contents are hidden Above Figure 6

shows the Notes object as closed or hidden Below in Figure 8 shows the Notes

object open to view

Double click on the Notes icon in the outline to show its contents

Figure 7 Notes from the Frequencies procedure

Notes are quite useful to check details if an SPSS procedure hasnrsquot worked as

you expected it to From the notes you can check to make sure you havent any

weights split le routines or any selection lters switched on You can check that

you have the correct le and syntax and there is a date and time stamp so you can

check when the output was produced

Showing and hiding output objects is discussed in the SPSS 1Getting Started with SPSS course Course Code 1301

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1254

Module 5 Starting with SPSS Syntax

6

S e s s i o n A U

s i n g S P S S C o m m a n d s The syntax window

The syntax window is used to store and run SPSS commands A default installation 1

of SPSS will not open a syntax window Each time you do something with the menus

whether it is to read in data or perform an analysis you have issued a command or

series of commands for SPSS to obey It is possible to paste the SPSS commands

underlying any dialog box into a syntax window using the Paste button - available

on most SPSS dialog boxes It will automatically open up a new syntax2 window

and paste the underlying command(s) from the dialog box into the new window The

contents of a syntax window can be saved to a le so the analysis can be rerun

later without using menus

Figure 8 Frequencies dialog box again

For example if we go back to the Frequencies procedure run earlier we can paste

the underlying FREQUENCIES command into a syntax window

Choose Anaze gt Descripive Saisics gt Freqencies

Click the Carts button and select a bar chart Click Continue to get back

to the Frequencies dialog box

Instead of pressing OK to run Frequencies click on Paste

Figure 9 Frequencies command in the syntax window

SPSS will automatically open up a syntax window and bring it to the front of the

screen with the FREQUENCIES command pasted into it

1 You can change this default using the Edi gt Opionshellip menu under the Gen-

era tab by ticking Open synta window at start-up

2 If a syntax window is already open it will put the command in it without bringing it

to the front

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1354

Module 5 Starting with SPSS Syntax

7

S e s s i onA U s i n g S P S

S C omm an d s

This is the FREQUENCIES command underlying the analysis we ran at the

beginning of the rst exercise You can see the FREQUENCIES command keyword

three subcommand keywords VARIABlES BARChART and ORDER separated

by and two other keywords FREQ and ANAlYSIS And like all commands its

nished with a full stop

To break the command down

Thebull VARIABlES subcommand lists the variables for which we want frequencies

in this example the variables appy and ife

Thebull BARChART subcommand requests a bar chart to be plotted for each of

the variables in the list FREQ species frequencies raw counts rather than

percents on the scale axis

Thebull ORDER subcommand with keyword ANAlYSIS species all the charts after

the tables If the VARIABlE keyword had been used instead the chart for a

particular variable would be placed next to its frequency table

If you want to see all the possible subcommands and other keywords you can use

with any command click the Sow synta cart button on the syntax window

toolbar to look at its syntax chart

Figure 10 The Sow synta cart button

Select the Frequencies command and click on the synta cart button to

see its syntax chart

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1454

Module 5 Starting with SPSS Syntax

8

S e s s i o n A U

s i n g S P S S C o m m a n d s Figure 11 FREQUENCIES syntax chart

As you can see the chart is quite complicated but will give you all the elements you

can put in a FREQUENCIES command You maybe able to spot the subcommands

and keywords just described

Each element in square brackets [ ] indicates a optional element a list of elements

aligned above one another in curly brackets indicates a choice of one out of that

series of elements default choice is usually given in bold All SPSS command sub

command and other keywords are presented in uppercase in chart (although you

dont need to type them in uppercase) Check the syntax reference guide or on-line

help for examples of how each command is used

Once you are nished having a look at the help information close the Helpwindow and you should go back to the Syntax window

The SPSS Command Syntax Reference guide will give you moredetails about SPSS commands and how to run them Syntax

guides are available on the SPSS manual CD or within SPSS in

PDF format from Hep gt Coand Snax Reference

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1554

Module 5 Starting with SPSS Syntax

9

S e s s i onA U s i n g S P S

S C omm an d s

Running an SPSS commandThe Run Current button on the syntax window tool bar looks like an audio play

button Its used to run the command currently selected in the syntax window It is

easy to run an already pasted command either click somewhere on the command

or highlight the entire command (including the full stop) and click the run button

Figure 12 Running the FREQUENCIES command

Click somewhere on the FREQUENCIES command and click the Run

button to execute the command

You will see the procedure being run as you did when you used the menu to run

Frequencies When the processor has nished - it shouldnt take very long - the

results will be stored in the output window as usual

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1654

Module 5 Starting with SPSS Syntax

10

S e s s i o n A U

s i n g S P S S C o m m a n d s Typing in a command

The Syntax window can be used on its own to run SPSS commands if you know

what the commands are (or have generated them from the menus) A new syntax

window can be opened by choosing Fie gt New gt Synta or an existing one can be

opened using Fie gt Open gt Synta A syntax window can be brought to the front by

choosing it from the Windows menu if it is already open

Select the Syntax window from the Window menuWindow gt 3 Snax1 ndash SPSS Snax Edior

The rst syntax window is usually called Synta1 and the 3 before the name

indicates its the third open SPSS window after the data editor and output window If

you have more windows open then the number might be different

The CROSSTABS command

The following CROSSTABS command will cross-tabulate the appy variable by the

ife variable from the social survey dataset

c r o s s t a b s h a p p y b y l i f e

It is a simple form of the CROSSTABS command with no subcommands

Type in the CROSSTABS command crosstabs appy by ife into the

Syntax window

Figure 13 CROSSTABS command selected

A full stop is needed to indicate the end of each SPSS commandIf you forget to nish a command with a full stop the command

may not run properly

Select the whole CROSSTABS command as shown above and click on the

Run button to run it

If you switched on Dispay coands in te og demonstrated on page 2 then

the commands are displayed with the output then you will see the CROSSTABS

command just before the output from Crosstabs

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1754

Module 5 Starting with SPSS Syntax

11

S e s s i onA U s i n g S P S

S C omm an d s

Figure 14 Start of Output from the CROSSTABS command

The CROSSTABS command has several subcommands try the syntax chart button

to see what they are They include

CEllS used to change the content of the table

BARChART to add a clustered bar chart for each layer

Figure 15 CROSSTABS using CEllS and BARChART subcommands

Try typing in and running the new command in Figure 15

Layers can be added to the table by adding another BY keyword follwed by the layer

variable or variable list For example

c r o s s t a b s h a p p y b y l i f e b y r e g i o n

will produce a crosstabs table layer for each region Try adding a layer to the latest

crosstabs command and run the command to check the effect

Some command names such as FINISh BEGIN DATA and

END DATA are complete commands which need no further

specication However most commands (including all of the statistical subprograms) have to have a certain amount

of specication and each has its own particular form of

specication so check the syntax guide before you run

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1854

Module 5 Starting with SPSS Syntax

12

S e s s i o n A U

s i n g S P S S C o m m a n d s Slide Content from presentation

SPSS Coand language

Starting with commands

Wy use Coands

More exible

Extra features

Can be saved

And repeated

Te Synta Window

SPSS commands are entered in the Syntax Window

Create a Syntax Window

Either selecting File gt New gt Syntax from the menu bar

Or creating le with sps extension in a text editor

Use an existing syntax window

Either Selecting an open syntax window from the Window menu

Or selecting File gt Open gt Syntax from the menu bar

SPSS coands

Commands are entered in the Syntax Window by

Either opening the syntax Window and typing directly

Or building up a command using dialog boxes then clicking the

PASTE button

Or using a mixture of the two

Forat of Coands

Control Field - consists of SPSS keyword

Specication Field - names of variables etc on which command willoperate

Full stop - to terminate command

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1954

Module 5 Starting with SPSS Syntax

13

S e s s i onA U s i n g S P S

S C omm an d s

Synta Rues

Commands start with a command keyword

Commands are case insensitive

Except for text in quotes

A single command can go over many syntax lines

Each line of syntax must not exceed 256 bytes3

Do not break a syntax line

in a variable name

in between quotes lsquo lsquo or ldquo ldquo

For a syntax window

Each command must start on a new line

Each command must end with a full stop with no non-blanks after it

or end with a completely blank line afterwards

For batc coands (see ast session)

Each command begins on a new line in column 1

unless you use + or -

Continuation lines mustnrsquot start in column 1

Full stops command terminators are optional

Te FINISh coand

FINISH

For batch commands

Terminates a le of SPSS commands and closes SPSS tidily

NB When SPSS windows manager is running (eg during an SPSS

for Windows session) FINISH will generate an error

Terminate an interactive SPSS session using Exit from the File menu

3 256 bytes are characters in single byte languageshellip

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2054

Module 5 Starting with SPSS Syntax

14

S e s s i o n A U

s i n g S P S S C o m m a n d s

Session summaryIn this session we covered

How to display SPSS commands in output window as they are runbull

How using thebull Paste button in an SPSS dialog box pastes the underlying

command into a synta window

The Syntax Window where SPSS commands (also calledbull synta) are stored

Using the syntax chart button to get help about a command in the syntax windowbull

How to type in and run an SPSS command from thebull synta window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2154

Session B

SPSS CommandStructure

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2254

Module 5 Starting with SPSS Syntax

16

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Commands Subcommands amp keywords

In general an SPSS command will be a mixture of different elements like this -

v kyw kyw kyw

An SPSS command starts with a recognized command keyword which has to

be spelt correctly Each command has its own general format with its own set of

subcommands and keywords These are combined with lists of variables names

or combinations of variable names and keywords to specify the analysis or action

required by the computer

Subcommands and other keywords are words recognized by SPSS as part of

the commands specication and so appear in the specication eld Some

SPSS procedures have subcommands which can appear either on their own or in

association with certain keywords As with commands subcommands and keywords

have to appear exactly as they are spelt

Figure 17 Different keywords in the Commands example

get FILE=mspssworkCARDIACsav

crosstabs chd B Y famhist CELLS=C O U N T R O W C O L U M N STATISTICS=C H I S Q

requencies VARIABLES=height cigaret

FORMAT=L I M I T (12 ) HIST

graPH SCATTERPLOT weight W I T H height

inisH

The different types of keyword have been highlighted as follows-

command keywords SUBCOMMAND keywords

O T H E R keywords variable names and other specication

Abbreviating keywords

SPSS will recognise most of its command and subcommand keywords when

abbreviated to the rst three or four characters of the name as long as the keywordsare unique So for example-

freq vars=height cigaret form=lim(12) hist

can be used as an abbreviation of-

frequencies variables=height cigaret

format=limit(12)

histogram

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2354

Module 5 SPSS command Structure

17

S e s s i onB S P S S C om

m an d S t r u c t ur e

Reserved keywords

With most procedures it does not matter if there is a variable name the same as a

keyword since there are certain places in a command specication where a variable

or variable list is expected and others where keywords are expected However there

are some reserved keywords which cannot be used as names These are

ALL AND BY EQ GE GT LE LT NE NOT OR TO WITH

These words cannot be used as variable names in SPSS because the program

already recognizes them as having meaning For example

FREQUENCIES VARIABLES=ALL

calculates a frequency table for all the variables currently stored on SPSSs

active le To have a variable called A L L would make the sense of the instruction

ambiguous to SPSS

Lists of Variables

The variables are kept in the SPSS active le in the order they are given in the

DATA lIST command and any variables generated during the session in the order

they were created This order can be utilized when instead of typing a long list of

consecutive variables you type the name of the rst variable then the TO keyword

and then the name of the last variable in the list These inclusive ranges of variables

can be combined with the names of other variables to produce a larger variable list

where needed For example if we had the following variables in a system le-

LIST OF VARIABLES ON THE ACTIVE FILE

1 AGE 6 HHLDTYPE 11 OCPENAMT 16 SPSCLASS

2 CHRONILL 7 HOMEHELP 12 REBTOWN 17 TOTINCID

3 CLASS 8 KINNEAR 13 REBTRENT 18 UNEARNED

4 EMPLOYED 9 MARITAL 14 GENDER 19 CASWGT

5 HEALTH 10 NIRAMT 15 SPSAGE

In SPSS variable lists individual variable names andor variable ranges can

be separated by either spaces or commas However any keywords have to be

separated from variable names by spaces ie use spaces with TO or All

AGE TO CASWGT implies

AGE CHRONILL CLASS EMPLOYED HEALTH HHLDTYPEHOMEHELP KINNEAR MARITAL NIRAMT OCPENAMT REBTOWNREBTRENT GENDER SPSAGE SPSCLASS TOTINCID UNEARNEDCASWGT

This rst list is all the variables in the active le since AGE is the rst variable on the

list and CASWGT is the last

hhlDTYPE TO GENDER implies

HHLDTYPE HOMEHELP KINNEAR MARITAL NIRAMT OCPENAMTREBTOWN REBTRENT GENDER

Notice in this list 7 of the 9 variables are implied in the same order as the active le

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2454

Module 5 Starting with SPSS Syntax

18

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e ClASS hEAlTh TO NIRAmT implies

CLASS HEALTH HHLDTYPE HOMEHELP KINNEAR MARITALNIRAMT

A list can be combined with individual variables

AGE SPAGE ClASS SPClASS All implies

AGE SPSAGE CLASS SPSCLASS CHRONILLEMPLOYED HEALTH HHLDTYPE HOMEHELP KINNEARMARITAL NIRAMT OCPENAMT REBTOWN REBTRENT GENDERTOTINCID UNEARNED CASWGT

The reserved keyword ALL in this context means all the variables in the active le

It can be used in a variable list for the KEEP subcommand on a GET or SAVE

command to change the order the variables are saved in whilst making sure no

variables are missed Variables not specically listed will appear after all the listed

ones in active le order

If new variables are created during a session they will be added to the active le in

the order they were created after the variables already stored in the active le You

can always check the variable order in the active le using the DISPlAY command -

it may not be the same order they will appear in dialog boxes

Syntax Charts

The Sow synta cart button as seen in the previous session in Figure 11 will let

you look at command syntax charts If you have no command selected in the syntax

window then the button will give you an alphabetic list of all the command keywords

Look at the syntax chart for each of these commands to see what subcommands and keywords are available

Use the Search button to look for more information about each

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2554

Module 5 SPSS command Structure

19

S e s s i onB S P S S C om

m an d S t r u c t ur e

Some SPSS Command examples (from slides)

Te GET coand

The GET command reads a sav le into SPSS

GET FILE = cardiacsav

Te GET coand - KEEP and DROP subcoands

Use the KEEP subcommand to select only some of the variables

GET FILE=cardiacsavKEEP=famhist chd

Use the DROP subcommand to select all of the le except certain

variables

GET FILE=cardiacsavDROP=height weight

Te DATASET NAmE coand

The DATASET NAME command give the active dataset a short name

which can be used in later commands

GET FILE = mspssworkcardiacsav

DATASET NAME cardiac

Te SAVE coand

Use the SAVE command to save your data in a sav le

SAVE OUTFILE=myfilesav

Te SAVE coand wi

save all your cases together with variable names values

missing values variable and value labels and formats

Saved in the state they are in when the command is issued

Any further changes can only be saved with another SAVE command

It will NOT save the results of any analyses results are saved in an

output window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2654

Module 5 Starting with SPSS Syntax

20

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Te SAVE coand KEEP and DROP subcoands

The KEEP and DROP subcommands are available with the SAVE

command and perform the same function as with GET

Using KEEP will save only the named variables

SAVE OUTFILE=filesav KEEP=AGE HEIGHT

Using DROP will save all variables except named ones (here it means

all variables except WEIGHT2)

SAVE OUTFILE=filesav DROP=WEIGHT2

FREQUENCIES coand

FREQUENCIES VARIABLES=HEIGHT

FREQUENCIES - STATISTICS subcoand

FREQ VAR=HEIGHT AGE STATISTICS

Gives four default statistics

MEAN STDDEV MINIMUM MAXIMUM

FREQUENCIES - more statistics

MEDIAN MODE SUM RANGE SEMEAN VARIANCESKEWNESS SESKEW KURTOSIS SEKURT

STATISTICS=DEFAULT

Use to get the default four statistics MEAN STDDEV MINIMUM

MAXIMUM together with others you ask for

STATISTICS=ALL

Gives all available statistics - can be wasteful

STATISTICS=NONE

Equivalent to omitting STATISTICS altogether

FREQUENCIES - FORmAT subcoand

To suppress the frequency table use

FORMAT=NOTABLE

To suppress tables over a certain length

FORMAT=LIMIT(50)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2754

Module 5 SPSS command Structure

21

S e s s i onB S P S S C om

m an d S t r u c t ur e

FREQUENCIES - subcoands for carts

There are three subcommands to produce type of charts named

HISTOGRAM

BARCHART

PIECHART

Using ore tan one subcoand

To use more than one subcommand string them together and nish

the command with the terminating full stop

FREQUENCIES VARIABLES=AGESEX STATISTICS=RANGEBARCHART

CROSSTABS coand

CROSSTABS TABLES=AGE BY SEX

CROSSTABS AGE BY SEX

SEX BY WEIGHT

The next three crosstabs commands produce the same three tables

CROSSTABS AGE BY SEX

AGE BY PARTY

AGE BY INCOME

CROSSTABS AGE BY SEX PARTY INCOMECROSSTABS AGE BY SEX TO INCOME

CROSSTABS - STATISTICS subcoand

CROSSTABS HEIGHT BY AGE

STATISTICS

Gives the Chi-square statistic tests

CROSSTABS - more statistics

Others available

CHISQ PHI CC LAMBDA UC BTAU CTAU GAMMA DETA CORR ALL NONE

and KAPPA RISK MCNEMAR CMH(1)

Usage

CROSSTABS TABLES=AGE BY WEIGHT

STATISTICS=BTAU CORR

Only gives those statistics asked for

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 9: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 954

Module 5 Starting with SPSS Syntax

3

S e s s i onA U s i n g S P S

S C omm an d s

Figure 2 Options dialog box (Viewer tab showing)

Tick Dispay coands in te og box under Initia Output State heading

to display commands Click OK or Appy

Figure 3 Options detail Dispay coands ticked

Once this feature has been switched on the underlying SPSS commands for

anything you do in SPSS will be pasted in the output window before the output

itself For example in Figure 4 you should see a GET command followed by the

DATASET command in the output window

Open the 1991 US Genera Socia Surveysav data le from the SPSS

directory (using Fie gt Open gt Daa) and look at the output window

Figure 4 Output viewer with GET command

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1054

Module 5 Starting with SPSS Syntax

4

S e s s i o n A U

s i n g S P S S C o m m a n d s The full GET command in the output is

getfile=EProgramFilesSPSS141991USGeneralSocialSurveysav

The GET command reads an SPSS data le into the data editor Its general form is-

G E T F I L E = phfl

Earlier versions of SPSS needed to have an ExECUTE command after the GET command so SPSS would run the GET

command and read the data into the data editor

The other command the DATASET command is used to name the active data

window Its not absolutely necessary but it looks like

D A T A S E T N A M E D a t a s e t 1 W I N D O W = F R O N T

Now we will run a FREQUENCIES procedure

Select Anaze gt Descripive Saisics gt Freqencies and

choose the variables shown in Figure 5

Figure 5 Frequencies dialog box

Click OK to run the Frequencies and look at the output

Figure 6 Frequencies command preceding Frequencies output

The FREQUENCIES command can be seen after the GET and DATASET

commands and before the title at the start of the Frequencies output

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1154

Module 5 Starting with SPSS Syntax

5

S e s s i onA U s i n g S P S

S C omm an d s

The FREQUENCIES command is-

F R E Q U E N C I E S V A R I A B L E S = h a p p y l i f e o r d e r = a n a l y s i s

In its simplest form all you need is a list of the variables-

F r e q u e n c i e s v a r i a b l e s = h a p p y l i f e

The commands in the output window merely document the SPSS session and cant be run from here To run commands we will

need use a syntax window to be discussed soon

Notes in output viewer

The other place you can see a report of the underlying commands is in the Notes

object produced every time you run something a procedure In the Viewer outline

to the left of the output as you look at it you can see a Notes object as part of every

procedures output although by default its contents are hidden Above Figure 6

shows the Notes object as closed or hidden Below in Figure 8 shows the Notes

object open to view

Double click on the Notes icon in the outline to show its contents

Figure 7 Notes from the Frequencies procedure

Notes are quite useful to check details if an SPSS procedure hasnrsquot worked as

you expected it to From the notes you can check to make sure you havent any

weights split le routines or any selection lters switched on You can check that

you have the correct le and syntax and there is a date and time stamp so you can

check when the output was produced

Showing and hiding output objects is discussed in the SPSS 1Getting Started with SPSS course Course Code 1301

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1254

Module 5 Starting with SPSS Syntax

6

S e s s i o n A U

s i n g S P S S C o m m a n d s The syntax window

The syntax window is used to store and run SPSS commands A default installation 1

of SPSS will not open a syntax window Each time you do something with the menus

whether it is to read in data or perform an analysis you have issued a command or

series of commands for SPSS to obey It is possible to paste the SPSS commands

underlying any dialog box into a syntax window using the Paste button - available

on most SPSS dialog boxes It will automatically open up a new syntax2 window

and paste the underlying command(s) from the dialog box into the new window The

contents of a syntax window can be saved to a le so the analysis can be rerun

later without using menus

Figure 8 Frequencies dialog box again

For example if we go back to the Frequencies procedure run earlier we can paste

the underlying FREQUENCIES command into a syntax window

Choose Anaze gt Descripive Saisics gt Freqencies

Click the Carts button and select a bar chart Click Continue to get back

to the Frequencies dialog box

Instead of pressing OK to run Frequencies click on Paste

Figure 9 Frequencies command in the syntax window

SPSS will automatically open up a syntax window and bring it to the front of the

screen with the FREQUENCIES command pasted into it

1 You can change this default using the Edi gt Opionshellip menu under the Gen-

era tab by ticking Open synta window at start-up

2 If a syntax window is already open it will put the command in it without bringing it

to the front

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1354

Module 5 Starting with SPSS Syntax

7

S e s s i onA U s i n g S P S

S C omm an d s

This is the FREQUENCIES command underlying the analysis we ran at the

beginning of the rst exercise You can see the FREQUENCIES command keyword

three subcommand keywords VARIABlES BARChART and ORDER separated

by and two other keywords FREQ and ANAlYSIS And like all commands its

nished with a full stop

To break the command down

Thebull VARIABlES subcommand lists the variables for which we want frequencies

in this example the variables appy and ife

Thebull BARChART subcommand requests a bar chart to be plotted for each of

the variables in the list FREQ species frequencies raw counts rather than

percents on the scale axis

Thebull ORDER subcommand with keyword ANAlYSIS species all the charts after

the tables If the VARIABlE keyword had been used instead the chart for a

particular variable would be placed next to its frequency table

If you want to see all the possible subcommands and other keywords you can use

with any command click the Sow synta cart button on the syntax window

toolbar to look at its syntax chart

Figure 10 The Sow synta cart button

Select the Frequencies command and click on the synta cart button to

see its syntax chart

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1454

Module 5 Starting with SPSS Syntax

8

S e s s i o n A U

s i n g S P S S C o m m a n d s Figure 11 FREQUENCIES syntax chart

As you can see the chart is quite complicated but will give you all the elements you

can put in a FREQUENCIES command You maybe able to spot the subcommands

and keywords just described

Each element in square brackets [ ] indicates a optional element a list of elements

aligned above one another in curly brackets indicates a choice of one out of that

series of elements default choice is usually given in bold All SPSS command sub

command and other keywords are presented in uppercase in chart (although you

dont need to type them in uppercase) Check the syntax reference guide or on-line

help for examples of how each command is used

Once you are nished having a look at the help information close the Helpwindow and you should go back to the Syntax window

The SPSS Command Syntax Reference guide will give you moredetails about SPSS commands and how to run them Syntax

guides are available on the SPSS manual CD or within SPSS in

PDF format from Hep gt Coand Snax Reference

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1554

Module 5 Starting with SPSS Syntax

9

S e s s i onA U s i n g S P S

S C omm an d s

Running an SPSS commandThe Run Current button on the syntax window tool bar looks like an audio play

button Its used to run the command currently selected in the syntax window It is

easy to run an already pasted command either click somewhere on the command

or highlight the entire command (including the full stop) and click the run button

Figure 12 Running the FREQUENCIES command

Click somewhere on the FREQUENCIES command and click the Run

button to execute the command

You will see the procedure being run as you did when you used the menu to run

Frequencies When the processor has nished - it shouldnt take very long - the

results will be stored in the output window as usual

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1654

Module 5 Starting with SPSS Syntax

10

S e s s i o n A U

s i n g S P S S C o m m a n d s Typing in a command

The Syntax window can be used on its own to run SPSS commands if you know

what the commands are (or have generated them from the menus) A new syntax

window can be opened by choosing Fie gt New gt Synta or an existing one can be

opened using Fie gt Open gt Synta A syntax window can be brought to the front by

choosing it from the Windows menu if it is already open

Select the Syntax window from the Window menuWindow gt 3 Snax1 ndash SPSS Snax Edior

The rst syntax window is usually called Synta1 and the 3 before the name

indicates its the third open SPSS window after the data editor and output window If

you have more windows open then the number might be different

The CROSSTABS command

The following CROSSTABS command will cross-tabulate the appy variable by the

ife variable from the social survey dataset

c r o s s t a b s h a p p y b y l i f e

It is a simple form of the CROSSTABS command with no subcommands

Type in the CROSSTABS command crosstabs appy by ife into the

Syntax window

Figure 13 CROSSTABS command selected

A full stop is needed to indicate the end of each SPSS commandIf you forget to nish a command with a full stop the command

may not run properly

Select the whole CROSSTABS command as shown above and click on the

Run button to run it

If you switched on Dispay coands in te og demonstrated on page 2 then

the commands are displayed with the output then you will see the CROSSTABS

command just before the output from Crosstabs

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1754

Module 5 Starting with SPSS Syntax

11

S e s s i onA U s i n g S P S

S C omm an d s

Figure 14 Start of Output from the CROSSTABS command

The CROSSTABS command has several subcommands try the syntax chart button

to see what they are They include

CEllS used to change the content of the table

BARChART to add a clustered bar chart for each layer

Figure 15 CROSSTABS using CEllS and BARChART subcommands

Try typing in and running the new command in Figure 15

Layers can be added to the table by adding another BY keyword follwed by the layer

variable or variable list For example

c r o s s t a b s h a p p y b y l i f e b y r e g i o n

will produce a crosstabs table layer for each region Try adding a layer to the latest

crosstabs command and run the command to check the effect

Some command names such as FINISh BEGIN DATA and

END DATA are complete commands which need no further

specication However most commands (including all of the statistical subprograms) have to have a certain amount

of specication and each has its own particular form of

specication so check the syntax guide before you run

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1854

Module 5 Starting with SPSS Syntax

12

S e s s i o n A U

s i n g S P S S C o m m a n d s Slide Content from presentation

SPSS Coand language

Starting with commands

Wy use Coands

More exible

Extra features

Can be saved

And repeated

Te Synta Window

SPSS commands are entered in the Syntax Window

Create a Syntax Window

Either selecting File gt New gt Syntax from the menu bar

Or creating le with sps extension in a text editor

Use an existing syntax window

Either Selecting an open syntax window from the Window menu

Or selecting File gt Open gt Syntax from the menu bar

SPSS coands

Commands are entered in the Syntax Window by

Either opening the syntax Window and typing directly

Or building up a command using dialog boxes then clicking the

PASTE button

Or using a mixture of the two

Forat of Coands

Control Field - consists of SPSS keyword

Specication Field - names of variables etc on which command willoperate

Full stop - to terminate command

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1954

Module 5 Starting with SPSS Syntax

13

S e s s i onA U s i n g S P S

S C omm an d s

Synta Rues

Commands start with a command keyword

Commands are case insensitive

Except for text in quotes

A single command can go over many syntax lines

Each line of syntax must not exceed 256 bytes3

Do not break a syntax line

in a variable name

in between quotes lsquo lsquo or ldquo ldquo

For a syntax window

Each command must start on a new line

Each command must end with a full stop with no non-blanks after it

or end with a completely blank line afterwards

For batc coands (see ast session)

Each command begins on a new line in column 1

unless you use + or -

Continuation lines mustnrsquot start in column 1

Full stops command terminators are optional

Te FINISh coand

FINISH

For batch commands

Terminates a le of SPSS commands and closes SPSS tidily

NB When SPSS windows manager is running (eg during an SPSS

for Windows session) FINISH will generate an error

Terminate an interactive SPSS session using Exit from the File menu

3 256 bytes are characters in single byte languageshellip

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2054

Module 5 Starting with SPSS Syntax

14

S e s s i o n A U

s i n g S P S S C o m m a n d s

Session summaryIn this session we covered

How to display SPSS commands in output window as they are runbull

How using thebull Paste button in an SPSS dialog box pastes the underlying

command into a synta window

The Syntax Window where SPSS commands (also calledbull synta) are stored

Using the syntax chart button to get help about a command in the syntax windowbull

How to type in and run an SPSS command from thebull synta window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2154

Session B

SPSS CommandStructure

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2254

Module 5 Starting with SPSS Syntax

16

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Commands Subcommands amp keywords

In general an SPSS command will be a mixture of different elements like this -

v kyw kyw kyw

An SPSS command starts with a recognized command keyword which has to

be spelt correctly Each command has its own general format with its own set of

subcommands and keywords These are combined with lists of variables names

or combinations of variable names and keywords to specify the analysis or action

required by the computer

Subcommands and other keywords are words recognized by SPSS as part of

the commands specication and so appear in the specication eld Some

SPSS procedures have subcommands which can appear either on their own or in

association with certain keywords As with commands subcommands and keywords

have to appear exactly as they are spelt

Figure 17 Different keywords in the Commands example

get FILE=mspssworkCARDIACsav

crosstabs chd B Y famhist CELLS=C O U N T R O W C O L U M N STATISTICS=C H I S Q

requencies VARIABLES=height cigaret

FORMAT=L I M I T (12 ) HIST

graPH SCATTERPLOT weight W I T H height

inisH

The different types of keyword have been highlighted as follows-

command keywords SUBCOMMAND keywords

O T H E R keywords variable names and other specication

Abbreviating keywords

SPSS will recognise most of its command and subcommand keywords when

abbreviated to the rst three or four characters of the name as long as the keywordsare unique So for example-

freq vars=height cigaret form=lim(12) hist

can be used as an abbreviation of-

frequencies variables=height cigaret

format=limit(12)

histogram

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2354

Module 5 SPSS command Structure

17

S e s s i onB S P S S C om

m an d S t r u c t ur e

Reserved keywords

With most procedures it does not matter if there is a variable name the same as a

keyword since there are certain places in a command specication where a variable

or variable list is expected and others where keywords are expected However there

are some reserved keywords which cannot be used as names These are

ALL AND BY EQ GE GT LE LT NE NOT OR TO WITH

These words cannot be used as variable names in SPSS because the program

already recognizes them as having meaning For example

FREQUENCIES VARIABLES=ALL

calculates a frequency table for all the variables currently stored on SPSSs

active le To have a variable called A L L would make the sense of the instruction

ambiguous to SPSS

Lists of Variables

The variables are kept in the SPSS active le in the order they are given in the

DATA lIST command and any variables generated during the session in the order

they were created This order can be utilized when instead of typing a long list of

consecutive variables you type the name of the rst variable then the TO keyword

and then the name of the last variable in the list These inclusive ranges of variables

can be combined with the names of other variables to produce a larger variable list

where needed For example if we had the following variables in a system le-

LIST OF VARIABLES ON THE ACTIVE FILE

1 AGE 6 HHLDTYPE 11 OCPENAMT 16 SPSCLASS

2 CHRONILL 7 HOMEHELP 12 REBTOWN 17 TOTINCID

3 CLASS 8 KINNEAR 13 REBTRENT 18 UNEARNED

4 EMPLOYED 9 MARITAL 14 GENDER 19 CASWGT

5 HEALTH 10 NIRAMT 15 SPSAGE

In SPSS variable lists individual variable names andor variable ranges can

be separated by either spaces or commas However any keywords have to be

separated from variable names by spaces ie use spaces with TO or All

AGE TO CASWGT implies

AGE CHRONILL CLASS EMPLOYED HEALTH HHLDTYPEHOMEHELP KINNEAR MARITAL NIRAMT OCPENAMT REBTOWNREBTRENT GENDER SPSAGE SPSCLASS TOTINCID UNEARNEDCASWGT

This rst list is all the variables in the active le since AGE is the rst variable on the

list and CASWGT is the last

hhlDTYPE TO GENDER implies

HHLDTYPE HOMEHELP KINNEAR MARITAL NIRAMT OCPENAMTREBTOWN REBTRENT GENDER

Notice in this list 7 of the 9 variables are implied in the same order as the active le

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2454

Module 5 Starting with SPSS Syntax

18

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e ClASS hEAlTh TO NIRAmT implies

CLASS HEALTH HHLDTYPE HOMEHELP KINNEAR MARITALNIRAMT

A list can be combined with individual variables

AGE SPAGE ClASS SPClASS All implies

AGE SPSAGE CLASS SPSCLASS CHRONILLEMPLOYED HEALTH HHLDTYPE HOMEHELP KINNEARMARITAL NIRAMT OCPENAMT REBTOWN REBTRENT GENDERTOTINCID UNEARNED CASWGT

The reserved keyword ALL in this context means all the variables in the active le

It can be used in a variable list for the KEEP subcommand on a GET or SAVE

command to change the order the variables are saved in whilst making sure no

variables are missed Variables not specically listed will appear after all the listed

ones in active le order

If new variables are created during a session they will be added to the active le in

the order they were created after the variables already stored in the active le You

can always check the variable order in the active le using the DISPlAY command -

it may not be the same order they will appear in dialog boxes

Syntax Charts

The Sow synta cart button as seen in the previous session in Figure 11 will let

you look at command syntax charts If you have no command selected in the syntax

window then the button will give you an alphabetic list of all the command keywords

Look at the syntax chart for each of these commands to see what subcommands and keywords are available

Use the Search button to look for more information about each

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2554

Module 5 SPSS command Structure

19

S e s s i onB S P S S C om

m an d S t r u c t ur e

Some SPSS Command examples (from slides)

Te GET coand

The GET command reads a sav le into SPSS

GET FILE = cardiacsav

Te GET coand - KEEP and DROP subcoands

Use the KEEP subcommand to select only some of the variables

GET FILE=cardiacsavKEEP=famhist chd

Use the DROP subcommand to select all of the le except certain

variables

GET FILE=cardiacsavDROP=height weight

Te DATASET NAmE coand

The DATASET NAME command give the active dataset a short name

which can be used in later commands

GET FILE = mspssworkcardiacsav

DATASET NAME cardiac

Te SAVE coand

Use the SAVE command to save your data in a sav le

SAVE OUTFILE=myfilesav

Te SAVE coand wi

save all your cases together with variable names values

missing values variable and value labels and formats

Saved in the state they are in when the command is issued

Any further changes can only be saved with another SAVE command

It will NOT save the results of any analyses results are saved in an

output window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2654

Module 5 Starting with SPSS Syntax

20

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Te SAVE coand KEEP and DROP subcoands

The KEEP and DROP subcommands are available with the SAVE

command and perform the same function as with GET

Using KEEP will save only the named variables

SAVE OUTFILE=filesav KEEP=AGE HEIGHT

Using DROP will save all variables except named ones (here it means

all variables except WEIGHT2)

SAVE OUTFILE=filesav DROP=WEIGHT2

FREQUENCIES coand

FREQUENCIES VARIABLES=HEIGHT

FREQUENCIES - STATISTICS subcoand

FREQ VAR=HEIGHT AGE STATISTICS

Gives four default statistics

MEAN STDDEV MINIMUM MAXIMUM

FREQUENCIES - more statistics

MEDIAN MODE SUM RANGE SEMEAN VARIANCESKEWNESS SESKEW KURTOSIS SEKURT

STATISTICS=DEFAULT

Use to get the default four statistics MEAN STDDEV MINIMUM

MAXIMUM together with others you ask for

STATISTICS=ALL

Gives all available statistics - can be wasteful

STATISTICS=NONE

Equivalent to omitting STATISTICS altogether

FREQUENCIES - FORmAT subcoand

To suppress the frequency table use

FORMAT=NOTABLE

To suppress tables over a certain length

FORMAT=LIMIT(50)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2754

Module 5 SPSS command Structure

21

S e s s i onB S P S S C om

m an d S t r u c t ur e

FREQUENCIES - subcoands for carts

There are three subcommands to produce type of charts named

HISTOGRAM

BARCHART

PIECHART

Using ore tan one subcoand

To use more than one subcommand string them together and nish

the command with the terminating full stop

FREQUENCIES VARIABLES=AGESEX STATISTICS=RANGEBARCHART

CROSSTABS coand

CROSSTABS TABLES=AGE BY SEX

CROSSTABS AGE BY SEX

SEX BY WEIGHT

The next three crosstabs commands produce the same three tables

CROSSTABS AGE BY SEX

AGE BY PARTY

AGE BY INCOME

CROSSTABS AGE BY SEX PARTY INCOMECROSSTABS AGE BY SEX TO INCOME

CROSSTABS - STATISTICS subcoand

CROSSTABS HEIGHT BY AGE

STATISTICS

Gives the Chi-square statistic tests

CROSSTABS - more statistics

Others available

CHISQ PHI CC LAMBDA UC BTAU CTAU GAMMA DETA CORR ALL NONE

and KAPPA RISK MCNEMAR CMH(1)

Usage

CROSSTABS TABLES=AGE BY WEIGHT

STATISTICS=BTAU CORR

Only gives those statistics asked for

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 10: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1054

Module 5 Starting with SPSS Syntax

4

S e s s i o n A U

s i n g S P S S C o m m a n d s The full GET command in the output is

getfile=EProgramFilesSPSS141991USGeneralSocialSurveysav

The GET command reads an SPSS data le into the data editor Its general form is-

G E T F I L E = phfl

Earlier versions of SPSS needed to have an ExECUTE command after the GET command so SPSS would run the GET

command and read the data into the data editor

The other command the DATASET command is used to name the active data

window Its not absolutely necessary but it looks like

D A T A S E T N A M E D a t a s e t 1 W I N D O W = F R O N T

Now we will run a FREQUENCIES procedure

Select Anaze gt Descripive Saisics gt Freqencies and

choose the variables shown in Figure 5

Figure 5 Frequencies dialog box

Click OK to run the Frequencies and look at the output

Figure 6 Frequencies command preceding Frequencies output

The FREQUENCIES command can be seen after the GET and DATASET

commands and before the title at the start of the Frequencies output

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1154

Module 5 Starting with SPSS Syntax

5

S e s s i onA U s i n g S P S

S C omm an d s

The FREQUENCIES command is-

F R E Q U E N C I E S V A R I A B L E S = h a p p y l i f e o r d e r = a n a l y s i s

In its simplest form all you need is a list of the variables-

F r e q u e n c i e s v a r i a b l e s = h a p p y l i f e

The commands in the output window merely document the SPSS session and cant be run from here To run commands we will

need use a syntax window to be discussed soon

Notes in output viewer

The other place you can see a report of the underlying commands is in the Notes

object produced every time you run something a procedure In the Viewer outline

to the left of the output as you look at it you can see a Notes object as part of every

procedures output although by default its contents are hidden Above Figure 6

shows the Notes object as closed or hidden Below in Figure 8 shows the Notes

object open to view

Double click on the Notes icon in the outline to show its contents

Figure 7 Notes from the Frequencies procedure

Notes are quite useful to check details if an SPSS procedure hasnrsquot worked as

you expected it to From the notes you can check to make sure you havent any

weights split le routines or any selection lters switched on You can check that

you have the correct le and syntax and there is a date and time stamp so you can

check when the output was produced

Showing and hiding output objects is discussed in the SPSS 1Getting Started with SPSS course Course Code 1301

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1254

Module 5 Starting with SPSS Syntax

6

S e s s i o n A U

s i n g S P S S C o m m a n d s The syntax window

The syntax window is used to store and run SPSS commands A default installation 1

of SPSS will not open a syntax window Each time you do something with the menus

whether it is to read in data or perform an analysis you have issued a command or

series of commands for SPSS to obey It is possible to paste the SPSS commands

underlying any dialog box into a syntax window using the Paste button - available

on most SPSS dialog boxes It will automatically open up a new syntax2 window

and paste the underlying command(s) from the dialog box into the new window The

contents of a syntax window can be saved to a le so the analysis can be rerun

later without using menus

Figure 8 Frequencies dialog box again

For example if we go back to the Frequencies procedure run earlier we can paste

the underlying FREQUENCIES command into a syntax window

Choose Anaze gt Descripive Saisics gt Freqencies

Click the Carts button and select a bar chart Click Continue to get back

to the Frequencies dialog box

Instead of pressing OK to run Frequencies click on Paste

Figure 9 Frequencies command in the syntax window

SPSS will automatically open up a syntax window and bring it to the front of the

screen with the FREQUENCIES command pasted into it

1 You can change this default using the Edi gt Opionshellip menu under the Gen-

era tab by ticking Open synta window at start-up

2 If a syntax window is already open it will put the command in it without bringing it

to the front

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1354

Module 5 Starting with SPSS Syntax

7

S e s s i onA U s i n g S P S

S C omm an d s

This is the FREQUENCIES command underlying the analysis we ran at the

beginning of the rst exercise You can see the FREQUENCIES command keyword

three subcommand keywords VARIABlES BARChART and ORDER separated

by and two other keywords FREQ and ANAlYSIS And like all commands its

nished with a full stop

To break the command down

Thebull VARIABlES subcommand lists the variables for which we want frequencies

in this example the variables appy and ife

Thebull BARChART subcommand requests a bar chart to be plotted for each of

the variables in the list FREQ species frequencies raw counts rather than

percents on the scale axis

Thebull ORDER subcommand with keyword ANAlYSIS species all the charts after

the tables If the VARIABlE keyword had been used instead the chart for a

particular variable would be placed next to its frequency table

If you want to see all the possible subcommands and other keywords you can use

with any command click the Sow synta cart button on the syntax window

toolbar to look at its syntax chart

Figure 10 The Sow synta cart button

Select the Frequencies command and click on the synta cart button to

see its syntax chart

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1454

Module 5 Starting with SPSS Syntax

8

S e s s i o n A U

s i n g S P S S C o m m a n d s Figure 11 FREQUENCIES syntax chart

As you can see the chart is quite complicated but will give you all the elements you

can put in a FREQUENCIES command You maybe able to spot the subcommands

and keywords just described

Each element in square brackets [ ] indicates a optional element a list of elements

aligned above one another in curly brackets indicates a choice of one out of that

series of elements default choice is usually given in bold All SPSS command sub

command and other keywords are presented in uppercase in chart (although you

dont need to type them in uppercase) Check the syntax reference guide or on-line

help for examples of how each command is used

Once you are nished having a look at the help information close the Helpwindow and you should go back to the Syntax window

The SPSS Command Syntax Reference guide will give you moredetails about SPSS commands and how to run them Syntax

guides are available on the SPSS manual CD or within SPSS in

PDF format from Hep gt Coand Snax Reference

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1554

Module 5 Starting with SPSS Syntax

9

S e s s i onA U s i n g S P S

S C omm an d s

Running an SPSS commandThe Run Current button on the syntax window tool bar looks like an audio play

button Its used to run the command currently selected in the syntax window It is

easy to run an already pasted command either click somewhere on the command

or highlight the entire command (including the full stop) and click the run button

Figure 12 Running the FREQUENCIES command

Click somewhere on the FREQUENCIES command and click the Run

button to execute the command

You will see the procedure being run as you did when you used the menu to run

Frequencies When the processor has nished - it shouldnt take very long - the

results will be stored in the output window as usual

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1654

Module 5 Starting with SPSS Syntax

10

S e s s i o n A U

s i n g S P S S C o m m a n d s Typing in a command

The Syntax window can be used on its own to run SPSS commands if you know

what the commands are (or have generated them from the menus) A new syntax

window can be opened by choosing Fie gt New gt Synta or an existing one can be

opened using Fie gt Open gt Synta A syntax window can be brought to the front by

choosing it from the Windows menu if it is already open

Select the Syntax window from the Window menuWindow gt 3 Snax1 ndash SPSS Snax Edior

The rst syntax window is usually called Synta1 and the 3 before the name

indicates its the third open SPSS window after the data editor and output window If

you have more windows open then the number might be different

The CROSSTABS command

The following CROSSTABS command will cross-tabulate the appy variable by the

ife variable from the social survey dataset

c r o s s t a b s h a p p y b y l i f e

It is a simple form of the CROSSTABS command with no subcommands

Type in the CROSSTABS command crosstabs appy by ife into the

Syntax window

Figure 13 CROSSTABS command selected

A full stop is needed to indicate the end of each SPSS commandIf you forget to nish a command with a full stop the command

may not run properly

Select the whole CROSSTABS command as shown above and click on the

Run button to run it

If you switched on Dispay coands in te og demonstrated on page 2 then

the commands are displayed with the output then you will see the CROSSTABS

command just before the output from Crosstabs

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1754

Module 5 Starting with SPSS Syntax

11

S e s s i onA U s i n g S P S

S C omm an d s

Figure 14 Start of Output from the CROSSTABS command

The CROSSTABS command has several subcommands try the syntax chart button

to see what they are They include

CEllS used to change the content of the table

BARChART to add a clustered bar chart for each layer

Figure 15 CROSSTABS using CEllS and BARChART subcommands

Try typing in and running the new command in Figure 15

Layers can be added to the table by adding another BY keyword follwed by the layer

variable or variable list For example

c r o s s t a b s h a p p y b y l i f e b y r e g i o n

will produce a crosstabs table layer for each region Try adding a layer to the latest

crosstabs command and run the command to check the effect

Some command names such as FINISh BEGIN DATA and

END DATA are complete commands which need no further

specication However most commands (including all of the statistical subprograms) have to have a certain amount

of specication and each has its own particular form of

specication so check the syntax guide before you run

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1854

Module 5 Starting with SPSS Syntax

12

S e s s i o n A U

s i n g S P S S C o m m a n d s Slide Content from presentation

SPSS Coand language

Starting with commands

Wy use Coands

More exible

Extra features

Can be saved

And repeated

Te Synta Window

SPSS commands are entered in the Syntax Window

Create a Syntax Window

Either selecting File gt New gt Syntax from the menu bar

Or creating le with sps extension in a text editor

Use an existing syntax window

Either Selecting an open syntax window from the Window menu

Or selecting File gt Open gt Syntax from the menu bar

SPSS coands

Commands are entered in the Syntax Window by

Either opening the syntax Window and typing directly

Or building up a command using dialog boxes then clicking the

PASTE button

Or using a mixture of the two

Forat of Coands

Control Field - consists of SPSS keyword

Specication Field - names of variables etc on which command willoperate

Full stop - to terminate command

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1954

Module 5 Starting with SPSS Syntax

13

S e s s i onA U s i n g S P S

S C omm an d s

Synta Rues

Commands start with a command keyword

Commands are case insensitive

Except for text in quotes

A single command can go over many syntax lines

Each line of syntax must not exceed 256 bytes3

Do not break a syntax line

in a variable name

in between quotes lsquo lsquo or ldquo ldquo

For a syntax window

Each command must start on a new line

Each command must end with a full stop with no non-blanks after it

or end with a completely blank line afterwards

For batc coands (see ast session)

Each command begins on a new line in column 1

unless you use + or -

Continuation lines mustnrsquot start in column 1

Full stops command terminators are optional

Te FINISh coand

FINISH

For batch commands

Terminates a le of SPSS commands and closes SPSS tidily

NB When SPSS windows manager is running (eg during an SPSS

for Windows session) FINISH will generate an error

Terminate an interactive SPSS session using Exit from the File menu

3 256 bytes are characters in single byte languageshellip

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2054

Module 5 Starting with SPSS Syntax

14

S e s s i o n A U

s i n g S P S S C o m m a n d s

Session summaryIn this session we covered

How to display SPSS commands in output window as they are runbull

How using thebull Paste button in an SPSS dialog box pastes the underlying

command into a synta window

The Syntax Window where SPSS commands (also calledbull synta) are stored

Using the syntax chart button to get help about a command in the syntax windowbull

How to type in and run an SPSS command from thebull synta window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2154

Session B

SPSS CommandStructure

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2254

Module 5 Starting with SPSS Syntax

16

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Commands Subcommands amp keywords

In general an SPSS command will be a mixture of different elements like this -

v kyw kyw kyw

An SPSS command starts with a recognized command keyword which has to

be spelt correctly Each command has its own general format with its own set of

subcommands and keywords These are combined with lists of variables names

or combinations of variable names and keywords to specify the analysis or action

required by the computer

Subcommands and other keywords are words recognized by SPSS as part of

the commands specication and so appear in the specication eld Some

SPSS procedures have subcommands which can appear either on their own or in

association with certain keywords As with commands subcommands and keywords

have to appear exactly as they are spelt

Figure 17 Different keywords in the Commands example

get FILE=mspssworkCARDIACsav

crosstabs chd B Y famhist CELLS=C O U N T R O W C O L U M N STATISTICS=C H I S Q

requencies VARIABLES=height cigaret

FORMAT=L I M I T (12 ) HIST

graPH SCATTERPLOT weight W I T H height

inisH

The different types of keyword have been highlighted as follows-

command keywords SUBCOMMAND keywords

O T H E R keywords variable names and other specication

Abbreviating keywords

SPSS will recognise most of its command and subcommand keywords when

abbreviated to the rst three or four characters of the name as long as the keywordsare unique So for example-

freq vars=height cigaret form=lim(12) hist

can be used as an abbreviation of-

frequencies variables=height cigaret

format=limit(12)

histogram

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2354

Module 5 SPSS command Structure

17

S e s s i onB S P S S C om

m an d S t r u c t ur e

Reserved keywords

With most procedures it does not matter if there is a variable name the same as a

keyword since there are certain places in a command specication where a variable

or variable list is expected and others where keywords are expected However there

are some reserved keywords which cannot be used as names These are

ALL AND BY EQ GE GT LE LT NE NOT OR TO WITH

These words cannot be used as variable names in SPSS because the program

already recognizes them as having meaning For example

FREQUENCIES VARIABLES=ALL

calculates a frequency table for all the variables currently stored on SPSSs

active le To have a variable called A L L would make the sense of the instruction

ambiguous to SPSS

Lists of Variables

The variables are kept in the SPSS active le in the order they are given in the

DATA lIST command and any variables generated during the session in the order

they were created This order can be utilized when instead of typing a long list of

consecutive variables you type the name of the rst variable then the TO keyword

and then the name of the last variable in the list These inclusive ranges of variables

can be combined with the names of other variables to produce a larger variable list

where needed For example if we had the following variables in a system le-

LIST OF VARIABLES ON THE ACTIVE FILE

1 AGE 6 HHLDTYPE 11 OCPENAMT 16 SPSCLASS

2 CHRONILL 7 HOMEHELP 12 REBTOWN 17 TOTINCID

3 CLASS 8 KINNEAR 13 REBTRENT 18 UNEARNED

4 EMPLOYED 9 MARITAL 14 GENDER 19 CASWGT

5 HEALTH 10 NIRAMT 15 SPSAGE

In SPSS variable lists individual variable names andor variable ranges can

be separated by either spaces or commas However any keywords have to be

separated from variable names by spaces ie use spaces with TO or All

AGE TO CASWGT implies

AGE CHRONILL CLASS EMPLOYED HEALTH HHLDTYPEHOMEHELP KINNEAR MARITAL NIRAMT OCPENAMT REBTOWNREBTRENT GENDER SPSAGE SPSCLASS TOTINCID UNEARNEDCASWGT

This rst list is all the variables in the active le since AGE is the rst variable on the

list and CASWGT is the last

hhlDTYPE TO GENDER implies

HHLDTYPE HOMEHELP KINNEAR MARITAL NIRAMT OCPENAMTREBTOWN REBTRENT GENDER

Notice in this list 7 of the 9 variables are implied in the same order as the active le

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2454

Module 5 Starting with SPSS Syntax

18

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e ClASS hEAlTh TO NIRAmT implies

CLASS HEALTH HHLDTYPE HOMEHELP KINNEAR MARITALNIRAMT

A list can be combined with individual variables

AGE SPAGE ClASS SPClASS All implies

AGE SPSAGE CLASS SPSCLASS CHRONILLEMPLOYED HEALTH HHLDTYPE HOMEHELP KINNEARMARITAL NIRAMT OCPENAMT REBTOWN REBTRENT GENDERTOTINCID UNEARNED CASWGT

The reserved keyword ALL in this context means all the variables in the active le

It can be used in a variable list for the KEEP subcommand on a GET or SAVE

command to change the order the variables are saved in whilst making sure no

variables are missed Variables not specically listed will appear after all the listed

ones in active le order

If new variables are created during a session they will be added to the active le in

the order they were created after the variables already stored in the active le You

can always check the variable order in the active le using the DISPlAY command -

it may not be the same order they will appear in dialog boxes

Syntax Charts

The Sow synta cart button as seen in the previous session in Figure 11 will let

you look at command syntax charts If you have no command selected in the syntax

window then the button will give you an alphabetic list of all the command keywords

Look at the syntax chart for each of these commands to see what subcommands and keywords are available

Use the Search button to look for more information about each

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2554

Module 5 SPSS command Structure

19

S e s s i onB S P S S C om

m an d S t r u c t ur e

Some SPSS Command examples (from slides)

Te GET coand

The GET command reads a sav le into SPSS

GET FILE = cardiacsav

Te GET coand - KEEP and DROP subcoands

Use the KEEP subcommand to select only some of the variables

GET FILE=cardiacsavKEEP=famhist chd

Use the DROP subcommand to select all of the le except certain

variables

GET FILE=cardiacsavDROP=height weight

Te DATASET NAmE coand

The DATASET NAME command give the active dataset a short name

which can be used in later commands

GET FILE = mspssworkcardiacsav

DATASET NAME cardiac

Te SAVE coand

Use the SAVE command to save your data in a sav le

SAVE OUTFILE=myfilesav

Te SAVE coand wi

save all your cases together with variable names values

missing values variable and value labels and formats

Saved in the state they are in when the command is issued

Any further changes can only be saved with another SAVE command

It will NOT save the results of any analyses results are saved in an

output window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2654

Module 5 Starting with SPSS Syntax

20

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Te SAVE coand KEEP and DROP subcoands

The KEEP and DROP subcommands are available with the SAVE

command and perform the same function as with GET

Using KEEP will save only the named variables

SAVE OUTFILE=filesav KEEP=AGE HEIGHT

Using DROP will save all variables except named ones (here it means

all variables except WEIGHT2)

SAVE OUTFILE=filesav DROP=WEIGHT2

FREQUENCIES coand

FREQUENCIES VARIABLES=HEIGHT

FREQUENCIES - STATISTICS subcoand

FREQ VAR=HEIGHT AGE STATISTICS

Gives four default statistics

MEAN STDDEV MINIMUM MAXIMUM

FREQUENCIES - more statistics

MEDIAN MODE SUM RANGE SEMEAN VARIANCESKEWNESS SESKEW KURTOSIS SEKURT

STATISTICS=DEFAULT

Use to get the default four statistics MEAN STDDEV MINIMUM

MAXIMUM together with others you ask for

STATISTICS=ALL

Gives all available statistics - can be wasteful

STATISTICS=NONE

Equivalent to omitting STATISTICS altogether

FREQUENCIES - FORmAT subcoand

To suppress the frequency table use

FORMAT=NOTABLE

To suppress tables over a certain length

FORMAT=LIMIT(50)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2754

Module 5 SPSS command Structure

21

S e s s i onB S P S S C om

m an d S t r u c t ur e

FREQUENCIES - subcoands for carts

There are three subcommands to produce type of charts named

HISTOGRAM

BARCHART

PIECHART

Using ore tan one subcoand

To use more than one subcommand string them together and nish

the command with the terminating full stop

FREQUENCIES VARIABLES=AGESEX STATISTICS=RANGEBARCHART

CROSSTABS coand

CROSSTABS TABLES=AGE BY SEX

CROSSTABS AGE BY SEX

SEX BY WEIGHT

The next three crosstabs commands produce the same three tables

CROSSTABS AGE BY SEX

AGE BY PARTY

AGE BY INCOME

CROSSTABS AGE BY SEX PARTY INCOMECROSSTABS AGE BY SEX TO INCOME

CROSSTABS - STATISTICS subcoand

CROSSTABS HEIGHT BY AGE

STATISTICS

Gives the Chi-square statistic tests

CROSSTABS - more statistics

Others available

CHISQ PHI CC LAMBDA UC BTAU CTAU GAMMA DETA CORR ALL NONE

and KAPPA RISK MCNEMAR CMH(1)

Usage

CROSSTABS TABLES=AGE BY WEIGHT

STATISTICS=BTAU CORR

Only gives those statistics asked for

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 11: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1154

Module 5 Starting with SPSS Syntax

5

S e s s i onA U s i n g S P S

S C omm an d s

The FREQUENCIES command is-

F R E Q U E N C I E S V A R I A B L E S = h a p p y l i f e o r d e r = a n a l y s i s

In its simplest form all you need is a list of the variables-

F r e q u e n c i e s v a r i a b l e s = h a p p y l i f e

The commands in the output window merely document the SPSS session and cant be run from here To run commands we will

need use a syntax window to be discussed soon

Notes in output viewer

The other place you can see a report of the underlying commands is in the Notes

object produced every time you run something a procedure In the Viewer outline

to the left of the output as you look at it you can see a Notes object as part of every

procedures output although by default its contents are hidden Above Figure 6

shows the Notes object as closed or hidden Below in Figure 8 shows the Notes

object open to view

Double click on the Notes icon in the outline to show its contents

Figure 7 Notes from the Frequencies procedure

Notes are quite useful to check details if an SPSS procedure hasnrsquot worked as

you expected it to From the notes you can check to make sure you havent any

weights split le routines or any selection lters switched on You can check that

you have the correct le and syntax and there is a date and time stamp so you can

check when the output was produced

Showing and hiding output objects is discussed in the SPSS 1Getting Started with SPSS course Course Code 1301

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1254

Module 5 Starting with SPSS Syntax

6

S e s s i o n A U

s i n g S P S S C o m m a n d s The syntax window

The syntax window is used to store and run SPSS commands A default installation 1

of SPSS will not open a syntax window Each time you do something with the menus

whether it is to read in data or perform an analysis you have issued a command or

series of commands for SPSS to obey It is possible to paste the SPSS commands

underlying any dialog box into a syntax window using the Paste button - available

on most SPSS dialog boxes It will automatically open up a new syntax2 window

and paste the underlying command(s) from the dialog box into the new window The

contents of a syntax window can be saved to a le so the analysis can be rerun

later without using menus

Figure 8 Frequencies dialog box again

For example if we go back to the Frequencies procedure run earlier we can paste

the underlying FREQUENCIES command into a syntax window

Choose Anaze gt Descripive Saisics gt Freqencies

Click the Carts button and select a bar chart Click Continue to get back

to the Frequencies dialog box

Instead of pressing OK to run Frequencies click on Paste

Figure 9 Frequencies command in the syntax window

SPSS will automatically open up a syntax window and bring it to the front of the

screen with the FREQUENCIES command pasted into it

1 You can change this default using the Edi gt Opionshellip menu under the Gen-

era tab by ticking Open synta window at start-up

2 If a syntax window is already open it will put the command in it without bringing it

to the front

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1354

Module 5 Starting with SPSS Syntax

7

S e s s i onA U s i n g S P S

S C omm an d s

This is the FREQUENCIES command underlying the analysis we ran at the

beginning of the rst exercise You can see the FREQUENCIES command keyword

three subcommand keywords VARIABlES BARChART and ORDER separated

by and two other keywords FREQ and ANAlYSIS And like all commands its

nished with a full stop

To break the command down

Thebull VARIABlES subcommand lists the variables for which we want frequencies

in this example the variables appy and ife

Thebull BARChART subcommand requests a bar chart to be plotted for each of

the variables in the list FREQ species frequencies raw counts rather than

percents on the scale axis

Thebull ORDER subcommand with keyword ANAlYSIS species all the charts after

the tables If the VARIABlE keyword had been used instead the chart for a

particular variable would be placed next to its frequency table

If you want to see all the possible subcommands and other keywords you can use

with any command click the Sow synta cart button on the syntax window

toolbar to look at its syntax chart

Figure 10 The Sow synta cart button

Select the Frequencies command and click on the synta cart button to

see its syntax chart

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1454

Module 5 Starting with SPSS Syntax

8

S e s s i o n A U

s i n g S P S S C o m m a n d s Figure 11 FREQUENCIES syntax chart

As you can see the chart is quite complicated but will give you all the elements you

can put in a FREQUENCIES command You maybe able to spot the subcommands

and keywords just described

Each element in square brackets [ ] indicates a optional element a list of elements

aligned above one another in curly brackets indicates a choice of one out of that

series of elements default choice is usually given in bold All SPSS command sub

command and other keywords are presented in uppercase in chart (although you

dont need to type them in uppercase) Check the syntax reference guide or on-line

help for examples of how each command is used

Once you are nished having a look at the help information close the Helpwindow and you should go back to the Syntax window

The SPSS Command Syntax Reference guide will give you moredetails about SPSS commands and how to run them Syntax

guides are available on the SPSS manual CD or within SPSS in

PDF format from Hep gt Coand Snax Reference

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1554

Module 5 Starting with SPSS Syntax

9

S e s s i onA U s i n g S P S

S C omm an d s

Running an SPSS commandThe Run Current button on the syntax window tool bar looks like an audio play

button Its used to run the command currently selected in the syntax window It is

easy to run an already pasted command either click somewhere on the command

or highlight the entire command (including the full stop) and click the run button

Figure 12 Running the FREQUENCIES command

Click somewhere on the FREQUENCIES command and click the Run

button to execute the command

You will see the procedure being run as you did when you used the menu to run

Frequencies When the processor has nished - it shouldnt take very long - the

results will be stored in the output window as usual

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1654

Module 5 Starting with SPSS Syntax

10

S e s s i o n A U

s i n g S P S S C o m m a n d s Typing in a command

The Syntax window can be used on its own to run SPSS commands if you know

what the commands are (or have generated them from the menus) A new syntax

window can be opened by choosing Fie gt New gt Synta or an existing one can be

opened using Fie gt Open gt Synta A syntax window can be brought to the front by

choosing it from the Windows menu if it is already open

Select the Syntax window from the Window menuWindow gt 3 Snax1 ndash SPSS Snax Edior

The rst syntax window is usually called Synta1 and the 3 before the name

indicates its the third open SPSS window after the data editor and output window If

you have more windows open then the number might be different

The CROSSTABS command

The following CROSSTABS command will cross-tabulate the appy variable by the

ife variable from the social survey dataset

c r o s s t a b s h a p p y b y l i f e

It is a simple form of the CROSSTABS command with no subcommands

Type in the CROSSTABS command crosstabs appy by ife into the

Syntax window

Figure 13 CROSSTABS command selected

A full stop is needed to indicate the end of each SPSS commandIf you forget to nish a command with a full stop the command

may not run properly

Select the whole CROSSTABS command as shown above and click on the

Run button to run it

If you switched on Dispay coands in te og demonstrated on page 2 then

the commands are displayed with the output then you will see the CROSSTABS

command just before the output from Crosstabs

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1754

Module 5 Starting with SPSS Syntax

11

S e s s i onA U s i n g S P S

S C omm an d s

Figure 14 Start of Output from the CROSSTABS command

The CROSSTABS command has several subcommands try the syntax chart button

to see what they are They include

CEllS used to change the content of the table

BARChART to add a clustered bar chart for each layer

Figure 15 CROSSTABS using CEllS and BARChART subcommands

Try typing in and running the new command in Figure 15

Layers can be added to the table by adding another BY keyword follwed by the layer

variable or variable list For example

c r o s s t a b s h a p p y b y l i f e b y r e g i o n

will produce a crosstabs table layer for each region Try adding a layer to the latest

crosstabs command and run the command to check the effect

Some command names such as FINISh BEGIN DATA and

END DATA are complete commands which need no further

specication However most commands (including all of the statistical subprograms) have to have a certain amount

of specication and each has its own particular form of

specication so check the syntax guide before you run

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1854

Module 5 Starting with SPSS Syntax

12

S e s s i o n A U

s i n g S P S S C o m m a n d s Slide Content from presentation

SPSS Coand language

Starting with commands

Wy use Coands

More exible

Extra features

Can be saved

And repeated

Te Synta Window

SPSS commands are entered in the Syntax Window

Create a Syntax Window

Either selecting File gt New gt Syntax from the menu bar

Or creating le with sps extension in a text editor

Use an existing syntax window

Either Selecting an open syntax window from the Window menu

Or selecting File gt Open gt Syntax from the menu bar

SPSS coands

Commands are entered in the Syntax Window by

Either opening the syntax Window and typing directly

Or building up a command using dialog boxes then clicking the

PASTE button

Or using a mixture of the two

Forat of Coands

Control Field - consists of SPSS keyword

Specication Field - names of variables etc on which command willoperate

Full stop - to terminate command

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1954

Module 5 Starting with SPSS Syntax

13

S e s s i onA U s i n g S P S

S C omm an d s

Synta Rues

Commands start with a command keyword

Commands are case insensitive

Except for text in quotes

A single command can go over many syntax lines

Each line of syntax must not exceed 256 bytes3

Do not break a syntax line

in a variable name

in between quotes lsquo lsquo or ldquo ldquo

For a syntax window

Each command must start on a new line

Each command must end with a full stop with no non-blanks after it

or end with a completely blank line afterwards

For batc coands (see ast session)

Each command begins on a new line in column 1

unless you use + or -

Continuation lines mustnrsquot start in column 1

Full stops command terminators are optional

Te FINISh coand

FINISH

For batch commands

Terminates a le of SPSS commands and closes SPSS tidily

NB When SPSS windows manager is running (eg during an SPSS

for Windows session) FINISH will generate an error

Terminate an interactive SPSS session using Exit from the File menu

3 256 bytes are characters in single byte languageshellip

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2054

Module 5 Starting with SPSS Syntax

14

S e s s i o n A U

s i n g S P S S C o m m a n d s

Session summaryIn this session we covered

How to display SPSS commands in output window as they are runbull

How using thebull Paste button in an SPSS dialog box pastes the underlying

command into a synta window

The Syntax Window where SPSS commands (also calledbull synta) are stored

Using the syntax chart button to get help about a command in the syntax windowbull

How to type in and run an SPSS command from thebull synta window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2154

Session B

SPSS CommandStructure

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2254

Module 5 Starting with SPSS Syntax

16

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Commands Subcommands amp keywords

In general an SPSS command will be a mixture of different elements like this -

v kyw kyw kyw

An SPSS command starts with a recognized command keyword which has to

be spelt correctly Each command has its own general format with its own set of

subcommands and keywords These are combined with lists of variables names

or combinations of variable names and keywords to specify the analysis or action

required by the computer

Subcommands and other keywords are words recognized by SPSS as part of

the commands specication and so appear in the specication eld Some

SPSS procedures have subcommands which can appear either on their own or in

association with certain keywords As with commands subcommands and keywords

have to appear exactly as they are spelt

Figure 17 Different keywords in the Commands example

get FILE=mspssworkCARDIACsav

crosstabs chd B Y famhist CELLS=C O U N T R O W C O L U M N STATISTICS=C H I S Q

requencies VARIABLES=height cigaret

FORMAT=L I M I T (12 ) HIST

graPH SCATTERPLOT weight W I T H height

inisH

The different types of keyword have been highlighted as follows-

command keywords SUBCOMMAND keywords

O T H E R keywords variable names and other specication

Abbreviating keywords

SPSS will recognise most of its command and subcommand keywords when

abbreviated to the rst three or four characters of the name as long as the keywordsare unique So for example-

freq vars=height cigaret form=lim(12) hist

can be used as an abbreviation of-

frequencies variables=height cigaret

format=limit(12)

histogram

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2354

Module 5 SPSS command Structure

17

S e s s i onB S P S S C om

m an d S t r u c t ur e

Reserved keywords

With most procedures it does not matter if there is a variable name the same as a

keyword since there are certain places in a command specication where a variable

or variable list is expected and others where keywords are expected However there

are some reserved keywords which cannot be used as names These are

ALL AND BY EQ GE GT LE LT NE NOT OR TO WITH

These words cannot be used as variable names in SPSS because the program

already recognizes them as having meaning For example

FREQUENCIES VARIABLES=ALL

calculates a frequency table for all the variables currently stored on SPSSs

active le To have a variable called A L L would make the sense of the instruction

ambiguous to SPSS

Lists of Variables

The variables are kept in the SPSS active le in the order they are given in the

DATA lIST command and any variables generated during the session in the order

they were created This order can be utilized when instead of typing a long list of

consecutive variables you type the name of the rst variable then the TO keyword

and then the name of the last variable in the list These inclusive ranges of variables

can be combined with the names of other variables to produce a larger variable list

where needed For example if we had the following variables in a system le-

LIST OF VARIABLES ON THE ACTIVE FILE

1 AGE 6 HHLDTYPE 11 OCPENAMT 16 SPSCLASS

2 CHRONILL 7 HOMEHELP 12 REBTOWN 17 TOTINCID

3 CLASS 8 KINNEAR 13 REBTRENT 18 UNEARNED

4 EMPLOYED 9 MARITAL 14 GENDER 19 CASWGT

5 HEALTH 10 NIRAMT 15 SPSAGE

In SPSS variable lists individual variable names andor variable ranges can

be separated by either spaces or commas However any keywords have to be

separated from variable names by spaces ie use spaces with TO or All

AGE TO CASWGT implies

AGE CHRONILL CLASS EMPLOYED HEALTH HHLDTYPEHOMEHELP KINNEAR MARITAL NIRAMT OCPENAMT REBTOWNREBTRENT GENDER SPSAGE SPSCLASS TOTINCID UNEARNEDCASWGT

This rst list is all the variables in the active le since AGE is the rst variable on the

list and CASWGT is the last

hhlDTYPE TO GENDER implies

HHLDTYPE HOMEHELP KINNEAR MARITAL NIRAMT OCPENAMTREBTOWN REBTRENT GENDER

Notice in this list 7 of the 9 variables are implied in the same order as the active le

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2454

Module 5 Starting with SPSS Syntax

18

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e ClASS hEAlTh TO NIRAmT implies

CLASS HEALTH HHLDTYPE HOMEHELP KINNEAR MARITALNIRAMT

A list can be combined with individual variables

AGE SPAGE ClASS SPClASS All implies

AGE SPSAGE CLASS SPSCLASS CHRONILLEMPLOYED HEALTH HHLDTYPE HOMEHELP KINNEARMARITAL NIRAMT OCPENAMT REBTOWN REBTRENT GENDERTOTINCID UNEARNED CASWGT

The reserved keyword ALL in this context means all the variables in the active le

It can be used in a variable list for the KEEP subcommand on a GET or SAVE

command to change the order the variables are saved in whilst making sure no

variables are missed Variables not specically listed will appear after all the listed

ones in active le order

If new variables are created during a session they will be added to the active le in

the order they were created after the variables already stored in the active le You

can always check the variable order in the active le using the DISPlAY command -

it may not be the same order they will appear in dialog boxes

Syntax Charts

The Sow synta cart button as seen in the previous session in Figure 11 will let

you look at command syntax charts If you have no command selected in the syntax

window then the button will give you an alphabetic list of all the command keywords

Look at the syntax chart for each of these commands to see what subcommands and keywords are available

Use the Search button to look for more information about each

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2554

Module 5 SPSS command Structure

19

S e s s i onB S P S S C om

m an d S t r u c t ur e

Some SPSS Command examples (from slides)

Te GET coand

The GET command reads a sav le into SPSS

GET FILE = cardiacsav

Te GET coand - KEEP and DROP subcoands

Use the KEEP subcommand to select only some of the variables

GET FILE=cardiacsavKEEP=famhist chd

Use the DROP subcommand to select all of the le except certain

variables

GET FILE=cardiacsavDROP=height weight

Te DATASET NAmE coand

The DATASET NAME command give the active dataset a short name

which can be used in later commands

GET FILE = mspssworkcardiacsav

DATASET NAME cardiac

Te SAVE coand

Use the SAVE command to save your data in a sav le

SAVE OUTFILE=myfilesav

Te SAVE coand wi

save all your cases together with variable names values

missing values variable and value labels and formats

Saved in the state they are in when the command is issued

Any further changes can only be saved with another SAVE command

It will NOT save the results of any analyses results are saved in an

output window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2654

Module 5 Starting with SPSS Syntax

20

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Te SAVE coand KEEP and DROP subcoands

The KEEP and DROP subcommands are available with the SAVE

command and perform the same function as with GET

Using KEEP will save only the named variables

SAVE OUTFILE=filesav KEEP=AGE HEIGHT

Using DROP will save all variables except named ones (here it means

all variables except WEIGHT2)

SAVE OUTFILE=filesav DROP=WEIGHT2

FREQUENCIES coand

FREQUENCIES VARIABLES=HEIGHT

FREQUENCIES - STATISTICS subcoand

FREQ VAR=HEIGHT AGE STATISTICS

Gives four default statistics

MEAN STDDEV MINIMUM MAXIMUM

FREQUENCIES - more statistics

MEDIAN MODE SUM RANGE SEMEAN VARIANCESKEWNESS SESKEW KURTOSIS SEKURT

STATISTICS=DEFAULT

Use to get the default four statistics MEAN STDDEV MINIMUM

MAXIMUM together with others you ask for

STATISTICS=ALL

Gives all available statistics - can be wasteful

STATISTICS=NONE

Equivalent to omitting STATISTICS altogether

FREQUENCIES - FORmAT subcoand

To suppress the frequency table use

FORMAT=NOTABLE

To suppress tables over a certain length

FORMAT=LIMIT(50)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2754

Module 5 SPSS command Structure

21

S e s s i onB S P S S C om

m an d S t r u c t ur e

FREQUENCIES - subcoands for carts

There are three subcommands to produce type of charts named

HISTOGRAM

BARCHART

PIECHART

Using ore tan one subcoand

To use more than one subcommand string them together and nish

the command with the terminating full stop

FREQUENCIES VARIABLES=AGESEX STATISTICS=RANGEBARCHART

CROSSTABS coand

CROSSTABS TABLES=AGE BY SEX

CROSSTABS AGE BY SEX

SEX BY WEIGHT

The next three crosstabs commands produce the same three tables

CROSSTABS AGE BY SEX

AGE BY PARTY

AGE BY INCOME

CROSSTABS AGE BY SEX PARTY INCOMECROSSTABS AGE BY SEX TO INCOME

CROSSTABS - STATISTICS subcoand

CROSSTABS HEIGHT BY AGE

STATISTICS

Gives the Chi-square statistic tests

CROSSTABS - more statistics

Others available

CHISQ PHI CC LAMBDA UC BTAU CTAU GAMMA DETA CORR ALL NONE

and KAPPA RISK MCNEMAR CMH(1)

Usage

CROSSTABS TABLES=AGE BY WEIGHT

STATISTICS=BTAU CORR

Only gives those statistics asked for

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 12: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1254

Module 5 Starting with SPSS Syntax

6

S e s s i o n A U

s i n g S P S S C o m m a n d s The syntax window

The syntax window is used to store and run SPSS commands A default installation 1

of SPSS will not open a syntax window Each time you do something with the menus

whether it is to read in data or perform an analysis you have issued a command or

series of commands for SPSS to obey It is possible to paste the SPSS commands

underlying any dialog box into a syntax window using the Paste button - available

on most SPSS dialog boxes It will automatically open up a new syntax2 window

and paste the underlying command(s) from the dialog box into the new window The

contents of a syntax window can be saved to a le so the analysis can be rerun

later without using menus

Figure 8 Frequencies dialog box again

For example if we go back to the Frequencies procedure run earlier we can paste

the underlying FREQUENCIES command into a syntax window

Choose Anaze gt Descripive Saisics gt Freqencies

Click the Carts button and select a bar chart Click Continue to get back

to the Frequencies dialog box

Instead of pressing OK to run Frequencies click on Paste

Figure 9 Frequencies command in the syntax window

SPSS will automatically open up a syntax window and bring it to the front of the

screen with the FREQUENCIES command pasted into it

1 You can change this default using the Edi gt Opionshellip menu under the Gen-

era tab by ticking Open synta window at start-up

2 If a syntax window is already open it will put the command in it without bringing it

to the front

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1354

Module 5 Starting with SPSS Syntax

7

S e s s i onA U s i n g S P S

S C omm an d s

This is the FREQUENCIES command underlying the analysis we ran at the

beginning of the rst exercise You can see the FREQUENCIES command keyword

three subcommand keywords VARIABlES BARChART and ORDER separated

by and two other keywords FREQ and ANAlYSIS And like all commands its

nished with a full stop

To break the command down

Thebull VARIABlES subcommand lists the variables for which we want frequencies

in this example the variables appy and ife

Thebull BARChART subcommand requests a bar chart to be plotted for each of

the variables in the list FREQ species frequencies raw counts rather than

percents on the scale axis

Thebull ORDER subcommand with keyword ANAlYSIS species all the charts after

the tables If the VARIABlE keyword had been used instead the chart for a

particular variable would be placed next to its frequency table

If you want to see all the possible subcommands and other keywords you can use

with any command click the Sow synta cart button on the syntax window

toolbar to look at its syntax chart

Figure 10 The Sow synta cart button

Select the Frequencies command and click on the synta cart button to

see its syntax chart

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1454

Module 5 Starting with SPSS Syntax

8

S e s s i o n A U

s i n g S P S S C o m m a n d s Figure 11 FREQUENCIES syntax chart

As you can see the chart is quite complicated but will give you all the elements you

can put in a FREQUENCIES command You maybe able to spot the subcommands

and keywords just described

Each element in square brackets [ ] indicates a optional element a list of elements

aligned above one another in curly brackets indicates a choice of one out of that

series of elements default choice is usually given in bold All SPSS command sub

command and other keywords are presented in uppercase in chart (although you

dont need to type them in uppercase) Check the syntax reference guide or on-line

help for examples of how each command is used

Once you are nished having a look at the help information close the Helpwindow and you should go back to the Syntax window

The SPSS Command Syntax Reference guide will give you moredetails about SPSS commands and how to run them Syntax

guides are available on the SPSS manual CD or within SPSS in

PDF format from Hep gt Coand Snax Reference

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1554

Module 5 Starting with SPSS Syntax

9

S e s s i onA U s i n g S P S

S C omm an d s

Running an SPSS commandThe Run Current button on the syntax window tool bar looks like an audio play

button Its used to run the command currently selected in the syntax window It is

easy to run an already pasted command either click somewhere on the command

or highlight the entire command (including the full stop) and click the run button

Figure 12 Running the FREQUENCIES command

Click somewhere on the FREQUENCIES command and click the Run

button to execute the command

You will see the procedure being run as you did when you used the menu to run

Frequencies When the processor has nished - it shouldnt take very long - the

results will be stored in the output window as usual

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1654

Module 5 Starting with SPSS Syntax

10

S e s s i o n A U

s i n g S P S S C o m m a n d s Typing in a command

The Syntax window can be used on its own to run SPSS commands if you know

what the commands are (or have generated them from the menus) A new syntax

window can be opened by choosing Fie gt New gt Synta or an existing one can be

opened using Fie gt Open gt Synta A syntax window can be brought to the front by

choosing it from the Windows menu if it is already open

Select the Syntax window from the Window menuWindow gt 3 Snax1 ndash SPSS Snax Edior

The rst syntax window is usually called Synta1 and the 3 before the name

indicates its the third open SPSS window after the data editor and output window If

you have more windows open then the number might be different

The CROSSTABS command

The following CROSSTABS command will cross-tabulate the appy variable by the

ife variable from the social survey dataset

c r o s s t a b s h a p p y b y l i f e

It is a simple form of the CROSSTABS command with no subcommands

Type in the CROSSTABS command crosstabs appy by ife into the

Syntax window

Figure 13 CROSSTABS command selected

A full stop is needed to indicate the end of each SPSS commandIf you forget to nish a command with a full stop the command

may not run properly

Select the whole CROSSTABS command as shown above and click on the

Run button to run it

If you switched on Dispay coands in te og demonstrated on page 2 then

the commands are displayed with the output then you will see the CROSSTABS

command just before the output from Crosstabs

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1754

Module 5 Starting with SPSS Syntax

11

S e s s i onA U s i n g S P S

S C omm an d s

Figure 14 Start of Output from the CROSSTABS command

The CROSSTABS command has several subcommands try the syntax chart button

to see what they are They include

CEllS used to change the content of the table

BARChART to add a clustered bar chart for each layer

Figure 15 CROSSTABS using CEllS and BARChART subcommands

Try typing in and running the new command in Figure 15

Layers can be added to the table by adding another BY keyword follwed by the layer

variable or variable list For example

c r o s s t a b s h a p p y b y l i f e b y r e g i o n

will produce a crosstabs table layer for each region Try adding a layer to the latest

crosstabs command and run the command to check the effect

Some command names such as FINISh BEGIN DATA and

END DATA are complete commands which need no further

specication However most commands (including all of the statistical subprograms) have to have a certain amount

of specication and each has its own particular form of

specication so check the syntax guide before you run

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1854

Module 5 Starting with SPSS Syntax

12

S e s s i o n A U

s i n g S P S S C o m m a n d s Slide Content from presentation

SPSS Coand language

Starting with commands

Wy use Coands

More exible

Extra features

Can be saved

And repeated

Te Synta Window

SPSS commands are entered in the Syntax Window

Create a Syntax Window

Either selecting File gt New gt Syntax from the menu bar

Or creating le with sps extension in a text editor

Use an existing syntax window

Either Selecting an open syntax window from the Window menu

Or selecting File gt Open gt Syntax from the menu bar

SPSS coands

Commands are entered in the Syntax Window by

Either opening the syntax Window and typing directly

Or building up a command using dialog boxes then clicking the

PASTE button

Or using a mixture of the two

Forat of Coands

Control Field - consists of SPSS keyword

Specication Field - names of variables etc on which command willoperate

Full stop - to terminate command

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1954

Module 5 Starting with SPSS Syntax

13

S e s s i onA U s i n g S P S

S C omm an d s

Synta Rues

Commands start with a command keyword

Commands are case insensitive

Except for text in quotes

A single command can go over many syntax lines

Each line of syntax must not exceed 256 bytes3

Do not break a syntax line

in a variable name

in between quotes lsquo lsquo or ldquo ldquo

For a syntax window

Each command must start on a new line

Each command must end with a full stop with no non-blanks after it

or end with a completely blank line afterwards

For batc coands (see ast session)

Each command begins on a new line in column 1

unless you use + or -

Continuation lines mustnrsquot start in column 1

Full stops command terminators are optional

Te FINISh coand

FINISH

For batch commands

Terminates a le of SPSS commands and closes SPSS tidily

NB When SPSS windows manager is running (eg during an SPSS

for Windows session) FINISH will generate an error

Terminate an interactive SPSS session using Exit from the File menu

3 256 bytes are characters in single byte languageshellip

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2054

Module 5 Starting with SPSS Syntax

14

S e s s i o n A U

s i n g S P S S C o m m a n d s

Session summaryIn this session we covered

How to display SPSS commands in output window as they are runbull

How using thebull Paste button in an SPSS dialog box pastes the underlying

command into a synta window

The Syntax Window where SPSS commands (also calledbull synta) are stored

Using the syntax chart button to get help about a command in the syntax windowbull

How to type in and run an SPSS command from thebull synta window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2154

Session B

SPSS CommandStructure

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2254

Module 5 Starting with SPSS Syntax

16

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Commands Subcommands amp keywords

In general an SPSS command will be a mixture of different elements like this -

v kyw kyw kyw

An SPSS command starts with a recognized command keyword which has to

be spelt correctly Each command has its own general format with its own set of

subcommands and keywords These are combined with lists of variables names

or combinations of variable names and keywords to specify the analysis or action

required by the computer

Subcommands and other keywords are words recognized by SPSS as part of

the commands specication and so appear in the specication eld Some

SPSS procedures have subcommands which can appear either on their own or in

association with certain keywords As with commands subcommands and keywords

have to appear exactly as they are spelt

Figure 17 Different keywords in the Commands example

get FILE=mspssworkCARDIACsav

crosstabs chd B Y famhist CELLS=C O U N T R O W C O L U M N STATISTICS=C H I S Q

requencies VARIABLES=height cigaret

FORMAT=L I M I T (12 ) HIST

graPH SCATTERPLOT weight W I T H height

inisH

The different types of keyword have been highlighted as follows-

command keywords SUBCOMMAND keywords

O T H E R keywords variable names and other specication

Abbreviating keywords

SPSS will recognise most of its command and subcommand keywords when

abbreviated to the rst three or four characters of the name as long as the keywordsare unique So for example-

freq vars=height cigaret form=lim(12) hist

can be used as an abbreviation of-

frequencies variables=height cigaret

format=limit(12)

histogram

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2354

Module 5 SPSS command Structure

17

S e s s i onB S P S S C om

m an d S t r u c t ur e

Reserved keywords

With most procedures it does not matter if there is a variable name the same as a

keyword since there are certain places in a command specication where a variable

or variable list is expected and others where keywords are expected However there

are some reserved keywords which cannot be used as names These are

ALL AND BY EQ GE GT LE LT NE NOT OR TO WITH

These words cannot be used as variable names in SPSS because the program

already recognizes them as having meaning For example

FREQUENCIES VARIABLES=ALL

calculates a frequency table for all the variables currently stored on SPSSs

active le To have a variable called A L L would make the sense of the instruction

ambiguous to SPSS

Lists of Variables

The variables are kept in the SPSS active le in the order they are given in the

DATA lIST command and any variables generated during the session in the order

they were created This order can be utilized when instead of typing a long list of

consecutive variables you type the name of the rst variable then the TO keyword

and then the name of the last variable in the list These inclusive ranges of variables

can be combined with the names of other variables to produce a larger variable list

where needed For example if we had the following variables in a system le-

LIST OF VARIABLES ON THE ACTIVE FILE

1 AGE 6 HHLDTYPE 11 OCPENAMT 16 SPSCLASS

2 CHRONILL 7 HOMEHELP 12 REBTOWN 17 TOTINCID

3 CLASS 8 KINNEAR 13 REBTRENT 18 UNEARNED

4 EMPLOYED 9 MARITAL 14 GENDER 19 CASWGT

5 HEALTH 10 NIRAMT 15 SPSAGE

In SPSS variable lists individual variable names andor variable ranges can

be separated by either spaces or commas However any keywords have to be

separated from variable names by spaces ie use spaces with TO or All

AGE TO CASWGT implies

AGE CHRONILL CLASS EMPLOYED HEALTH HHLDTYPEHOMEHELP KINNEAR MARITAL NIRAMT OCPENAMT REBTOWNREBTRENT GENDER SPSAGE SPSCLASS TOTINCID UNEARNEDCASWGT

This rst list is all the variables in the active le since AGE is the rst variable on the

list and CASWGT is the last

hhlDTYPE TO GENDER implies

HHLDTYPE HOMEHELP KINNEAR MARITAL NIRAMT OCPENAMTREBTOWN REBTRENT GENDER

Notice in this list 7 of the 9 variables are implied in the same order as the active le

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2454

Module 5 Starting with SPSS Syntax

18

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e ClASS hEAlTh TO NIRAmT implies

CLASS HEALTH HHLDTYPE HOMEHELP KINNEAR MARITALNIRAMT

A list can be combined with individual variables

AGE SPAGE ClASS SPClASS All implies

AGE SPSAGE CLASS SPSCLASS CHRONILLEMPLOYED HEALTH HHLDTYPE HOMEHELP KINNEARMARITAL NIRAMT OCPENAMT REBTOWN REBTRENT GENDERTOTINCID UNEARNED CASWGT

The reserved keyword ALL in this context means all the variables in the active le

It can be used in a variable list for the KEEP subcommand on a GET or SAVE

command to change the order the variables are saved in whilst making sure no

variables are missed Variables not specically listed will appear after all the listed

ones in active le order

If new variables are created during a session they will be added to the active le in

the order they were created after the variables already stored in the active le You

can always check the variable order in the active le using the DISPlAY command -

it may not be the same order they will appear in dialog boxes

Syntax Charts

The Sow synta cart button as seen in the previous session in Figure 11 will let

you look at command syntax charts If you have no command selected in the syntax

window then the button will give you an alphabetic list of all the command keywords

Look at the syntax chart for each of these commands to see what subcommands and keywords are available

Use the Search button to look for more information about each

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2554

Module 5 SPSS command Structure

19

S e s s i onB S P S S C om

m an d S t r u c t ur e

Some SPSS Command examples (from slides)

Te GET coand

The GET command reads a sav le into SPSS

GET FILE = cardiacsav

Te GET coand - KEEP and DROP subcoands

Use the KEEP subcommand to select only some of the variables

GET FILE=cardiacsavKEEP=famhist chd

Use the DROP subcommand to select all of the le except certain

variables

GET FILE=cardiacsavDROP=height weight

Te DATASET NAmE coand

The DATASET NAME command give the active dataset a short name

which can be used in later commands

GET FILE = mspssworkcardiacsav

DATASET NAME cardiac

Te SAVE coand

Use the SAVE command to save your data in a sav le

SAVE OUTFILE=myfilesav

Te SAVE coand wi

save all your cases together with variable names values

missing values variable and value labels and formats

Saved in the state they are in when the command is issued

Any further changes can only be saved with another SAVE command

It will NOT save the results of any analyses results are saved in an

output window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2654

Module 5 Starting with SPSS Syntax

20

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Te SAVE coand KEEP and DROP subcoands

The KEEP and DROP subcommands are available with the SAVE

command and perform the same function as with GET

Using KEEP will save only the named variables

SAVE OUTFILE=filesav KEEP=AGE HEIGHT

Using DROP will save all variables except named ones (here it means

all variables except WEIGHT2)

SAVE OUTFILE=filesav DROP=WEIGHT2

FREQUENCIES coand

FREQUENCIES VARIABLES=HEIGHT

FREQUENCIES - STATISTICS subcoand

FREQ VAR=HEIGHT AGE STATISTICS

Gives four default statistics

MEAN STDDEV MINIMUM MAXIMUM

FREQUENCIES - more statistics

MEDIAN MODE SUM RANGE SEMEAN VARIANCESKEWNESS SESKEW KURTOSIS SEKURT

STATISTICS=DEFAULT

Use to get the default four statistics MEAN STDDEV MINIMUM

MAXIMUM together with others you ask for

STATISTICS=ALL

Gives all available statistics - can be wasteful

STATISTICS=NONE

Equivalent to omitting STATISTICS altogether

FREQUENCIES - FORmAT subcoand

To suppress the frequency table use

FORMAT=NOTABLE

To suppress tables over a certain length

FORMAT=LIMIT(50)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2754

Module 5 SPSS command Structure

21

S e s s i onB S P S S C om

m an d S t r u c t ur e

FREQUENCIES - subcoands for carts

There are three subcommands to produce type of charts named

HISTOGRAM

BARCHART

PIECHART

Using ore tan one subcoand

To use more than one subcommand string them together and nish

the command with the terminating full stop

FREQUENCIES VARIABLES=AGESEX STATISTICS=RANGEBARCHART

CROSSTABS coand

CROSSTABS TABLES=AGE BY SEX

CROSSTABS AGE BY SEX

SEX BY WEIGHT

The next three crosstabs commands produce the same three tables

CROSSTABS AGE BY SEX

AGE BY PARTY

AGE BY INCOME

CROSSTABS AGE BY SEX PARTY INCOMECROSSTABS AGE BY SEX TO INCOME

CROSSTABS - STATISTICS subcoand

CROSSTABS HEIGHT BY AGE

STATISTICS

Gives the Chi-square statistic tests

CROSSTABS - more statistics

Others available

CHISQ PHI CC LAMBDA UC BTAU CTAU GAMMA DETA CORR ALL NONE

and KAPPA RISK MCNEMAR CMH(1)

Usage

CROSSTABS TABLES=AGE BY WEIGHT

STATISTICS=BTAU CORR

Only gives those statistics asked for

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 13: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1354

Module 5 Starting with SPSS Syntax

7

S e s s i onA U s i n g S P S

S C omm an d s

This is the FREQUENCIES command underlying the analysis we ran at the

beginning of the rst exercise You can see the FREQUENCIES command keyword

three subcommand keywords VARIABlES BARChART and ORDER separated

by and two other keywords FREQ and ANAlYSIS And like all commands its

nished with a full stop

To break the command down

Thebull VARIABlES subcommand lists the variables for which we want frequencies

in this example the variables appy and ife

Thebull BARChART subcommand requests a bar chart to be plotted for each of

the variables in the list FREQ species frequencies raw counts rather than

percents on the scale axis

Thebull ORDER subcommand with keyword ANAlYSIS species all the charts after

the tables If the VARIABlE keyword had been used instead the chart for a

particular variable would be placed next to its frequency table

If you want to see all the possible subcommands and other keywords you can use

with any command click the Sow synta cart button on the syntax window

toolbar to look at its syntax chart

Figure 10 The Sow synta cart button

Select the Frequencies command and click on the synta cart button to

see its syntax chart

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1454

Module 5 Starting with SPSS Syntax

8

S e s s i o n A U

s i n g S P S S C o m m a n d s Figure 11 FREQUENCIES syntax chart

As you can see the chart is quite complicated but will give you all the elements you

can put in a FREQUENCIES command You maybe able to spot the subcommands

and keywords just described

Each element in square brackets [ ] indicates a optional element a list of elements

aligned above one another in curly brackets indicates a choice of one out of that

series of elements default choice is usually given in bold All SPSS command sub

command and other keywords are presented in uppercase in chart (although you

dont need to type them in uppercase) Check the syntax reference guide or on-line

help for examples of how each command is used

Once you are nished having a look at the help information close the Helpwindow and you should go back to the Syntax window

The SPSS Command Syntax Reference guide will give you moredetails about SPSS commands and how to run them Syntax

guides are available on the SPSS manual CD or within SPSS in

PDF format from Hep gt Coand Snax Reference

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1554

Module 5 Starting with SPSS Syntax

9

S e s s i onA U s i n g S P S

S C omm an d s

Running an SPSS commandThe Run Current button on the syntax window tool bar looks like an audio play

button Its used to run the command currently selected in the syntax window It is

easy to run an already pasted command either click somewhere on the command

or highlight the entire command (including the full stop) and click the run button

Figure 12 Running the FREQUENCIES command

Click somewhere on the FREQUENCIES command and click the Run

button to execute the command

You will see the procedure being run as you did when you used the menu to run

Frequencies When the processor has nished - it shouldnt take very long - the

results will be stored in the output window as usual

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1654

Module 5 Starting with SPSS Syntax

10

S e s s i o n A U

s i n g S P S S C o m m a n d s Typing in a command

The Syntax window can be used on its own to run SPSS commands if you know

what the commands are (or have generated them from the menus) A new syntax

window can be opened by choosing Fie gt New gt Synta or an existing one can be

opened using Fie gt Open gt Synta A syntax window can be brought to the front by

choosing it from the Windows menu if it is already open

Select the Syntax window from the Window menuWindow gt 3 Snax1 ndash SPSS Snax Edior

The rst syntax window is usually called Synta1 and the 3 before the name

indicates its the third open SPSS window after the data editor and output window If

you have more windows open then the number might be different

The CROSSTABS command

The following CROSSTABS command will cross-tabulate the appy variable by the

ife variable from the social survey dataset

c r o s s t a b s h a p p y b y l i f e

It is a simple form of the CROSSTABS command with no subcommands

Type in the CROSSTABS command crosstabs appy by ife into the

Syntax window

Figure 13 CROSSTABS command selected

A full stop is needed to indicate the end of each SPSS commandIf you forget to nish a command with a full stop the command

may not run properly

Select the whole CROSSTABS command as shown above and click on the

Run button to run it

If you switched on Dispay coands in te og demonstrated on page 2 then

the commands are displayed with the output then you will see the CROSSTABS

command just before the output from Crosstabs

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1754

Module 5 Starting with SPSS Syntax

11

S e s s i onA U s i n g S P S

S C omm an d s

Figure 14 Start of Output from the CROSSTABS command

The CROSSTABS command has several subcommands try the syntax chart button

to see what they are They include

CEllS used to change the content of the table

BARChART to add a clustered bar chart for each layer

Figure 15 CROSSTABS using CEllS and BARChART subcommands

Try typing in and running the new command in Figure 15

Layers can be added to the table by adding another BY keyword follwed by the layer

variable or variable list For example

c r o s s t a b s h a p p y b y l i f e b y r e g i o n

will produce a crosstabs table layer for each region Try adding a layer to the latest

crosstabs command and run the command to check the effect

Some command names such as FINISh BEGIN DATA and

END DATA are complete commands which need no further

specication However most commands (including all of the statistical subprograms) have to have a certain amount

of specication and each has its own particular form of

specication so check the syntax guide before you run

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1854

Module 5 Starting with SPSS Syntax

12

S e s s i o n A U

s i n g S P S S C o m m a n d s Slide Content from presentation

SPSS Coand language

Starting with commands

Wy use Coands

More exible

Extra features

Can be saved

And repeated

Te Synta Window

SPSS commands are entered in the Syntax Window

Create a Syntax Window

Either selecting File gt New gt Syntax from the menu bar

Or creating le with sps extension in a text editor

Use an existing syntax window

Either Selecting an open syntax window from the Window menu

Or selecting File gt Open gt Syntax from the menu bar

SPSS coands

Commands are entered in the Syntax Window by

Either opening the syntax Window and typing directly

Or building up a command using dialog boxes then clicking the

PASTE button

Or using a mixture of the two

Forat of Coands

Control Field - consists of SPSS keyword

Specication Field - names of variables etc on which command willoperate

Full stop - to terminate command

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1954

Module 5 Starting with SPSS Syntax

13

S e s s i onA U s i n g S P S

S C omm an d s

Synta Rues

Commands start with a command keyword

Commands are case insensitive

Except for text in quotes

A single command can go over many syntax lines

Each line of syntax must not exceed 256 bytes3

Do not break a syntax line

in a variable name

in between quotes lsquo lsquo or ldquo ldquo

For a syntax window

Each command must start on a new line

Each command must end with a full stop with no non-blanks after it

or end with a completely blank line afterwards

For batc coands (see ast session)

Each command begins on a new line in column 1

unless you use + or -

Continuation lines mustnrsquot start in column 1

Full stops command terminators are optional

Te FINISh coand

FINISH

For batch commands

Terminates a le of SPSS commands and closes SPSS tidily

NB When SPSS windows manager is running (eg during an SPSS

for Windows session) FINISH will generate an error

Terminate an interactive SPSS session using Exit from the File menu

3 256 bytes are characters in single byte languageshellip

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2054

Module 5 Starting with SPSS Syntax

14

S e s s i o n A U

s i n g S P S S C o m m a n d s

Session summaryIn this session we covered

How to display SPSS commands in output window as they are runbull

How using thebull Paste button in an SPSS dialog box pastes the underlying

command into a synta window

The Syntax Window where SPSS commands (also calledbull synta) are stored

Using the syntax chart button to get help about a command in the syntax windowbull

How to type in and run an SPSS command from thebull synta window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2154

Session B

SPSS CommandStructure

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2254

Module 5 Starting with SPSS Syntax

16

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Commands Subcommands amp keywords

In general an SPSS command will be a mixture of different elements like this -

v kyw kyw kyw

An SPSS command starts with a recognized command keyword which has to

be spelt correctly Each command has its own general format with its own set of

subcommands and keywords These are combined with lists of variables names

or combinations of variable names and keywords to specify the analysis or action

required by the computer

Subcommands and other keywords are words recognized by SPSS as part of

the commands specication and so appear in the specication eld Some

SPSS procedures have subcommands which can appear either on their own or in

association with certain keywords As with commands subcommands and keywords

have to appear exactly as they are spelt

Figure 17 Different keywords in the Commands example

get FILE=mspssworkCARDIACsav

crosstabs chd B Y famhist CELLS=C O U N T R O W C O L U M N STATISTICS=C H I S Q

requencies VARIABLES=height cigaret

FORMAT=L I M I T (12 ) HIST

graPH SCATTERPLOT weight W I T H height

inisH

The different types of keyword have been highlighted as follows-

command keywords SUBCOMMAND keywords

O T H E R keywords variable names and other specication

Abbreviating keywords

SPSS will recognise most of its command and subcommand keywords when

abbreviated to the rst three or four characters of the name as long as the keywordsare unique So for example-

freq vars=height cigaret form=lim(12) hist

can be used as an abbreviation of-

frequencies variables=height cigaret

format=limit(12)

histogram

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2354

Module 5 SPSS command Structure

17

S e s s i onB S P S S C om

m an d S t r u c t ur e

Reserved keywords

With most procedures it does not matter if there is a variable name the same as a

keyword since there are certain places in a command specication where a variable

or variable list is expected and others where keywords are expected However there

are some reserved keywords which cannot be used as names These are

ALL AND BY EQ GE GT LE LT NE NOT OR TO WITH

These words cannot be used as variable names in SPSS because the program

already recognizes them as having meaning For example

FREQUENCIES VARIABLES=ALL

calculates a frequency table for all the variables currently stored on SPSSs

active le To have a variable called A L L would make the sense of the instruction

ambiguous to SPSS

Lists of Variables

The variables are kept in the SPSS active le in the order they are given in the

DATA lIST command and any variables generated during the session in the order

they were created This order can be utilized when instead of typing a long list of

consecutive variables you type the name of the rst variable then the TO keyword

and then the name of the last variable in the list These inclusive ranges of variables

can be combined with the names of other variables to produce a larger variable list

where needed For example if we had the following variables in a system le-

LIST OF VARIABLES ON THE ACTIVE FILE

1 AGE 6 HHLDTYPE 11 OCPENAMT 16 SPSCLASS

2 CHRONILL 7 HOMEHELP 12 REBTOWN 17 TOTINCID

3 CLASS 8 KINNEAR 13 REBTRENT 18 UNEARNED

4 EMPLOYED 9 MARITAL 14 GENDER 19 CASWGT

5 HEALTH 10 NIRAMT 15 SPSAGE

In SPSS variable lists individual variable names andor variable ranges can

be separated by either spaces or commas However any keywords have to be

separated from variable names by spaces ie use spaces with TO or All

AGE TO CASWGT implies

AGE CHRONILL CLASS EMPLOYED HEALTH HHLDTYPEHOMEHELP KINNEAR MARITAL NIRAMT OCPENAMT REBTOWNREBTRENT GENDER SPSAGE SPSCLASS TOTINCID UNEARNEDCASWGT

This rst list is all the variables in the active le since AGE is the rst variable on the

list and CASWGT is the last

hhlDTYPE TO GENDER implies

HHLDTYPE HOMEHELP KINNEAR MARITAL NIRAMT OCPENAMTREBTOWN REBTRENT GENDER

Notice in this list 7 of the 9 variables are implied in the same order as the active le

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2454

Module 5 Starting with SPSS Syntax

18

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e ClASS hEAlTh TO NIRAmT implies

CLASS HEALTH HHLDTYPE HOMEHELP KINNEAR MARITALNIRAMT

A list can be combined with individual variables

AGE SPAGE ClASS SPClASS All implies

AGE SPSAGE CLASS SPSCLASS CHRONILLEMPLOYED HEALTH HHLDTYPE HOMEHELP KINNEARMARITAL NIRAMT OCPENAMT REBTOWN REBTRENT GENDERTOTINCID UNEARNED CASWGT

The reserved keyword ALL in this context means all the variables in the active le

It can be used in a variable list for the KEEP subcommand on a GET or SAVE

command to change the order the variables are saved in whilst making sure no

variables are missed Variables not specically listed will appear after all the listed

ones in active le order

If new variables are created during a session they will be added to the active le in

the order they were created after the variables already stored in the active le You

can always check the variable order in the active le using the DISPlAY command -

it may not be the same order they will appear in dialog boxes

Syntax Charts

The Sow synta cart button as seen in the previous session in Figure 11 will let

you look at command syntax charts If you have no command selected in the syntax

window then the button will give you an alphabetic list of all the command keywords

Look at the syntax chart for each of these commands to see what subcommands and keywords are available

Use the Search button to look for more information about each

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2554

Module 5 SPSS command Structure

19

S e s s i onB S P S S C om

m an d S t r u c t ur e

Some SPSS Command examples (from slides)

Te GET coand

The GET command reads a sav le into SPSS

GET FILE = cardiacsav

Te GET coand - KEEP and DROP subcoands

Use the KEEP subcommand to select only some of the variables

GET FILE=cardiacsavKEEP=famhist chd

Use the DROP subcommand to select all of the le except certain

variables

GET FILE=cardiacsavDROP=height weight

Te DATASET NAmE coand

The DATASET NAME command give the active dataset a short name

which can be used in later commands

GET FILE = mspssworkcardiacsav

DATASET NAME cardiac

Te SAVE coand

Use the SAVE command to save your data in a sav le

SAVE OUTFILE=myfilesav

Te SAVE coand wi

save all your cases together with variable names values

missing values variable and value labels and formats

Saved in the state they are in when the command is issued

Any further changes can only be saved with another SAVE command

It will NOT save the results of any analyses results are saved in an

output window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2654

Module 5 Starting with SPSS Syntax

20

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Te SAVE coand KEEP and DROP subcoands

The KEEP and DROP subcommands are available with the SAVE

command and perform the same function as with GET

Using KEEP will save only the named variables

SAVE OUTFILE=filesav KEEP=AGE HEIGHT

Using DROP will save all variables except named ones (here it means

all variables except WEIGHT2)

SAVE OUTFILE=filesav DROP=WEIGHT2

FREQUENCIES coand

FREQUENCIES VARIABLES=HEIGHT

FREQUENCIES - STATISTICS subcoand

FREQ VAR=HEIGHT AGE STATISTICS

Gives four default statistics

MEAN STDDEV MINIMUM MAXIMUM

FREQUENCIES - more statistics

MEDIAN MODE SUM RANGE SEMEAN VARIANCESKEWNESS SESKEW KURTOSIS SEKURT

STATISTICS=DEFAULT

Use to get the default four statistics MEAN STDDEV MINIMUM

MAXIMUM together with others you ask for

STATISTICS=ALL

Gives all available statistics - can be wasteful

STATISTICS=NONE

Equivalent to omitting STATISTICS altogether

FREQUENCIES - FORmAT subcoand

To suppress the frequency table use

FORMAT=NOTABLE

To suppress tables over a certain length

FORMAT=LIMIT(50)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2754

Module 5 SPSS command Structure

21

S e s s i onB S P S S C om

m an d S t r u c t ur e

FREQUENCIES - subcoands for carts

There are three subcommands to produce type of charts named

HISTOGRAM

BARCHART

PIECHART

Using ore tan one subcoand

To use more than one subcommand string them together and nish

the command with the terminating full stop

FREQUENCIES VARIABLES=AGESEX STATISTICS=RANGEBARCHART

CROSSTABS coand

CROSSTABS TABLES=AGE BY SEX

CROSSTABS AGE BY SEX

SEX BY WEIGHT

The next three crosstabs commands produce the same three tables

CROSSTABS AGE BY SEX

AGE BY PARTY

AGE BY INCOME

CROSSTABS AGE BY SEX PARTY INCOMECROSSTABS AGE BY SEX TO INCOME

CROSSTABS - STATISTICS subcoand

CROSSTABS HEIGHT BY AGE

STATISTICS

Gives the Chi-square statistic tests

CROSSTABS - more statistics

Others available

CHISQ PHI CC LAMBDA UC BTAU CTAU GAMMA DETA CORR ALL NONE

and KAPPA RISK MCNEMAR CMH(1)

Usage

CROSSTABS TABLES=AGE BY WEIGHT

STATISTICS=BTAU CORR

Only gives those statistics asked for

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 14: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1454

Module 5 Starting with SPSS Syntax

8

S e s s i o n A U

s i n g S P S S C o m m a n d s Figure 11 FREQUENCIES syntax chart

As you can see the chart is quite complicated but will give you all the elements you

can put in a FREQUENCIES command You maybe able to spot the subcommands

and keywords just described

Each element in square brackets [ ] indicates a optional element a list of elements

aligned above one another in curly brackets indicates a choice of one out of that

series of elements default choice is usually given in bold All SPSS command sub

command and other keywords are presented in uppercase in chart (although you

dont need to type them in uppercase) Check the syntax reference guide or on-line

help for examples of how each command is used

Once you are nished having a look at the help information close the Helpwindow and you should go back to the Syntax window

The SPSS Command Syntax Reference guide will give you moredetails about SPSS commands and how to run them Syntax

guides are available on the SPSS manual CD or within SPSS in

PDF format from Hep gt Coand Snax Reference

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1554

Module 5 Starting with SPSS Syntax

9

S e s s i onA U s i n g S P S

S C omm an d s

Running an SPSS commandThe Run Current button on the syntax window tool bar looks like an audio play

button Its used to run the command currently selected in the syntax window It is

easy to run an already pasted command either click somewhere on the command

or highlight the entire command (including the full stop) and click the run button

Figure 12 Running the FREQUENCIES command

Click somewhere on the FREQUENCIES command and click the Run

button to execute the command

You will see the procedure being run as you did when you used the menu to run

Frequencies When the processor has nished - it shouldnt take very long - the

results will be stored in the output window as usual

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1654

Module 5 Starting with SPSS Syntax

10

S e s s i o n A U

s i n g S P S S C o m m a n d s Typing in a command

The Syntax window can be used on its own to run SPSS commands if you know

what the commands are (or have generated them from the menus) A new syntax

window can be opened by choosing Fie gt New gt Synta or an existing one can be

opened using Fie gt Open gt Synta A syntax window can be brought to the front by

choosing it from the Windows menu if it is already open

Select the Syntax window from the Window menuWindow gt 3 Snax1 ndash SPSS Snax Edior

The rst syntax window is usually called Synta1 and the 3 before the name

indicates its the third open SPSS window after the data editor and output window If

you have more windows open then the number might be different

The CROSSTABS command

The following CROSSTABS command will cross-tabulate the appy variable by the

ife variable from the social survey dataset

c r o s s t a b s h a p p y b y l i f e

It is a simple form of the CROSSTABS command with no subcommands

Type in the CROSSTABS command crosstabs appy by ife into the

Syntax window

Figure 13 CROSSTABS command selected

A full stop is needed to indicate the end of each SPSS commandIf you forget to nish a command with a full stop the command

may not run properly

Select the whole CROSSTABS command as shown above and click on the

Run button to run it

If you switched on Dispay coands in te og demonstrated on page 2 then

the commands are displayed with the output then you will see the CROSSTABS

command just before the output from Crosstabs

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1754

Module 5 Starting with SPSS Syntax

11

S e s s i onA U s i n g S P S

S C omm an d s

Figure 14 Start of Output from the CROSSTABS command

The CROSSTABS command has several subcommands try the syntax chart button

to see what they are They include

CEllS used to change the content of the table

BARChART to add a clustered bar chart for each layer

Figure 15 CROSSTABS using CEllS and BARChART subcommands

Try typing in and running the new command in Figure 15

Layers can be added to the table by adding another BY keyword follwed by the layer

variable or variable list For example

c r o s s t a b s h a p p y b y l i f e b y r e g i o n

will produce a crosstabs table layer for each region Try adding a layer to the latest

crosstabs command and run the command to check the effect

Some command names such as FINISh BEGIN DATA and

END DATA are complete commands which need no further

specication However most commands (including all of the statistical subprograms) have to have a certain amount

of specication and each has its own particular form of

specication so check the syntax guide before you run

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1854

Module 5 Starting with SPSS Syntax

12

S e s s i o n A U

s i n g S P S S C o m m a n d s Slide Content from presentation

SPSS Coand language

Starting with commands

Wy use Coands

More exible

Extra features

Can be saved

And repeated

Te Synta Window

SPSS commands are entered in the Syntax Window

Create a Syntax Window

Either selecting File gt New gt Syntax from the menu bar

Or creating le with sps extension in a text editor

Use an existing syntax window

Either Selecting an open syntax window from the Window menu

Or selecting File gt Open gt Syntax from the menu bar

SPSS coands

Commands are entered in the Syntax Window by

Either opening the syntax Window and typing directly

Or building up a command using dialog boxes then clicking the

PASTE button

Or using a mixture of the two

Forat of Coands

Control Field - consists of SPSS keyword

Specication Field - names of variables etc on which command willoperate

Full stop - to terminate command

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1954

Module 5 Starting with SPSS Syntax

13

S e s s i onA U s i n g S P S

S C omm an d s

Synta Rues

Commands start with a command keyword

Commands are case insensitive

Except for text in quotes

A single command can go over many syntax lines

Each line of syntax must not exceed 256 bytes3

Do not break a syntax line

in a variable name

in between quotes lsquo lsquo or ldquo ldquo

For a syntax window

Each command must start on a new line

Each command must end with a full stop with no non-blanks after it

or end with a completely blank line afterwards

For batc coands (see ast session)

Each command begins on a new line in column 1

unless you use + or -

Continuation lines mustnrsquot start in column 1

Full stops command terminators are optional

Te FINISh coand

FINISH

For batch commands

Terminates a le of SPSS commands and closes SPSS tidily

NB When SPSS windows manager is running (eg during an SPSS

for Windows session) FINISH will generate an error

Terminate an interactive SPSS session using Exit from the File menu

3 256 bytes are characters in single byte languageshellip

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2054

Module 5 Starting with SPSS Syntax

14

S e s s i o n A U

s i n g S P S S C o m m a n d s

Session summaryIn this session we covered

How to display SPSS commands in output window as they are runbull

How using thebull Paste button in an SPSS dialog box pastes the underlying

command into a synta window

The Syntax Window where SPSS commands (also calledbull synta) are stored

Using the syntax chart button to get help about a command in the syntax windowbull

How to type in and run an SPSS command from thebull synta window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2154

Session B

SPSS CommandStructure

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2254

Module 5 Starting with SPSS Syntax

16

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Commands Subcommands amp keywords

In general an SPSS command will be a mixture of different elements like this -

v kyw kyw kyw

An SPSS command starts with a recognized command keyword which has to

be spelt correctly Each command has its own general format with its own set of

subcommands and keywords These are combined with lists of variables names

or combinations of variable names and keywords to specify the analysis or action

required by the computer

Subcommands and other keywords are words recognized by SPSS as part of

the commands specication and so appear in the specication eld Some

SPSS procedures have subcommands which can appear either on their own or in

association with certain keywords As with commands subcommands and keywords

have to appear exactly as they are spelt

Figure 17 Different keywords in the Commands example

get FILE=mspssworkCARDIACsav

crosstabs chd B Y famhist CELLS=C O U N T R O W C O L U M N STATISTICS=C H I S Q

requencies VARIABLES=height cigaret

FORMAT=L I M I T (12 ) HIST

graPH SCATTERPLOT weight W I T H height

inisH

The different types of keyword have been highlighted as follows-

command keywords SUBCOMMAND keywords

O T H E R keywords variable names and other specication

Abbreviating keywords

SPSS will recognise most of its command and subcommand keywords when

abbreviated to the rst three or four characters of the name as long as the keywordsare unique So for example-

freq vars=height cigaret form=lim(12) hist

can be used as an abbreviation of-

frequencies variables=height cigaret

format=limit(12)

histogram

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2354

Module 5 SPSS command Structure

17

S e s s i onB S P S S C om

m an d S t r u c t ur e

Reserved keywords

With most procedures it does not matter if there is a variable name the same as a

keyword since there are certain places in a command specication where a variable

or variable list is expected and others where keywords are expected However there

are some reserved keywords which cannot be used as names These are

ALL AND BY EQ GE GT LE LT NE NOT OR TO WITH

These words cannot be used as variable names in SPSS because the program

already recognizes them as having meaning For example

FREQUENCIES VARIABLES=ALL

calculates a frequency table for all the variables currently stored on SPSSs

active le To have a variable called A L L would make the sense of the instruction

ambiguous to SPSS

Lists of Variables

The variables are kept in the SPSS active le in the order they are given in the

DATA lIST command and any variables generated during the session in the order

they were created This order can be utilized when instead of typing a long list of

consecutive variables you type the name of the rst variable then the TO keyword

and then the name of the last variable in the list These inclusive ranges of variables

can be combined with the names of other variables to produce a larger variable list

where needed For example if we had the following variables in a system le-

LIST OF VARIABLES ON THE ACTIVE FILE

1 AGE 6 HHLDTYPE 11 OCPENAMT 16 SPSCLASS

2 CHRONILL 7 HOMEHELP 12 REBTOWN 17 TOTINCID

3 CLASS 8 KINNEAR 13 REBTRENT 18 UNEARNED

4 EMPLOYED 9 MARITAL 14 GENDER 19 CASWGT

5 HEALTH 10 NIRAMT 15 SPSAGE

In SPSS variable lists individual variable names andor variable ranges can

be separated by either spaces or commas However any keywords have to be

separated from variable names by spaces ie use spaces with TO or All

AGE TO CASWGT implies

AGE CHRONILL CLASS EMPLOYED HEALTH HHLDTYPEHOMEHELP KINNEAR MARITAL NIRAMT OCPENAMT REBTOWNREBTRENT GENDER SPSAGE SPSCLASS TOTINCID UNEARNEDCASWGT

This rst list is all the variables in the active le since AGE is the rst variable on the

list and CASWGT is the last

hhlDTYPE TO GENDER implies

HHLDTYPE HOMEHELP KINNEAR MARITAL NIRAMT OCPENAMTREBTOWN REBTRENT GENDER

Notice in this list 7 of the 9 variables are implied in the same order as the active le

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2454

Module 5 Starting with SPSS Syntax

18

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e ClASS hEAlTh TO NIRAmT implies

CLASS HEALTH HHLDTYPE HOMEHELP KINNEAR MARITALNIRAMT

A list can be combined with individual variables

AGE SPAGE ClASS SPClASS All implies

AGE SPSAGE CLASS SPSCLASS CHRONILLEMPLOYED HEALTH HHLDTYPE HOMEHELP KINNEARMARITAL NIRAMT OCPENAMT REBTOWN REBTRENT GENDERTOTINCID UNEARNED CASWGT

The reserved keyword ALL in this context means all the variables in the active le

It can be used in a variable list for the KEEP subcommand on a GET or SAVE

command to change the order the variables are saved in whilst making sure no

variables are missed Variables not specically listed will appear after all the listed

ones in active le order

If new variables are created during a session they will be added to the active le in

the order they were created after the variables already stored in the active le You

can always check the variable order in the active le using the DISPlAY command -

it may not be the same order they will appear in dialog boxes

Syntax Charts

The Sow synta cart button as seen in the previous session in Figure 11 will let

you look at command syntax charts If you have no command selected in the syntax

window then the button will give you an alphabetic list of all the command keywords

Look at the syntax chart for each of these commands to see what subcommands and keywords are available

Use the Search button to look for more information about each

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2554

Module 5 SPSS command Structure

19

S e s s i onB S P S S C om

m an d S t r u c t ur e

Some SPSS Command examples (from slides)

Te GET coand

The GET command reads a sav le into SPSS

GET FILE = cardiacsav

Te GET coand - KEEP and DROP subcoands

Use the KEEP subcommand to select only some of the variables

GET FILE=cardiacsavKEEP=famhist chd

Use the DROP subcommand to select all of the le except certain

variables

GET FILE=cardiacsavDROP=height weight

Te DATASET NAmE coand

The DATASET NAME command give the active dataset a short name

which can be used in later commands

GET FILE = mspssworkcardiacsav

DATASET NAME cardiac

Te SAVE coand

Use the SAVE command to save your data in a sav le

SAVE OUTFILE=myfilesav

Te SAVE coand wi

save all your cases together with variable names values

missing values variable and value labels and formats

Saved in the state they are in when the command is issued

Any further changes can only be saved with another SAVE command

It will NOT save the results of any analyses results are saved in an

output window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2654

Module 5 Starting with SPSS Syntax

20

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Te SAVE coand KEEP and DROP subcoands

The KEEP and DROP subcommands are available with the SAVE

command and perform the same function as with GET

Using KEEP will save only the named variables

SAVE OUTFILE=filesav KEEP=AGE HEIGHT

Using DROP will save all variables except named ones (here it means

all variables except WEIGHT2)

SAVE OUTFILE=filesav DROP=WEIGHT2

FREQUENCIES coand

FREQUENCIES VARIABLES=HEIGHT

FREQUENCIES - STATISTICS subcoand

FREQ VAR=HEIGHT AGE STATISTICS

Gives four default statistics

MEAN STDDEV MINIMUM MAXIMUM

FREQUENCIES - more statistics

MEDIAN MODE SUM RANGE SEMEAN VARIANCESKEWNESS SESKEW KURTOSIS SEKURT

STATISTICS=DEFAULT

Use to get the default four statistics MEAN STDDEV MINIMUM

MAXIMUM together with others you ask for

STATISTICS=ALL

Gives all available statistics - can be wasteful

STATISTICS=NONE

Equivalent to omitting STATISTICS altogether

FREQUENCIES - FORmAT subcoand

To suppress the frequency table use

FORMAT=NOTABLE

To suppress tables over a certain length

FORMAT=LIMIT(50)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2754

Module 5 SPSS command Structure

21

S e s s i onB S P S S C om

m an d S t r u c t ur e

FREQUENCIES - subcoands for carts

There are three subcommands to produce type of charts named

HISTOGRAM

BARCHART

PIECHART

Using ore tan one subcoand

To use more than one subcommand string them together and nish

the command with the terminating full stop

FREQUENCIES VARIABLES=AGESEX STATISTICS=RANGEBARCHART

CROSSTABS coand

CROSSTABS TABLES=AGE BY SEX

CROSSTABS AGE BY SEX

SEX BY WEIGHT

The next three crosstabs commands produce the same three tables

CROSSTABS AGE BY SEX

AGE BY PARTY

AGE BY INCOME

CROSSTABS AGE BY SEX PARTY INCOMECROSSTABS AGE BY SEX TO INCOME

CROSSTABS - STATISTICS subcoand

CROSSTABS HEIGHT BY AGE

STATISTICS

Gives the Chi-square statistic tests

CROSSTABS - more statistics

Others available

CHISQ PHI CC LAMBDA UC BTAU CTAU GAMMA DETA CORR ALL NONE

and KAPPA RISK MCNEMAR CMH(1)

Usage

CROSSTABS TABLES=AGE BY WEIGHT

STATISTICS=BTAU CORR

Only gives those statistics asked for

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 15: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1554

Module 5 Starting with SPSS Syntax

9

S e s s i onA U s i n g S P S

S C omm an d s

Running an SPSS commandThe Run Current button on the syntax window tool bar looks like an audio play

button Its used to run the command currently selected in the syntax window It is

easy to run an already pasted command either click somewhere on the command

or highlight the entire command (including the full stop) and click the run button

Figure 12 Running the FREQUENCIES command

Click somewhere on the FREQUENCIES command and click the Run

button to execute the command

You will see the procedure being run as you did when you used the menu to run

Frequencies When the processor has nished - it shouldnt take very long - the

results will be stored in the output window as usual

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1654

Module 5 Starting with SPSS Syntax

10

S e s s i o n A U

s i n g S P S S C o m m a n d s Typing in a command

The Syntax window can be used on its own to run SPSS commands if you know

what the commands are (or have generated them from the menus) A new syntax

window can be opened by choosing Fie gt New gt Synta or an existing one can be

opened using Fie gt Open gt Synta A syntax window can be brought to the front by

choosing it from the Windows menu if it is already open

Select the Syntax window from the Window menuWindow gt 3 Snax1 ndash SPSS Snax Edior

The rst syntax window is usually called Synta1 and the 3 before the name

indicates its the third open SPSS window after the data editor and output window If

you have more windows open then the number might be different

The CROSSTABS command

The following CROSSTABS command will cross-tabulate the appy variable by the

ife variable from the social survey dataset

c r o s s t a b s h a p p y b y l i f e

It is a simple form of the CROSSTABS command with no subcommands

Type in the CROSSTABS command crosstabs appy by ife into the

Syntax window

Figure 13 CROSSTABS command selected

A full stop is needed to indicate the end of each SPSS commandIf you forget to nish a command with a full stop the command

may not run properly

Select the whole CROSSTABS command as shown above and click on the

Run button to run it

If you switched on Dispay coands in te og demonstrated on page 2 then

the commands are displayed with the output then you will see the CROSSTABS

command just before the output from Crosstabs

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1754

Module 5 Starting with SPSS Syntax

11

S e s s i onA U s i n g S P S

S C omm an d s

Figure 14 Start of Output from the CROSSTABS command

The CROSSTABS command has several subcommands try the syntax chart button

to see what they are They include

CEllS used to change the content of the table

BARChART to add a clustered bar chart for each layer

Figure 15 CROSSTABS using CEllS and BARChART subcommands

Try typing in and running the new command in Figure 15

Layers can be added to the table by adding another BY keyword follwed by the layer

variable or variable list For example

c r o s s t a b s h a p p y b y l i f e b y r e g i o n

will produce a crosstabs table layer for each region Try adding a layer to the latest

crosstabs command and run the command to check the effect

Some command names such as FINISh BEGIN DATA and

END DATA are complete commands which need no further

specication However most commands (including all of the statistical subprograms) have to have a certain amount

of specication and each has its own particular form of

specication so check the syntax guide before you run

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1854

Module 5 Starting with SPSS Syntax

12

S e s s i o n A U

s i n g S P S S C o m m a n d s Slide Content from presentation

SPSS Coand language

Starting with commands

Wy use Coands

More exible

Extra features

Can be saved

And repeated

Te Synta Window

SPSS commands are entered in the Syntax Window

Create a Syntax Window

Either selecting File gt New gt Syntax from the menu bar

Or creating le with sps extension in a text editor

Use an existing syntax window

Either Selecting an open syntax window from the Window menu

Or selecting File gt Open gt Syntax from the menu bar

SPSS coands

Commands are entered in the Syntax Window by

Either opening the syntax Window and typing directly

Or building up a command using dialog boxes then clicking the

PASTE button

Or using a mixture of the two

Forat of Coands

Control Field - consists of SPSS keyword

Specication Field - names of variables etc on which command willoperate

Full stop - to terminate command

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1954

Module 5 Starting with SPSS Syntax

13

S e s s i onA U s i n g S P S

S C omm an d s

Synta Rues

Commands start with a command keyword

Commands are case insensitive

Except for text in quotes

A single command can go over many syntax lines

Each line of syntax must not exceed 256 bytes3

Do not break a syntax line

in a variable name

in between quotes lsquo lsquo or ldquo ldquo

For a syntax window

Each command must start on a new line

Each command must end with a full stop with no non-blanks after it

or end with a completely blank line afterwards

For batc coands (see ast session)

Each command begins on a new line in column 1

unless you use + or -

Continuation lines mustnrsquot start in column 1

Full stops command terminators are optional

Te FINISh coand

FINISH

For batch commands

Terminates a le of SPSS commands and closes SPSS tidily

NB When SPSS windows manager is running (eg during an SPSS

for Windows session) FINISH will generate an error

Terminate an interactive SPSS session using Exit from the File menu

3 256 bytes are characters in single byte languageshellip

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2054

Module 5 Starting with SPSS Syntax

14

S e s s i o n A U

s i n g S P S S C o m m a n d s

Session summaryIn this session we covered

How to display SPSS commands in output window as they are runbull

How using thebull Paste button in an SPSS dialog box pastes the underlying

command into a synta window

The Syntax Window where SPSS commands (also calledbull synta) are stored

Using the syntax chart button to get help about a command in the syntax windowbull

How to type in and run an SPSS command from thebull synta window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2154

Session B

SPSS CommandStructure

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2254

Module 5 Starting with SPSS Syntax

16

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Commands Subcommands amp keywords

In general an SPSS command will be a mixture of different elements like this -

v kyw kyw kyw

An SPSS command starts with a recognized command keyword which has to

be spelt correctly Each command has its own general format with its own set of

subcommands and keywords These are combined with lists of variables names

or combinations of variable names and keywords to specify the analysis or action

required by the computer

Subcommands and other keywords are words recognized by SPSS as part of

the commands specication and so appear in the specication eld Some

SPSS procedures have subcommands which can appear either on their own or in

association with certain keywords As with commands subcommands and keywords

have to appear exactly as they are spelt

Figure 17 Different keywords in the Commands example

get FILE=mspssworkCARDIACsav

crosstabs chd B Y famhist CELLS=C O U N T R O W C O L U M N STATISTICS=C H I S Q

requencies VARIABLES=height cigaret

FORMAT=L I M I T (12 ) HIST

graPH SCATTERPLOT weight W I T H height

inisH

The different types of keyword have been highlighted as follows-

command keywords SUBCOMMAND keywords

O T H E R keywords variable names and other specication

Abbreviating keywords

SPSS will recognise most of its command and subcommand keywords when

abbreviated to the rst three or four characters of the name as long as the keywordsare unique So for example-

freq vars=height cigaret form=lim(12) hist

can be used as an abbreviation of-

frequencies variables=height cigaret

format=limit(12)

histogram

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2354

Module 5 SPSS command Structure

17

S e s s i onB S P S S C om

m an d S t r u c t ur e

Reserved keywords

With most procedures it does not matter if there is a variable name the same as a

keyword since there are certain places in a command specication where a variable

or variable list is expected and others where keywords are expected However there

are some reserved keywords which cannot be used as names These are

ALL AND BY EQ GE GT LE LT NE NOT OR TO WITH

These words cannot be used as variable names in SPSS because the program

already recognizes them as having meaning For example

FREQUENCIES VARIABLES=ALL

calculates a frequency table for all the variables currently stored on SPSSs

active le To have a variable called A L L would make the sense of the instruction

ambiguous to SPSS

Lists of Variables

The variables are kept in the SPSS active le in the order they are given in the

DATA lIST command and any variables generated during the session in the order

they were created This order can be utilized when instead of typing a long list of

consecutive variables you type the name of the rst variable then the TO keyword

and then the name of the last variable in the list These inclusive ranges of variables

can be combined with the names of other variables to produce a larger variable list

where needed For example if we had the following variables in a system le-

LIST OF VARIABLES ON THE ACTIVE FILE

1 AGE 6 HHLDTYPE 11 OCPENAMT 16 SPSCLASS

2 CHRONILL 7 HOMEHELP 12 REBTOWN 17 TOTINCID

3 CLASS 8 KINNEAR 13 REBTRENT 18 UNEARNED

4 EMPLOYED 9 MARITAL 14 GENDER 19 CASWGT

5 HEALTH 10 NIRAMT 15 SPSAGE

In SPSS variable lists individual variable names andor variable ranges can

be separated by either spaces or commas However any keywords have to be

separated from variable names by spaces ie use spaces with TO or All

AGE TO CASWGT implies

AGE CHRONILL CLASS EMPLOYED HEALTH HHLDTYPEHOMEHELP KINNEAR MARITAL NIRAMT OCPENAMT REBTOWNREBTRENT GENDER SPSAGE SPSCLASS TOTINCID UNEARNEDCASWGT

This rst list is all the variables in the active le since AGE is the rst variable on the

list and CASWGT is the last

hhlDTYPE TO GENDER implies

HHLDTYPE HOMEHELP KINNEAR MARITAL NIRAMT OCPENAMTREBTOWN REBTRENT GENDER

Notice in this list 7 of the 9 variables are implied in the same order as the active le

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2454

Module 5 Starting with SPSS Syntax

18

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e ClASS hEAlTh TO NIRAmT implies

CLASS HEALTH HHLDTYPE HOMEHELP KINNEAR MARITALNIRAMT

A list can be combined with individual variables

AGE SPAGE ClASS SPClASS All implies

AGE SPSAGE CLASS SPSCLASS CHRONILLEMPLOYED HEALTH HHLDTYPE HOMEHELP KINNEARMARITAL NIRAMT OCPENAMT REBTOWN REBTRENT GENDERTOTINCID UNEARNED CASWGT

The reserved keyword ALL in this context means all the variables in the active le

It can be used in a variable list for the KEEP subcommand on a GET or SAVE

command to change the order the variables are saved in whilst making sure no

variables are missed Variables not specically listed will appear after all the listed

ones in active le order

If new variables are created during a session they will be added to the active le in

the order they were created after the variables already stored in the active le You

can always check the variable order in the active le using the DISPlAY command -

it may not be the same order they will appear in dialog boxes

Syntax Charts

The Sow synta cart button as seen in the previous session in Figure 11 will let

you look at command syntax charts If you have no command selected in the syntax

window then the button will give you an alphabetic list of all the command keywords

Look at the syntax chart for each of these commands to see what subcommands and keywords are available

Use the Search button to look for more information about each

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2554

Module 5 SPSS command Structure

19

S e s s i onB S P S S C om

m an d S t r u c t ur e

Some SPSS Command examples (from slides)

Te GET coand

The GET command reads a sav le into SPSS

GET FILE = cardiacsav

Te GET coand - KEEP and DROP subcoands

Use the KEEP subcommand to select only some of the variables

GET FILE=cardiacsavKEEP=famhist chd

Use the DROP subcommand to select all of the le except certain

variables

GET FILE=cardiacsavDROP=height weight

Te DATASET NAmE coand

The DATASET NAME command give the active dataset a short name

which can be used in later commands

GET FILE = mspssworkcardiacsav

DATASET NAME cardiac

Te SAVE coand

Use the SAVE command to save your data in a sav le

SAVE OUTFILE=myfilesav

Te SAVE coand wi

save all your cases together with variable names values

missing values variable and value labels and formats

Saved in the state they are in when the command is issued

Any further changes can only be saved with another SAVE command

It will NOT save the results of any analyses results are saved in an

output window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2654

Module 5 Starting with SPSS Syntax

20

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Te SAVE coand KEEP and DROP subcoands

The KEEP and DROP subcommands are available with the SAVE

command and perform the same function as with GET

Using KEEP will save only the named variables

SAVE OUTFILE=filesav KEEP=AGE HEIGHT

Using DROP will save all variables except named ones (here it means

all variables except WEIGHT2)

SAVE OUTFILE=filesav DROP=WEIGHT2

FREQUENCIES coand

FREQUENCIES VARIABLES=HEIGHT

FREQUENCIES - STATISTICS subcoand

FREQ VAR=HEIGHT AGE STATISTICS

Gives four default statistics

MEAN STDDEV MINIMUM MAXIMUM

FREQUENCIES - more statistics

MEDIAN MODE SUM RANGE SEMEAN VARIANCESKEWNESS SESKEW KURTOSIS SEKURT

STATISTICS=DEFAULT

Use to get the default four statistics MEAN STDDEV MINIMUM

MAXIMUM together with others you ask for

STATISTICS=ALL

Gives all available statistics - can be wasteful

STATISTICS=NONE

Equivalent to omitting STATISTICS altogether

FREQUENCIES - FORmAT subcoand

To suppress the frequency table use

FORMAT=NOTABLE

To suppress tables over a certain length

FORMAT=LIMIT(50)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2754

Module 5 SPSS command Structure

21

S e s s i onB S P S S C om

m an d S t r u c t ur e

FREQUENCIES - subcoands for carts

There are three subcommands to produce type of charts named

HISTOGRAM

BARCHART

PIECHART

Using ore tan one subcoand

To use more than one subcommand string them together and nish

the command with the terminating full stop

FREQUENCIES VARIABLES=AGESEX STATISTICS=RANGEBARCHART

CROSSTABS coand

CROSSTABS TABLES=AGE BY SEX

CROSSTABS AGE BY SEX

SEX BY WEIGHT

The next three crosstabs commands produce the same three tables

CROSSTABS AGE BY SEX

AGE BY PARTY

AGE BY INCOME

CROSSTABS AGE BY SEX PARTY INCOMECROSSTABS AGE BY SEX TO INCOME

CROSSTABS - STATISTICS subcoand

CROSSTABS HEIGHT BY AGE

STATISTICS

Gives the Chi-square statistic tests

CROSSTABS - more statistics

Others available

CHISQ PHI CC LAMBDA UC BTAU CTAU GAMMA DETA CORR ALL NONE

and KAPPA RISK MCNEMAR CMH(1)

Usage

CROSSTABS TABLES=AGE BY WEIGHT

STATISTICS=BTAU CORR

Only gives those statistics asked for

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 16: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1654

Module 5 Starting with SPSS Syntax

10

S e s s i o n A U

s i n g S P S S C o m m a n d s Typing in a command

The Syntax window can be used on its own to run SPSS commands if you know

what the commands are (or have generated them from the menus) A new syntax

window can be opened by choosing Fie gt New gt Synta or an existing one can be

opened using Fie gt Open gt Synta A syntax window can be brought to the front by

choosing it from the Windows menu if it is already open

Select the Syntax window from the Window menuWindow gt 3 Snax1 ndash SPSS Snax Edior

The rst syntax window is usually called Synta1 and the 3 before the name

indicates its the third open SPSS window after the data editor and output window If

you have more windows open then the number might be different

The CROSSTABS command

The following CROSSTABS command will cross-tabulate the appy variable by the

ife variable from the social survey dataset

c r o s s t a b s h a p p y b y l i f e

It is a simple form of the CROSSTABS command with no subcommands

Type in the CROSSTABS command crosstabs appy by ife into the

Syntax window

Figure 13 CROSSTABS command selected

A full stop is needed to indicate the end of each SPSS commandIf you forget to nish a command with a full stop the command

may not run properly

Select the whole CROSSTABS command as shown above and click on the

Run button to run it

If you switched on Dispay coands in te og demonstrated on page 2 then

the commands are displayed with the output then you will see the CROSSTABS

command just before the output from Crosstabs

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1754

Module 5 Starting with SPSS Syntax

11

S e s s i onA U s i n g S P S

S C omm an d s

Figure 14 Start of Output from the CROSSTABS command

The CROSSTABS command has several subcommands try the syntax chart button

to see what they are They include

CEllS used to change the content of the table

BARChART to add a clustered bar chart for each layer

Figure 15 CROSSTABS using CEllS and BARChART subcommands

Try typing in and running the new command in Figure 15

Layers can be added to the table by adding another BY keyword follwed by the layer

variable or variable list For example

c r o s s t a b s h a p p y b y l i f e b y r e g i o n

will produce a crosstabs table layer for each region Try adding a layer to the latest

crosstabs command and run the command to check the effect

Some command names such as FINISh BEGIN DATA and

END DATA are complete commands which need no further

specication However most commands (including all of the statistical subprograms) have to have a certain amount

of specication and each has its own particular form of

specication so check the syntax guide before you run

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1854

Module 5 Starting with SPSS Syntax

12

S e s s i o n A U

s i n g S P S S C o m m a n d s Slide Content from presentation

SPSS Coand language

Starting with commands

Wy use Coands

More exible

Extra features

Can be saved

And repeated

Te Synta Window

SPSS commands are entered in the Syntax Window

Create a Syntax Window

Either selecting File gt New gt Syntax from the menu bar

Or creating le with sps extension in a text editor

Use an existing syntax window

Either Selecting an open syntax window from the Window menu

Or selecting File gt Open gt Syntax from the menu bar

SPSS coands

Commands are entered in the Syntax Window by

Either opening the syntax Window and typing directly

Or building up a command using dialog boxes then clicking the

PASTE button

Or using a mixture of the two

Forat of Coands

Control Field - consists of SPSS keyword

Specication Field - names of variables etc on which command willoperate

Full stop - to terminate command

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1954

Module 5 Starting with SPSS Syntax

13

S e s s i onA U s i n g S P S

S C omm an d s

Synta Rues

Commands start with a command keyword

Commands are case insensitive

Except for text in quotes

A single command can go over many syntax lines

Each line of syntax must not exceed 256 bytes3

Do not break a syntax line

in a variable name

in between quotes lsquo lsquo or ldquo ldquo

For a syntax window

Each command must start on a new line

Each command must end with a full stop with no non-blanks after it

or end with a completely blank line afterwards

For batc coands (see ast session)

Each command begins on a new line in column 1

unless you use + or -

Continuation lines mustnrsquot start in column 1

Full stops command terminators are optional

Te FINISh coand

FINISH

For batch commands

Terminates a le of SPSS commands and closes SPSS tidily

NB When SPSS windows manager is running (eg during an SPSS

for Windows session) FINISH will generate an error

Terminate an interactive SPSS session using Exit from the File menu

3 256 bytes are characters in single byte languageshellip

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2054

Module 5 Starting with SPSS Syntax

14

S e s s i o n A U

s i n g S P S S C o m m a n d s

Session summaryIn this session we covered

How to display SPSS commands in output window as they are runbull

How using thebull Paste button in an SPSS dialog box pastes the underlying

command into a synta window

The Syntax Window where SPSS commands (also calledbull synta) are stored

Using the syntax chart button to get help about a command in the syntax windowbull

How to type in and run an SPSS command from thebull synta window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2154

Session B

SPSS CommandStructure

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2254

Module 5 Starting with SPSS Syntax

16

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Commands Subcommands amp keywords

In general an SPSS command will be a mixture of different elements like this -

v kyw kyw kyw

An SPSS command starts with a recognized command keyword which has to

be spelt correctly Each command has its own general format with its own set of

subcommands and keywords These are combined with lists of variables names

or combinations of variable names and keywords to specify the analysis or action

required by the computer

Subcommands and other keywords are words recognized by SPSS as part of

the commands specication and so appear in the specication eld Some

SPSS procedures have subcommands which can appear either on their own or in

association with certain keywords As with commands subcommands and keywords

have to appear exactly as they are spelt

Figure 17 Different keywords in the Commands example

get FILE=mspssworkCARDIACsav

crosstabs chd B Y famhist CELLS=C O U N T R O W C O L U M N STATISTICS=C H I S Q

requencies VARIABLES=height cigaret

FORMAT=L I M I T (12 ) HIST

graPH SCATTERPLOT weight W I T H height

inisH

The different types of keyword have been highlighted as follows-

command keywords SUBCOMMAND keywords

O T H E R keywords variable names and other specication

Abbreviating keywords

SPSS will recognise most of its command and subcommand keywords when

abbreviated to the rst three or four characters of the name as long as the keywordsare unique So for example-

freq vars=height cigaret form=lim(12) hist

can be used as an abbreviation of-

frequencies variables=height cigaret

format=limit(12)

histogram

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2354

Module 5 SPSS command Structure

17

S e s s i onB S P S S C om

m an d S t r u c t ur e

Reserved keywords

With most procedures it does not matter if there is a variable name the same as a

keyword since there are certain places in a command specication where a variable

or variable list is expected and others where keywords are expected However there

are some reserved keywords which cannot be used as names These are

ALL AND BY EQ GE GT LE LT NE NOT OR TO WITH

These words cannot be used as variable names in SPSS because the program

already recognizes them as having meaning For example

FREQUENCIES VARIABLES=ALL

calculates a frequency table for all the variables currently stored on SPSSs

active le To have a variable called A L L would make the sense of the instruction

ambiguous to SPSS

Lists of Variables

The variables are kept in the SPSS active le in the order they are given in the

DATA lIST command and any variables generated during the session in the order

they were created This order can be utilized when instead of typing a long list of

consecutive variables you type the name of the rst variable then the TO keyword

and then the name of the last variable in the list These inclusive ranges of variables

can be combined with the names of other variables to produce a larger variable list

where needed For example if we had the following variables in a system le-

LIST OF VARIABLES ON THE ACTIVE FILE

1 AGE 6 HHLDTYPE 11 OCPENAMT 16 SPSCLASS

2 CHRONILL 7 HOMEHELP 12 REBTOWN 17 TOTINCID

3 CLASS 8 KINNEAR 13 REBTRENT 18 UNEARNED

4 EMPLOYED 9 MARITAL 14 GENDER 19 CASWGT

5 HEALTH 10 NIRAMT 15 SPSAGE

In SPSS variable lists individual variable names andor variable ranges can

be separated by either spaces or commas However any keywords have to be

separated from variable names by spaces ie use spaces with TO or All

AGE TO CASWGT implies

AGE CHRONILL CLASS EMPLOYED HEALTH HHLDTYPEHOMEHELP KINNEAR MARITAL NIRAMT OCPENAMT REBTOWNREBTRENT GENDER SPSAGE SPSCLASS TOTINCID UNEARNEDCASWGT

This rst list is all the variables in the active le since AGE is the rst variable on the

list and CASWGT is the last

hhlDTYPE TO GENDER implies

HHLDTYPE HOMEHELP KINNEAR MARITAL NIRAMT OCPENAMTREBTOWN REBTRENT GENDER

Notice in this list 7 of the 9 variables are implied in the same order as the active le

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2454

Module 5 Starting with SPSS Syntax

18

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e ClASS hEAlTh TO NIRAmT implies

CLASS HEALTH HHLDTYPE HOMEHELP KINNEAR MARITALNIRAMT

A list can be combined with individual variables

AGE SPAGE ClASS SPClASS All implies

AGE SPSAGE CLASS SPSCLASS CHRONILLEMPLOYED HEALTH HHLDTYPE HOMEHELP KINNEARMARITAL NIRAMT OCPENAMT REBTOWN REBTRENT GENDERTOTINCID UNEARNED CASWGT

The reserved keyword ALL in this context means all the variables in the active le

It can be used in a variable list for the KEEP subcommand on a GET or SAVE

command to change the order the variables are saved in whilst making sure no

variables are missed Variables not specically listed will appear after all the listed

ones in active le order

If new variables are created during a session they will be added to the active le in

the order they were created after the variables already stored in the active le You

can always check the variable order in the active le using the DISPlAY command -

it may not be the same order they will appear in dialog boxes

Syntax Charts

The Sow synta cart button as seen in the previous session in Figure 11 will let

you look at command syntax charts If you have no command selected in the syntax

window then the button will give you an alphabetic list of all the command keywords

Look at the syntax chart for each of these commands to see what subcommands and keywords are available

Use the Search button to look for more information about each

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2554

Module 5 SPSS command Structure

19

S e s s i onB S P S S C om

m an d S t r u c t ur e

Some SPSS Command examples (from slides)

Te GET coand

The GET command reads a sav le into SPSS

GET FILE = cardiacsav

Te GET coand - KEEP and DROP subcoands

Use the KEEP subcommand to select only some of the variables

GET FILE=cardiacsavKEEP=famhist chd

Use the DROP subcommand to select all of the le except certain

variables

GET FILE=cardiacsavDROP=height weight

Te DATASET NAmE coand

The DATASET NAME command give the active dataset a short name

which can be used in later commands

GET FILE = mspssworkcardiacsav

DATASET NAME cardiac

Te SAVE coand

Use the SAVE command to save your data in a sav le

SAVE OUTFILE=myfilesav

Te SAVE coand wi

save all your cases together with variable names values

missing values variable and value labels and formats

Saved in the state they are in when the command is issued

Any further changes can only be saved with another SAVE command

It will NOT save the results of any analyses results are saved in an

output window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2654

Module 5 Starting with SPSS Syntax

20

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Te SAVE coand KEEP and DROP subcoands

The KEEP and DROP subcommands are available with the SAVE

command and perform the same function as with GET

Using KEEP will save only the named variables

SAVE OUTFILE=filesav KEEP=AGE HEIGHT

Using DROP will save all variables except named ones (here it means

all variables except WEIGHT2)

SAVE OUTFILE=filesav DROP=WEIGHT2

FREQUENCIES coand

FREQUENCIES VARIABLES=HEIGHT

FREQUENCIES - STATISTICS subcoand

FREQ VAR=HEIGHT AGE STATISTICS

Gives four default statistics

MEAN STDDEV MINIMUM MAXIMUM

FREQUENCIES - more statistics

MEDIAN MODE SUM RANGE SEMEAN VARIANCESKEWNESS SESKEW KURTOSIS SEKURT

STATISTICS=DEFAULT

Use to get the default four statistics MEAN STDDEV MINIMUM

MAXIMUM together with others you ask for

STATISTICS=ALL

Gives all available statistics - can be wasteful

STATISTICS=NONE

Equivalent to omitting STATISTICS altogether

FREQUENCIES - FORmAT subcoand

To suppress the frequency table use

FORMAT=NOTABLE

To suppress tables over a certain length

FORMAT=LIMIT(50)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2754

Module 5 SPSS command Structure

21

S e s s i onB S P S S C om

m an d S t r u c t ur e

FREQUENCIES - subcoands for carts

There are three subcommands to produce type of charts named

HISTOGRAM

BARCHART

PIECHART

Using ore tan one subcoand

To use more than one subcommand string them together and nish

the command with the terminating full stop

FREQUENCIES VARIABLES=AGESEX STATISTICS=RANGEBARCHART

CROSSTABS coand

CROSSTABS TABLES=AGE BY SEX

CROSSTABS AGE BY SEX

SEX BY WEIGHT

The next three crosstabs commands produce the same three tables

CROSSTABS AGE BY SEX

AGE BY PARTY

AGE BY INCOME

CROSSTABS AGE BY SEX PARTY INCOMECROSSTABS AGE BY SEX TO INCOME

CROSSTABS - STATISTICS subcoand

CROSSTABS HEIGHT BY AGE

STATISTICS

Gives the Chi-square statistic tests

CROSSTABS - more statistics

Others available

CHISQ PHI CC LAMBDA UC BTAU CTAU GAMMA DETA CORR ALL NONE

and KAPPA RISK MCNEMAR CMH(1)

Usage

CROSSTABS TABLES=AGE BY WEIGHT

STATISTICS=BTAU CORR

Only gives those statistics asked for

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 17: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1754

Module 5 Starting with SPSS Syntax

11

S e s s i onA U s i n g S P S

S C omm an d s

Figure 14 Start of Output from the CROSSTABS command

The CROSSTABS command has several subcommands try the syntax chart button

to see what they are They include

CEllS used to change the content of the table

BARChART to add a clustered bar chart for each layer

Figure 15 CROSSTABS using CEllS and BARChART subcommands

Try typing in and running the new command in Figure 15

Layers can be added to the table by adding another BY keyword follwed by the layer

variable or variable list For example

c r o s s t a b s h a p p y b y l i f e b y r e g i o n

will produce a crosstabs table layer for each region Try adding a layer to the latest

crosstabs command and run the command to check the effect

Some command names such as FINISh BEGIN DATA and

END DATA are complete commands which need no further

specication However most commands (including all of the statistical subprograms) have to have a certain amount

of specication and each has its own particular form of

specication so check the syntax guide before you run

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1854

Module 5 Starting with SPSS Syntax

12

S e s s i o n A U

s i n g S P S S C o m m a n d s Slide Content from presentation

SPSS Coand language

Starting with commands

Wy use Coands

More exible

Extra features

Can be saved

And repeated

Te Synta Window

SPSS commands are entered in the Syntax Window

Create a Syntax Window

Either selecting File gt New gt Syntax from the menu bar

Or creating le with sps extension in a text editor

Use an existing syntax window

Either Selecting an open syntax window from the Window menu

Or selecting File gt Open gt Syntax from the menu bar

SPSS coands

Commands are entered in the Syntax Window by

Either opening the syntax Window and typing directly

Or building up a command using dialog boxes then clicking the

PASTE button

Or using a mixture of the two

Forat of Coands

Control Field - consists of SPSS keyword

Specication Field - names of variables etc on which command willoperate

Full stop - to terminate command

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1954

Module 5 Starting with SPSS Syntax

13

S e s s i onA U s i n g S P S

S C omm an d s

Synta Rues

Commands start with a command keyword

Commands are case insensitive

Except for text in quotes

A single command can go over many syntax lines

Each line of syntax must not exceed 256 bytes3

Do not break a syntax line

in a variable name

in between quotes lsquo lsquo or ldquo ldquo

For a syntax window

Each command must start on a new line

Each command must end with a full stop with no non-blanks after it

or end with a completely blank line afterwards

For batc coands (see ast session)

Each command begins on a new line in column 1

unless you use + or -

Continuation lines mustnrsquot start in column 1

Full stops command terminators are optional

Te FINISh coand

FINISH

For batch commands

Terminates a le of SPSS commands and closes SPSS tidily

NB When SPSS windows manager is running (eg during an SPSS

for Windows session) FINISH will generate an error

Terminate an interactive SPSS session using Exit from the File menu

3 256 bytes are characters in single byte languageshellip

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2054

Module 5 Starting with SPSS Syntax

14

S e s s i o n A U

s i n g S P S S C o m m a n d s

Session summaryIn this session we covered

How to display SPSS commands in output window as they are runbull

How using thebull Paste button in an SPSS dialog box pastes the underlying

command into a synta window

The Syntax Window where SPSS commands (also calledbull synta) are stored

Using the syntax chart button to get help about a command in the syntax windowbull

How to type in and run an SPSS command from thebull synta window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2154

Session B

SPSS CommandStructure

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2254

Module 5 Starting with SPSS Syntax

16

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Commands Subcommands amp keywords

In general an SPSS command will be a mixture of different elements like this -

v kyw kyw kyw

An SPSS command starts with a recognized command keyword which has to

be spelt correctly Each command has its own general format with its own set of

subcommands and keywords These are combined with lists of variables names

or combinations of variable names and keywords to specify the analysis or action

required by the computer

Subcommands and other keywords are words recognized by SPSS as part of

the commands specication and so appear in the specication eld Some

SPSS procedures have subcommands which can appear either on their own or in

association with certain keywords As with commands subcommands and keywords

have to appear exactly as they are spelt

Figure 17 Different keywords in the Commands example

get FILE=mspssworkCARDIACsav

crosstabs chd B Y famhist CELLS=C O U N T R O W C O L U M N STATISTICS=C H I S Q

requencies VARIABLES=height cigaret

FORMAT=L I M I T (12 ) HIST

graPH SCATTERPLOT weight W I T H height

inisH

The different types of keyword have been highlighted as follows-

command keywords SUBCOMMAND keywords

O T H E R keywords variable names and other specication

Abbreviating keywords

SPSS will recognise most of its command and subcommand keywords when

abbreviated to the rst three or four characters of the name as long as the keywordsare unique So for example-

freq vars=height cigaret form=lim(12) hist

can be used as an abbreviation of-

frequencies variables=height cigaret

format=limit(12)

histogram

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2354

Module 5 SPSS command Structure

17

S e s s i onB S P S S C om

m an d S t r u c t ur e

Reserved keywords

With most procedures it does not matter if there is a variable name the same as a

keyword since there are certain places in a command specication where a variable

or variable list is expected and others where keywords are expected However there

are some reserved keywords which cannot be used as names These are

ALL AND BY EQ GE GT LE LT NE NOT OR TO WITH

These words cannot be used as variable names in SPSS because the program

already recognizes them as having meaning For example

FREQUENCIES VARIABLES=ALL

calculates a frequency table for all the variables currently stored on SPSSs

active le To have a variable called A L L would make the sense of the instruction

ambiguous to SPSS

Lists of Variables

The variables are kept in the SPSS active le in the order they are given in the

DATA lIST command and any variables generated during the session in the order

they were created This order can be utilized when instead of typing a long list of

consecutive variables you type the name of the rst variable then the TO keyword

and then the name of the last variable in the list These inclusive ranges of variables

can be combined with the names of other variables to produce a larger variable list

where needed For example if we had the following variables in a system le-

LIST OF VARIABLES ON THE ACTIVE FILE

1 AGE 6 HHLDTYPE 11 OCPENAMT 16 SPSCLASS

2 CHRONILL 7 HOMEHELP 12 REBTOWN 17 TOTINCID

3 CLASS 8 KINNEAR 13 REBTRENT 18 UNEARNED

4 EMPLOYED 9 MARITAL 14 GENDER 19 CASWGT

5 HEALTH 10 NIRAMT 15 SPSAGE

In SPSS variable lists individual variable names andor variable ranges can

be separated by either spaces or commas However any keywords have to be

separated from variable names by spaces ie use spaces with TO or All

AGE TO CASWGT implies

AGE CHRONILL CLASS EMPLOYED HEALTH HHLDTYPEHOMEHELP KINNEAR MARITAL NIRAMT OCPENAMT REBTOWNREBTRENT GENDER SPSAGE SPSCLASS TOTINCID UNEARNEDCASWGT

This rst list is all the variables in the active le since AGE is the rst variable on the

list and CASWGT is the last

hhlDTYPE TO GENDER implies

HHLDTYPE HOMEHELP KINNEAR MARITAL NIRAMT OCPENAMTREBTOWN REBTRENT GENDER

Notice in this list 7 of the 9 variables are implied in the same order as the active le

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2454

Module 5 Starting with SPSS Syntax

18

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e ClASS hEAlTh TO NIRAmT implies

CLASS HEALTH HHLDTYPE HOMEHELP KINNEAR MARITALNIRAMT

A list can be combined with individual variables

AGE SPAGE ClASS SPClASS All implies

AGE SPSAGE CLASS SPSCLASS CHRONILLEMPLOYED HEALTH HHLDTYPE HOMEHELP KINNEARMARITAL NIRAMT OCPENAMT REBTOWN REBTRENT GENDERTOTINCID UNEARNED CASWGT

The reserved keyword ALL in this context means all the variables in the active le

It can be used in a variable list for the KEEP subcommand on a GET or SAVE

command to change the order the variables are saved in whilst making sure no

variables are missed Variables not specically listed will appear after all the listed

ones in active le order

If new variables are created during a session they will be added to the active le in

the order they were created after the variables already stored in the active le You

can always check the variable order in the active le using the DISPlAY command -

it may not be the same order they will appear in dialog boxes

Syntax Charts

The Sow synta cart button as seen in the previous session in Figure 11 will let

you look at command syntax charts If you have no command selected in the syntax

window then the button will give you an alphabetic list of all the command keywords

Look at the syntax chart for each of these commands to see what subcommands and keywords are available

Use the Search button to look for more information about each

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2554

Module 5 SPSS command Structure

19

S e s s i onB S P S S C om

m an d S t r u c t ur e

Some SPSS Command examples (from slides)

Te GET coand

The GET command reads a sav le into SPSS

GET FILE = cardiacsav

Te GET coand - KEEP and DROP subcoands

Use the KEEP subcommand to select only some of the variables

GET FILE=cardiacsavKEEP=famhist chd

Use the DROP subcommand to select all of the le except certain

variables

GET FILE=cardiacsavDROP=height weight

Te DATASET NAmE coand

The DATASET NAME command give the active dataset a short name

which can be used in later commands

GET FILE = mspssworkcardiacsav

DATASET NAME cardiac

Te SAVE coand

Use the SAVE command to save your data in a sav le

SAVE OUTFILE=myfilesav

Te SAVE coand wi

save all your cases together with variable names values

missing values variable and value labels and formats

Saved in the state they are in when the command is issued

Any further changes can only be saved with another SAVE command

It will NOT save the results of any analyses results are saved in an

output window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2654

Module 5 Starting with SPSS Syntax

20

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Te SAVE coand KEEP and DROP subcoands

The KEEP and DROP subcommands are available with the SAVE

command and perform the same function as with GET

Using KEEP will save only the named variables

SAVE OUTFILE=filesav KEEP=AGE HEIGHT

Using DROP will save all variables except named ones (here it means

all variables except WEIGHT2)

SAVE OUTFILE=filesav DROP=WEIGHT2

FREQUENCIES coand

FREQUENCIES VARIABLES=HEIGHT

FREQUENCIES - STATISTICS subcoand

FREQ VAR=HEIGHT AGE STATISTICS

Gives four default statistics

MEAN STDDEV MINIMUM MAXIMUM

FREQUENCIES - more statistics

MEDIAN MODE SUM RANGE SEMEAN VARIANCESKEWNESS SESKEW KURTOSIS SEKURT

STATISTICS=DEFAULT

Use to get the default four statistics MEAN STDDEV MINIMUM

MAXIMUM together with others you ask for

STATISTICS=ALL

Gives all available statistics - can be wasteful

STATISTICS=NONE

Equivalent to omitting STATISTICS altogether

FREQUENCIES - FORmAT subcoand

To suppress the frequency table use

FORMAT=NOTABLE

To suppress tables over a certain length

FORMAT=LIMIT(50)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2754

Module 5 SPSS command Structure

21

S e s s i onB S P S S C om

m an d S t r u c t ur e

FREQUENCIES - subcoands for carts

There are three subcommands to produce type of charts named

HISTOGRAM

BARCHART

PIECHART

Using ore tan one subcoand

To use more than one subcommand string them together and nish

the command with the terminating full stop

FREQUENCIES VARIABLES=AGESEX STATISTICS=RANGEBARCHART

CROSSTABS coand

CROSSTABS TABLES=AGE BY SEX

CROSSTABS AGE BY SEX

SEX BY WEIGHT

The next three crosstabs commands produce the same three tables

CROSSTABS AGE BY SEX

AGE BY PARTY

AGE BY INCOME

CROSSTABS AGE BY SEX PARTY INCOMECROSSTABS AGE BY SEX TO INCOME

CROSSTABS - STATISTICS subcoand

CROSSTABS HEIGHT BY AGE

STATISTICS

Gives the Chi-square statistic tests

CROSSTABS - more statistics

Others available

CHISQ PHI CC LAMBDA UC BTAU CTAU GAMMA DETA CORR ALL NONE

and KAPPA RISK MCNEMAR CMH(1)

Usage

CROSSTABS TABLES=AGE BY WEIGHT

STATISTICS=BTAU CORR

Only gives those statistics asked for

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 18: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1854

Module 5 Starting with SPSS Syntax

12

S e s s i o n A U

s i n g S P S S C o m m a n d s Slide Content from presentation

SPSS Coand language

Starting with commands

Wy use Coands

More exible

Extra features

Can be saved

And repeated

Te Synta Window

SPSS commands are entered in the Syntax Window

Create a Syntax Window

Either selecting File gt New gt Syntax from the menu bar

Or creating le with sps extension in a text editor

Use an existing syntax window

Either Selecting an open syntax window from the Window menu

Or selecting File gt Open gt Syntax from the menu bar

SPSS coands

Commands are entered in the Syntax Window by

Either opening the syntax Window and typing directly

Or building up a command using dialog boxes then clicking the

PASTE button

Or using a mixture of the two

Forat of Coands

Control Field - consists of SPSS keyword

Specication Field - names of variables etc on which command willoperate

Full stop - to terminate command

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1954

Module 5 Starting with SPSS Syntax

13

S e s s i onA U s i n g S P S

S C omm an d s

Synta Rues

Commands start with a command keyword

Commands are case insensitive

Except for text in quotes

A single command can go over many syntax lines

Each line of syntax must not exceed 256 bytes3

Do not break a syntax line

in a variable name

in between quotes lsquo lsquo or ldquo ldquo

For a syntax window

Each command must start on a new line

Each command must end with a full stop with no non-blanks after it

or end with a completely blank line afterwards

For batc coands (see ast session)

Each command begins on a new line in column 1

unless you use + or -

Continuation lines mustnrsquot start in column 1

Full stops command terminators are optional

Te FINISh coand

FINISH

For batch commands

Terminates a le of SPSS commands and closes SPSS tidily

NB When SPSS windows manager is running (eg during an SPSS

for Windows session) FINISH will generate an error

Terminate an interactive SPSS session using Exit from the File menu

3 256 bytes are characters in single byte languageshellip

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2054

Module 5 Starting with SPSS Syntax

14

S e s s i o n A U

s i n g S P S S C o m m a n d s

Session summaryIn this session we covered

How to display SPSS commands in output window as they are runbull

How using thebull Paste button in an SPSS dialog box pastes the underlying

command into a synta window

The Syntax Window where SPSS commands (also calledbull synta) are stored

Using the syntax chart button to get help about a command in the syntax windowbull

How to type in and run an SPSS command from thebull synta window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2154

Session B

SPSS CommandStructure

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2254

Module 5 Starting with SPSS Syntax

16

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Commands Subcommands amp keywords

In general an SPSS command will be a mixture of different elements like this -

v kyw kyw kyw

An SPSS command starts with a recognized command keyword which has to

be spelt correctly Each command has its own general format with its own set of

subcommands and keywords These are combined with lists of variables names

or combinations of variable names and keywords to specify the analysis or action

required by the computer

Subcommands and other keywords are words recognized by SPSS as part of

the commands specication and so appear in the specication eld Some

SPSS procedures have subcommands which can appear either on their own or in

association with certain keywords As with commands subcommands and keywords

have to appear exactly as they are spelt

Figure 17 Different keywords in the Commands example

get FILE=mspssworkCARDIACsav

crosstabs chd B Y famhist CELLS=C O U N T R O W C O L U M N STATISTICS=C H I S Q

requencies VARIABLES=height cigaret

FORMAT=L I M I T (12 ) HIST

graPH SCATTERPLOT weight W I T H height

inisH

The different types of keyword have been highlighted as follows-

command keywords SUBCOMMAND keywords

O T H E R keywords variable names and other specication

Abbreviating keywords

SPSS will recognise most of its command and subcommand keywords when

abbreviated to the rst three or four characters of the name as long as the keywordsare unique So for example-

freq vars=height cigaret form=lim(12) hist

can be used as an abbreviation of-

frequencies variables=height cigaret

format=limit(12)

histogram

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2354

Module 5 SPSS command Structure

17

S e s s i onB S P S S C om

m an d S t r u c t ur e

Reserved keywords

With most procedures it does not matter if there is a variable name the same as a

keyword since there are certain places in a command specication where a variable

or variable list is expected and others where keywords are expected However there

are some reserved keywords which cannot be used as names These are

ALL AND BY EQ GE GT LE LT NE NOT OR TO WITH

These words cannot be used as variable names in SPSS because the program

already recognizes them as having meaning For example

FREQUENCIES VARIABLES=ALL

calculates a frequency table for all the variables currently stored on SPSSs

active le To have a variable called A L L would make the sense of the instruction

ambiguous to SPSS

Lists of Variables

The variables are kept in the SPSS active le in the order they are given in the

DATA lIST command and any variables generated during the session in the order

they were created This order can be utilized when instead of typing a long list of

consecutive variables you type the name of the rst variable then the TO keyword

and then the name of the last variable in the list These inclusive ranges of variables

can be combined with the names of other variables to produce a larger variable list

where needed For example if we had the following variables in a system le-

LIST OF VARIABLES ON THE ACTIVE FILE

1 AGE 6 HHLDTYPE 11 OCPENAMT 16 SPSCLASS

2 CHRONILL 7 HOMEHELP 12 REBTOWN 17 TOTINCID

3 CLASS 8 KINNEAR 13 REBTRENT 18 UNEARNED

4 EMPLOYED 9 MARITAL 14 GENDER 19 CASWGT

5 HEALTH 10 NIRAMT 15 SPSAGE

In SPSS variable lists individual variable names andor variable ranges can

be separated by either spaces or commas However any keywords have to be

separated from variable names by spaces ie use spaces with TO or All

AGE TO CASWGT implies

AGE CHRONILL CLASS EMPLOYED HEALTH HHLDTYPEHOMEHELP KINNEAR MARITAL NIRAMT OCPENAMT REBTOWNREBTRENT GENDER SPSAGE SPSCLASS TOTINCID UNEARNEDCASWGT

This rst list is all the variables in the active le since AGE is the rst variable on the

list and CASWGT is the last

hhlDTYPE TO GENDER implies

HHLDTYPE HOMEHELP KINNEAR MARITAL NIRAMT OCPENAMTREBTOWN REBTRENT GENDER

Notice in this list 7 of the 9 variables are implied in the same order as the active le

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2454

Module 5 Starting with SPSS Syntax

18

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e ClASS hEAlTh TO NIRAmT implies

CLASS HEALTH HHLDTYPE HOMEHELP KINNEAR MARITALNIRAMT

A list can be combined with individual variables

AGE SPAGE ClASS SPClASS All implies

AGE SPSAGE CLASS SPSCLASS CHRONILLEMPLOYED HEALTH HHLDTYPE HOMEHELP KINNEARMARITAL NIRAMT OCPENAMT REBTOWN REBTRENT GENDERTOTINCID UNEARNED CASWGT

The reserved keyword ALL in this context means all the variables in the active le

It can be used in a variable list for the KEEP subcommand on a GET or SAVE

command to change the order the variables are saved in whilst making sure no

variables are missed Variables not specically listed will appear after all the listed

ones in active le order

If new variables are created during a session they will be added to the active le in

the order they were created after the variables already stored in the active le You

can always check the variable order in the active le using the DISPlAY command -

it may not be the same order they will appear in dialog boxes

Syntax Charts

The Sow synta cart button as seen in the previous session in Figure 11 will let

you look at command syntax charts If you have no command selected in the syntax

window then the button will give you an alphabetic list of all the command keywords

Look at the syntax chart for each of these commands to see what subcommands and keywords are available

Use the Search button to look for more information about each

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2554

Module 5 SPSS command Structure

19

S e s s i onB S P S S C om

m an d S t r u c t ur e

Some SPSS Command examples (from slides)

Te GET coand

The GET command reads a sav le into SPSS

GET FILE = cardiacsav

Te GET coand - KEEP and DROP subcoands

Use the KEEP subcommand to select only some of the variables

GET FILE=cardiacsavKEEP=famhist chd

Use the DROP subcommand to select all of the le except certain

variables

GET FILE=cardiacsavDROP=height weight

Te DATASET NAmE coand

The DATASET NAME command give the active dataset a short name

which can be used in later commands

GET FILE = mspssworkcardiacsav

DATASET NAME cardiac

Te SAVE coand

Use the SAVE command to save your data in a sav le

SAVE OUTFILE=myfilesav

Te SAVE coand wi

save all your cases together with variable names values

missing values variable and value labels and formats

Saved in the state they are in when the command is issued

Any further changes can only be saved with another SAVE command

It will NOT save the results of any analyses results are saved in an

output window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2654

Module 5 Starting with SPSS Syntax

20

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Te SAVE coand KEEP and DROP subcoands

The KEEP and DROP subcommands are available with the SAVE

command and perform the same function as with GET

Using KEEP will save only the named variables

SAVE OUTFILE=filesav KEEP=AGE HEIGHT

Using DROP will save all variables except named ones (here it means

all variables except WEIGHT2)

SAVE OUTFILE=filesav DROP=WEIGHT2

FREQUENCIES coand

FREQUENCIES VARIABLES=HEIGHT

FREQUENCIES - STATISTICS subcoand

FREQ VAR=HEIGHT AGE STATISTICS

Gives four default statistics

MEAN STDDEV MINIMUM MAXIMUM

FREQUENCIES - more statistics

MEDIAN MODE SUM RANGE SEMEAN VARIANCESKEWNESS SESKEW KURTOSIS SEKURT

STATISTICS=DEFAULT

Use to get the default four statistics MEAN STDDEV MINIMUM

MAXIMUM together with others you ask for

STATISTICS=ALL

Gives all available statistics - can be wasteful

STATISTICS=NONE

Equivalent to omitting STATISTICS altogether

FREQUENCIES - FORmAT subcoand

To suppress the frequency table use

FORMAT=NOTABLE

To suppress tables over a certain length

FORMAT=LIMIT(50)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2754

Module 5 SPSS command Structure

21

S e s s i onB S P S S C om

m an d S t r u c t ur e

FREQUENCIES - subcoands for carts

There are three subcommands to produce type of charts named

HISTOGRAM

BARCHART

PIECHART

Using ore tan one subcoand

To use more than one subcommand string them together and nish

the command with the terminating full stop

FREQUENCIES VARIABLES=AGESEX STATISTICS=RANGEBARCHART

CROSSTABS coand

CROSSTABS TABLES=AGE BY SEX

CROSSTABS AGE BY SEX

SEX BY WEIGHT

The next three crosstabs commands produce the same three tables

CROSSTABS AGE BY SEX

AGE BY PARTY

AGE BY INCOME

CROSSTABS AGE BY SEX PARTY INCOMECROSSTABS AGE BY SEX TO INCOME

CROSSTABS - STATISTICS subcoand

CROSSTABS HEIGHT BY AGE

STATISTICS

Gives the Chi-square statistic tests

CROSSTABS - more statistics

Others available

CHISQ PHI CC LAMBDA UC BTAU CTAU GAMMA DETA CORR ALL NONE

and KAPPA RISK MCNEMAR CMH(1)

Usage

CROSSTABS TABLES=AGE BY WEIGHT

STATISTICS=BTAU CORR

Only gives those statistics asked for

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 19: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 1954

Module 5 Starting with SPSS Syntax

13

S e s s i onA U s i n g S P S

S C omm an d s

Synta Rues

Commands start with a command keyword

Commands are case insensitive

Except for text in quotes

A single command can go over many syntax lines

Each line of syntax must not exceed 256 bytes3

Do not break a syntax line

in a variable name

in between quotes lsquo lsquo or ldquo ldquo

For a syntax window

Each command must start on a new line

Each command must end with a full stop with no non-blanks after it

or end with a completely blank line afterwards

For batc coands (see ast session)

Each command begins on a new line in column 1

unless you use + or -

Continuation lines mustnrsquot start in column 1

Full stops command terminators are optional

Te FINISh coand

FINISH

For batch commands

Terminates a le of SPSS commands and closes SPSS tidily

NB When SPSS windows manager is running (eg during an SPSS

for Windows session) FINISH will generate an error

Terminate an interactive SPSS session using Exit from the File menu

3 256 bytes are characters in single byte languageshellip

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2054

Module 5 Starting with SPSS Syntax

14

S e s s i o n A U

s i n g S P S S C o m m a n d s

Session summaryIn this session we covered

How to display SPSS commands in output window as they are runbull

How using thebull Paste button in an SPSS dialog box pastes the underlying

command into a synta window

The Syntax Window where SPSS commands (also calledbull synta) are stored

Using the syntax chart button to get help about a command in the syntax windowbull

How to type in and run an SPSS command from thebull synta window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2154

Session B

SPSS CommandStructure

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2254

Module 5 Starting with SPSS Syntax

16

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Commands Subcommands amp keywords

In general an SPSS command will be a mixture of different elements like this -

v kyw kyw kyw

An SPSS command starts with a recognized command keyword which has to

be spelt correctly Each command has its own general format with its own set of

subcommands and keywords These are combined with lists of variables names

or combinations of variable names and keywords to specify the analysis or action

required by the computer

Subcommands and other keywords are words recognized by SPSS as part of

the commands specication and so appear in the specication eld Some

SPSS procedures have subcommands which can appear either on their own or in

association with certain keywords As with commands subcommands and keywords

have to appear exactly as they are spelt

Figure 17 Different keywords in the Commands example

get FILE=mspssworkCARDIACsav

crosstabs chd B Y famhist CELLS=C O U N T R O W C O L U M N STATISTICS=C H I S Q

requencies VARIABLES=height cigaret

FORMAT=L I M I T (12 ) HIST

graPH SCATTERPLOT weight W I T H height

inisH

The different types of keyword have been highlighted as follows-

command keywords SUBCOMMAND keywords

O T H E R keywords variable names and other specication

Abbreviating keywords

SPSS will recognise most of its command and subcommand keywords when

abbreviated to the rst three or four characters of the name as long as the keywordsare unique So for example-

freq vars=height cigaret form=lim(12) hist

can be used as an abbreviation of-

frequencies variables=height cigaret

format=limit(12)

histogram

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2354

Module 5 SPSS command Structure

17

S e s s i onB S P S S C om

m an d S t r u c t ur e

Reserved keywords

With most procedures it does not matter if there is a variable name the same as a

keyword since there are certain places in a command specication where a variable

or variable list is expected and others where keywords are expected However there

are some reserved keywords which cannot be used as names These are

ALL AND BY EQ GE GT LE LT NE NOT OR TO WITH

These words cannot be used as variable names in SPSS because the program

already recognizes them as having meaning For example

FREQUENCIES VARIABLES=ALL

calculates a frequency table for all the variables currently stored on SPSSs

active le To have a variable called A L L would make the sense of the instruction

ambiguous to SPSS

Lists of Variables

The variables are kept in the SPSS active le in the order they are given in the

DATA lIST command and any variables generated during the session in the order

they were created This order can be utilized when instead of typing a long list of

consecutive variables you type the name of the rst variable then the TO keyword

and then the name of the last variable in the list These inclusive ranges of variables

can be combined with the names of other variables to produce a larger variable list

where needed For example if we had the following variables in a system le-

LIST OF VARIABLES ON THE ACTIVE FILE

1 AGE 6 HHLDTYPE 11 OCPENAMT 16 SPSCLASS

2 CHRONILL 7 HOMEHELP 12 REBTOWN 17 TOTINCID

3 CLASS 8 KINNEAR 13 REBTRENT 18 UNEARNED

4 EMPLOYED 9 MARITAL 14 GENDER 19 CASWGT

5 HEALTH 10 NIRAMT 15 SPSAGE

In SPSS variable lists individual variable names andor variable ranges can

be separated by either spaces or commas However any keywords have to be

separated from variable names by spaces ie use spaces with TO or All

AGE TO CASWGT implies

AGE CHRONILL CLASS EMPLOYED HEALTH HHLDTYPEHOMEHELP KINNEAR MARITAL NIRAMT OCPENAMT REBTOWNREBTRENT GENDER SPSAGE SPSCLASS TOTINCID UNEARNEDCASWGT

This rst list is all the variables in the active le since AGE is the rst variable on the

list and CASWGT is the last

hhlDTYPE TO GENDER implies

HHLDTYPE HOMEHELP KINNEAR MARITAL NIRAMT OCPENAMTREBTOWN REBTRENT GENDER

Notice in this list 7 of the 9 variables are implied in the same order as the active le

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2454

Module 5 Starting with SPSS Syntax

18

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e ClASS hEAlTh TO NIRAmT implies

CLASS HEALTH HHLDTYPE HOMEHELP KINNEAR MARITALNIRAMT

A list can be combined with individual variables

AGE SPAGE ClASS SPClASS All implies

AGE SPSAGE CLASS SPSCLASS CHRONILLEMPLOYED HEALTH HHLDTYPE HOMEHELP KINNEARMARITAL NIRAMT OCPENAMT REBTOWN REBTRENT GENDERTOTINCID UNEARNED CASWGT

The reserved keyword ALL in this context means all the variables in the active le

It can be used in a variable list for the KEEP subcommand on a GET or SAVE

command to change the order the variables are saved in whilst making sure no

variables are missed Variables not specically listed will appear after all the listed

ones in active le order

If new variables are created during a session they will be added to the active le in

the order they were created after the variables already stored in the active le You

can always check the variable order in the active le using the DISPlAY command -

it may not be the same order they will appear in dialog boxes

Syntax Charts

The Sow synta cart button as seen in the previous session in Figure 11 will let

you look at command syntax charts If you have no command selected in the syntax

window then the button will give you an alphabetic list of all the command keywords

Look at the syntax chart for each of these commands to see what subcommands and keywords are available

Use the Search button to look for more information about each

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2554

Module 5 SPSS command Structure

19

S e s s i onB S P S S C om

m an d S t r u c t ur e

Some SPSS Command examples (from slides)

Te GET coand

The GET command reads a sav le into SPSS

GET FILE = cardiacsav

Te GET coand - KEEP and DROP subcoands

Use the KEEP subcommand to select only some of the variables

GET FILE=cardiacsavKEEP=famhist chd

Use the DROP subcommand to select all of the le except certain

variables

GET FILE=cardiacsavDROP=height weight

Te DATASET NAmE coand

The DATASET NAME command give the active dataset a short name

which can be used in later commands

GET FILE = mspssworkcardiacsav

DATASET NAME cardiac

Te SAVE coand

Use the SAVE command to save your data in a sav le

SAVE OUTFILE=myfilesav

Te SAVE coand wi

save all your cases together with variable names values

missing values variable and value labels and formats

Saved in the state they are in when the command is issued

Any further changes can only be saved with another SAVE command

It will NOT save the results of any analyses results are saved in an

output window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2654

Module 5 Starting with SPSS Syntax

20

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Te SAVE coand KEEP and DROP subcoands

The KEEP and DROP subcommands are available with the SAVE

command and perform the same function as with GET

Using KEEP will save only the named variables

SAVE OUTFILE=filesav KEEP=AGE HEIGHT

Using DROP will save all variables except named ones (here it means

all variables except WEIGHT2)

SAVE OUTFILE=filesav DROP=WEIGHT2

FREQUENCIES coand

FREQUENCIES VARIABLES=HEIGHT

FREQUENCIES - STATISTICS subcoand

FREQ VAR=HEIGHT AGE STATISTICS

Gives four default statistics

MEAN STDDEV MINIMUM MAXIMUM

FREQUENCIES - more statistics

MEDIAN MODE SUM RANGE SEMEAN VARIANCESKEWNESS SESKEW KURTOSIS SEKURT

STATISTICS=DEFAULT

Use to get the default four statistics MEAN STDDEV MINIMUM

MAXIMUM together with others you ask for

STATISTICS=ALL

Gives all available statistics - can be wasteful

STATISTICS=NONE

Equivalent to omitting STATISTICS altogether

FREQUENCIES - FORmAT subcoand

To suppress the frequency table use

FORMAT=NOTABLE

To suppress tables over a certain length

FORMAT=LIMIT(50)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2754

Module 5 SPSS command Structure

21

S e s s i onB S P S S C om

m an d S t r u c t ur e

FREQUENCIES - subcoands for carts

There are three subcommands to produce type of charts named

HISTOGRAM

BARCHART

PIECHART

Using ore tan one subcoand

To use more than one subcommand string them together and nish

the command with the terminating full stop

FREQUENCIES VARIABLES=AGESEX STATISTICS=RANGEBARCHART

CROSSTABS coand

CROSSTABS TABLES=AGE BY SEX

CROSSTABS AGE BY SEX

SEX BY WEIGHT

The next three crosstabs commands produce the same three tables

CROSSTABS AGE BY SEX

AGE BY PARTY

AGE BY INCOME

CROSSTABS AGE BY SEX PARTY INCOMECROSSTABS AGE BY SEX TO INCOME

CROSSTABS - STATISTICS subcoand

CROSSTABS HEIGHT BY AGE

STATISTICS

Gives the Chi-square statistic tests

CROSSTABS - more statistics

Others available

CHISQ PHI CC LAMBDA UC BTAU CTAU GAMMA DETA CORR ALL NONE

and KAPPA RISK MCNEMAR CMH(1)

Usage

CROSSTABS TABLES=AGE BY WEIGHT

STATISTICS=BTAU CORR

Only gives those statistics asked for

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 20: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2054

Module 5 Starting with SPSS Syntax

14

S e s s i o n A U

s i n g S P S S C o m m a n d s

Session summaryIn this session we covered

How to display SPSS commands in output window as they are runbull

How using thebull Paste button in an SPSS dialog box pastes the underlying

command into a synta window

The Syntax Window where SPSS commands (also calledbull synta) are stored

Using the syntax chart button to get help about a command in the syntax windowbull

How to type in and run an SPSS command from thebull synta window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2154

Session B

SPSS CommandStructure

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2254

Module 5 Starting with SPSS Syntax

16

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Commands Subcommands amp keywords

In general an SPSS command will be a mixture of different elements like this -

v kyw kyw kyw

An SPSS command starts with a recognized command keyword which has to

be spelt correctly Each command has its own general format with its own set of

subcommands and keywords These are combined with lists of variables names

or combinations of variable names and keywords to specify the analysis or action

required by the computer

Subcommands and other keywords are words recognized by SPSS as part of

the commands specication and so appear in the specication eld Some

SPSS procedures have subcommands which can appear either on their own or in

association with certain keywords As with commands subcommands and keywords

have to appear exactly as they are spelt

Figure 17 Different keywords in the Commands example

get FILE=mspssworkCARDIACsav

crosstabs chd B Y famhist CELLS=C O U N T R O W C O L U M N STATISTICS=C H I S Q

requencies VARIABLES=height cigaret

FORMAT=L I M I T (12 ) HIST

graPH SCATTERPLOT weight W I T H height

inisH

The different types of keyword have been highlighted as follows-

command keywords SUBCOMMAND keywords

O T H E R keywords variable names and other specication

Abbreviating keywords

SPSS will recognise most of its command and subcommand keywords when

abbreviated to the rst three or four characters of the name as long as the keywordsare unique So for example-

freq vars=height cigaret form=lim(12) hist

can be used as an abbreviation of-

frequencies variables=height cigaret

format=limit(12)

histogram

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2354

Module 5 SPSS command Structure

17

S e s s i onB S P S S C om

m an d S t r u c t ur e

Reserved keywords

With most procedures it does not matter if there is a variable name the same as a

keyword since there are certain places in a command specication where a variable

or variable list is expected and others where keywords are expected However there

are some reserved keywords which cannot be used as names These are

ALL AND BY EQ GE GT LE LT NE NOT OR TO WITH

These words cannot be used as variable names in SPSS because the program

already recognizes them as having meaning For example

FREQUENCIES VARIABLES=ALL

calculates a frequency table for all the variables currently stored on SPSSs

active le To have a variable called A L L would make the sense of the instruction

ambiguous to SPSS

Lists of Variables

The variables are kept in the SPSS active le in the order they are given in the

DATA lIST command and any variables generated during the session in the order

they were created This order can be utilized when instead of typing a long list of

consecutive variables you type the name of the rst variable then the TO keyword

and then the name of the last variable in the list These inclusive ranges of variables

can be combined with the names of other variables to produce a larger variable list

where needed For example if we had the following variables in a system le-

LIST OF VARIABLES ON THE ACTIVE FILE

1 AGE 6 HHLDTYPE 11 OCPENAMT 16 SPSCLASS

2 CHRONILL 7 HOMEHELP 12 REBTOWN 17 TOTINCID

3 CLASS 8 KINNEAR 13 REBTRENT 18 UNEARNED

4 EMPLOYED 9 MARITAL 14 GENDER 19 CASWGT

5 HEALTH 10 NIRAMT 15 SPSAGE

In SPSS variable lists individual variable names andor variable ranges can

be separated by either spaces or commas However any keywords have to be

separated from variable names by spaces ie use spaces with TO or All

AGE TO CASWGT implies

AGE CHRONILL CLASS EMPLOYED HEALTH HHLDTYPEHOMEHELP KINNEAR MARITAL NIRAMT OCPENAMT REBTOWNREBTRENT GENDER SPSAGE SPSCLASS TOTINCID UNEARNEDCASWGT

This rst list is all the variables in the active le since AGE is the rst variable on the

list and CASWGT is the last

hhlDTYPE TO GENDER implies

HHLDTYPE HOMEHELP KINNEAR MARITAL NIRAMT OCPENAMTREBTOWN REBTRENT GENDER

Notice in this list 7 of the 9 variables are implied in the same order as the active le

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2454

Module 5 Starting with SPSS Syntax

18

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e ClASS hEAlTh TO NIRAmT implies

CLASS HEALTH HHLDTYPE HOMEHELP KINNEAR MARITALNIRAMT

A list can be combined with individual variables

AGE SPAGE ClASS SPClASS All implies

AGE SPSAGE CLASS SPSCLASS CHRONILLEMPLOYED HEALTH HHLDTYPE HOMEHELP KINNEARMARITAL NIRAMT OCPENAMT REBTOWN REBTRENT GENDERTOTINCID UNEARNED CASWGT

The reserved keyword ALL in this context means all the variables in the active le

It can be used in a variable list for the KEEP subcommand on a GET or SAVE

command to change the order the variables are saved in whilst making sure no

variables are missed Variables not specically listed will appear after all the listed

ones in active le order

If new variables are created during a session they will be added to the active le in

the order they were created after the variables already stored in the active le You

can always check the variable order in the active le using the DISPlAY command -

it may not be the same order they will appear in dialog boxes

Syntax Charts

The Sow synta cart button as seen in the previous session in Figure 11 will let

you look at command syntax charts If you have no command selected in the syntax

window then the button will give you an alphabetic list of all the command keywords

Look at the syntax chart for each of these commands to see what subcommands and keywords are available

Use the Search button to look for more information about each

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2554

Module 5 SPSS command Structure

19

S e s s i onB S P S S C om

m an d S t r u c t ur e

Some SPSS Command examples (from slides)

Te GET coand

The GET command reads a sav le into SPSS

GET FILE = cardiacsav

Te GET coand - KEEP and DROP subcoands

Use the KEEP subcommand to select only some of the variables

GET FILE=cardiacsavKEEP=famhist chd

Use the DROP subcommand to select all of the le except certain

variables

GET FILE=cardiacsavDROP=height weight

Te DATASET NAmE coand

The DATASET NAME command give the active dataset a short name

which can be used in later commands

GET FILE = mspssworkcardiacsav

DATASET NAME cardiac

Te SAVE coand

Use the SAVE command to save your data in a sav le

SAVE OUTFILE=myfilesav

Te SAVE coand wi

save all your cases together with variable names values

missing values variable and value labels and formats

Saved in the state they are in when the command is issued

Any further changes can only be saved with another SAVE command

It will NOT save the results of any analyses results are saved in an

output window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2654

Module 5 Starting with SPSS Syntax

20

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Te SAVE coand KEEP and DROP subcoands

The KEEP and DROP subcommands are available with the SAVE

command and perform the same function as with GET

Using KEEP will save only the named variables

SAVE OUTFILE=filesav KEEP=AGE HEIGHT

Using DROP will save all variables except named ones (here it means

all variables except WEIGHT2)

SAVE OUTFILE=filesav DROP=WEIGHT2

FREQUENCIES coand

FREQUENCIES VARIABLES=HEIGHT

FREQUENCIES - STATISTICS subcoand

FREQ VAR=HEIGHT AGE STATISTICS

Gives four default statistics

MEAN STDDEV MINIMUM MAXIMUM

FREQUENCIES - more statistics

MEDIAN MODE SUM RANGE SEMEAN VARIANCESKEWNESS SESKEW KURTOSIS SEKURT

STATISTICS=DEFAULT

Use to get the default four statistics MEAN STDDEV MINIMUM

MAXIMUM together with others you ask for

STATISTICS=ALL

Gives all available statistics - can be wasteful

STATISTICS=NONE

Equivalent to omitting STATISTICS altogether

FREQUENCIES - FORmAT subcoand

To suppress the frequency table use

FORMAT=NOTABLE

To suppress tables over a certain length

FORMAT=LIMIT(50)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2754

Module 5 SPSS command Structure

21

S e s s i onB S P S S C om

m an d S t r u c t ur e

FREQUENCIES - subcoands for carts

There are three subcommands to produce type of charts named

HISTOGRAM

BARCHART

PIECHART

Using ore tan one subcoand

To use more than one subcommand string them together and nish

the command with the terminating full stop

FREQUENCIES VARIABLES=AGESEX STATISTICS=RANGEBARCHART

CROSSTABS coand

CROSSTABS TABLES=AGE BY SEX

CROSSTABS AGE BY SEX

SEX BY WEIGHT

The next three crosstabs commands produce the same three tables

CROSSTABS AGE BY SEX

AGE BY PARTY

AGE BY INCOME

CROSSTABS AGE BY SEX PARTY INCOMECROSSTABS AGE BY SEX TO INCOME

CROSSTABS - STATISTICS subcoand

CROSSTABS HEIGHT BY AGE

STATISTICS

Gives the Chi-square statistic tests

CROSSTABS - more statistics

Others available

CHISQ PHI CC LAMBDA UC BTAU CTAU GAMMA DETA CORR ALL NONE

and KAPPA RISK MCNEMAR CMH(1)

Usage

CROSSTABS TABLES=AGE BY WEIGHT

STATISTICS=BTAU CORR

Only gives those statistics asked for

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 21: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2154

Session B

SPSS CommandStructure

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2254

Module 5 Starting with SPSS Syntax

16

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Commands Subcommands amp keywords

In general an SPSS command will be a mixture of different elements like this -

v kyw kyw kyw

An SPSS command starts with a recognized command keyword which has to

be spelt correctly Each command has its own general format with its own set of

subcommands and keywords These are combined with lists of variables names

or combinations of variable names and keywords to specify the analysis or action

required by the computer

Subcommands and other keywords are words recognized by SPSS as part of

the commands specication and so appear in the specication eld Some

SPSS procedures have subcommands which can appear either on their own or in

association with certain keywords As with commands subcommands and keywords

have to appear exactly as they are spelt

Figure 17 Different keywords in the Commands example

get FILE=mspssworkCARDIACsav

crosstabs chd B Y famhist CELLS=C O U N T R O W C O L U M N STATISTICS=C H I S Q

requencies VARIABLES=height cigaret

FORMAT=L I M I T (12 ) HIST

graPH SCATTERPLOT weight W I T H height

inisH

The different types of keyword have been highlighted as follows-

command keywords SUBCOMMAND keywords

O T H E R keywords variable names and other specication

Abbreviating keywords

SPSS will recognise most of its command and subcommand keywords when

abbreviated to the rst three or four characters of the name as long as the keywordsare unique So for example-

freq vars=height cigaret form=lim(12) hist

can be used as an abbreviation of-

frequencies variables=height cigaret

format=limit(12)

histogram

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2354

Module 5 SPSS command Structure

17

S e s s i onB S P S S C om

m an d S t r u c t ur e

Reserved keywords

With most procedures it does not matter if there is a variable name the same as a

keyword since there are certain places in a command specication where a variable

or variable list is expected and others where keywords are expected However there

are some reserved keywords which cannot be used as names These are

ALL AND BY EQ GE GT LE LT NE NOT OR TO WITH

These words cannot be used as variable names in SPSS because the program

already recognizes them as having meaning For example

FREQUENCIES VARIABLES=ALL

calculates a frequency table for all the variables currently stored on SPSSs

active le To have a variable called A L L would make the sense of the instruction

ambiguous to SPSS

Lists of Variables

The variables are kept in the SPSS active le in the order they are given in the

DATA lIST command and any variables generated during the session in the order

they were created This order can be utilized when instead of typing a long list of

consecutive variables you type the name of the rst variable then the TO keyword

and then the name of the last variable in the list These inclusive ranges of variables

can be combined with the names of other variables to produce a larger variable list

where needed For example if we had the following variables in a system le-

LIST OF VARIABLES ON THE ACTIVE FILE

1 AGE 6 HHLDTYPE 11 OCPENAMT 16 SPSCLASS

2 CHRONILL 7 HOMEHELP 12 REBTOWN 17 TOTINCID

3 CLASS 8 KINNEAR 13 REBTRENT 18 UNEARNED

4 EMPLOYED 9 MARITAL 14 GENDER 19 CASWGT

5 HEALTH 10 NIRAMT 15 SPSAGE

In SPSS variable lists individual variable names andor variable ranges can

be separated by either spaces or commas However any keywords have to be

separated from variable names by spaces ie use spaces with TO or All

AGE TO CASWGT implies

AGE CHRONILL CLASS EMPLOYED HEALTH HHLDTYPEHOMEHELP KINNEAR MARITAL NIRAMT OCPENAMT REBTOWNREBTRENT GENDER SPSAGE SPSCLASS TOTINCID UNEARNEDCASWGT

This rst list is all the variables in the active le since AGE is the rst variable on the

list and CASWGT is the last

hhlDTYPE TO GENDER implies

HHLDTYPE HOMEHELP KINNEAR MARITAL NIRAMT OCPENAMTREBTOWN REBTRENT GENDER

Notice in this list 7 of the 9 variables are implied in the same order as the active le

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2454

Module 5 Starting with SPSS Syntax

18

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e ClASS hEAlTh TO NIRAmT implies

CLASS HEALTH HHLDTYPE HOMEHELP KINNEAR MARITALNIRAMT

A list can be combined with individual variables

AGE SPAGE ClASS SPClASS All implies

AGE SPSAGE CLASS SPSCLASS CHRONILLEMPLOYED HEALTH HHLDTYPE HOMEHELP KINNEARMARITAL NIRAMT OCPENAMT REBTOWN REBTRENT GENDERTOTINCID UNEARNED CASWGT

The reserved keyword ALL in this context means all the variables in the active le

It can be used in a variable list for the KEEP subcommand on a GET or SAVE

command to change the order the variables are saved in whilst making sure no

variables are missed Variables not specically listed will appear after all the listed

ones in active le order

If new variables are created during a session they will be added to the active le in

the order they were created after the variables already stored in the active le You

can always check the variable order in the active le using the DISPlAY command -

it may not be the same order they will appear in dialog boxes

Syntax Charts

The Sow synta cart button as seen in the previous session in Figure 11 will let

you look at command syntax charts If you have no command selected in the syntax

window then the button will give you an alphabetic list of all the command keywords

Look at the syntax chart for each of these commands to see what subcommands and keywords are available

Use the Search button to look for more information about each

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2554

Module 5 SPSS command Structure

19

S e s s i onB S P S S C om

m an d S t r u c t ur e

Some SPSS Command examples (from slides)

Te GET coand

The GET command reads a sav le into SPSS

GET FILE = cardiacsav

Te GET coand - KEEP and DROP subcoands

Use the KEEP subcommand to select only some of the variables

GET FILE=cardiacsavKEEP=famhist chd

Use the DROP subcommand to select all of the le except certain

variables

GET FILE=cardiacsavDROP=height weight

Te DATASET NAmE coand

The DATASET NAME command give the active dataset a short name

which can be used in later commands

GET FILE = mspssworkcardiacsav

DATASET NAME cardiac

Te SAVE coand

Use the SAVE command to save your data in a sav le

SAVE OUTFILE=myfilesav

Te SAVE coand wi

save all your cases together with variable names values

missing values variable and value labels and formats

Saved in the state they are in when the command is issued

Any further changes can only be saved with another SAVE command

It will NOT save the results of any analyses results are saved in an

output window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2654

Module 5 Starting with SPSS Syntax

20

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Te SAVE coand KEEP and DROP subcoands

The KEEP and DROP subcommands are available with the SAVE

command and perform the same function as with GET

Using KEEP will save only the named variables

SAVE OUTFILE=filesav KEEP=AGE HEIGHT

Using DROP will save all variables except named ones (here it means

all variables except WEIGHT2)

SAVE OUTFILE=filesav DROP=WEIGHT2

FREQUENCIES coand

FREQUENCIES VARIABLES=HEIGHT

FREQUENCIES - STATISTICS subcoand

FREQ VAR=HEIGHT AGE STATISTICS

Gives four default statistics

MEAN STDDEV MINIMUM MAXIMUM

FREQUENCIES - more statistics

MEDIAN MODE SUM RANGE SEMEAN VARIANCESKEWNESS SESKEW KURTOSIS SEKURT

STATISTICS=DEFAULT

Use to get the default four statistics MEAN STDDEV MINIMUM

MAXIMUM together with others you ask for

STATISTICS=ALL

Gives all available statistics - can be wasteful

STATISTICS=NONE

Equivalent to omitting STATISTICS altogether

FREQUENCIES - FORmAT subcoand

To suppress the frequency table use

FORMAT=NOTABLE

To suppress tables over a certain length

FORMAT=LIMIT(50)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2754

Module 5 SPSS command Structure

21

S e s s i onB S P S S C om

m an d S t r u c t ur e

FREQUENCIES - subcoands for carts

There are three subcommands to produce type of charts named

HISTOGRAM

BARCHART

PIECHART

Using ore tan one subcoand

To use more than one subcommand string them together and nish

the command with the terminating full stop

FREQUENCIES VARIABLES=AGESEX STATISTICS=RANGEBARCHART

CROSSTABS coand

CROSSTABS TABLES=AGE BY SEX

CROSSTABS AGE BY SEX

SEX BY WEIGHT

The next three crosstabs commands produce the same three tables

CROSSTABS AGE BY SEX

AGE BY PARTY

AGE BY INCOME

CROSSTABS AGE BY SEX PARTY INCOMECROSSTABS AGE BY SEX TO INCOME

CROSSTABS - STATISTICS subcoand

CROSSTABS HEIGHT BY AGE

STATISTICS

Gives the Chi-square statistic tests

CROSSTABS - more statistics

Others available

CHISQ PHI CC LAMBDA UC BTAU CTAU GAMMA DETA CORR ALL NONE

and KAPPA RISK MCNEMAR CMH(1)

Usage

CROSSTABS TABLES=AGE BY WEIGHT

STATISTICS=BTAU CORR

Only gives those statistics asked for

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 22: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2254

Module 5 Starting with SPSS Syntax

16

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Commands Subcommands amp keywords

In general an SPSS command will be a mixture of different elements like this -

v kyw kyw kyw

An SPSS command starts with a recognized command keyword which has to

be spelt correctly Each command has its own general format with its own set of

subcommands and keywords These are combined with lists of variables names

or combinations of variable names and keywords to specify the analysis or action

required by the computer

Subcommands and other keywords are words recognized by SPSS as part of

the commands specication and so appear in the specication eld Some

SPSS procedures have subcommands which can appear either on their own or in

association with certain keywords As with commands subcommands and keywords

have to appear exactly as they are spelt

Figure 17 Different keywords in the Commands example

get FILE=mspssworkCARDIACsav

crosstabs chd B Y famhist CELLS=C O U N T R O W C O L U M N STATISTICS=C H I S Q

requencies VARIABLES=height cigaret

FORMAT=L I M I T (12 ) HIST

graPH SCATTERPLOT weight W I T H height

inisH

The different types of keyword have been highlighted as follows-

command keywords SUBCOMMAND keywords

O T H E R keywords variable names and other specication

Abbreviating keywords

SPSS will recognise most of its command and subcommand keywords when

abbreviated to the rst three or four characters of the name as long as the keywordsare unique So for example-

freq vars=height cigaret form=lim(12) hist

can be used as an abbreviation of-

frequencies variables=height cigaret

format=limit(12)

histogram

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2354

Module 5 SPSS command Structure

17

S e s s i onB S P S S C om

m an d S t r u c t ur e

Reserved keywords

With most procedures it does not matter if there is a variable name the same as a

keyword since there are certain places in a command specication where a variable

or variable list is expected and others where keywords are expected However there

are some reserved keywords which cannot be used as names These are

ALL AND BY EQ GE GT LE LT NE NOT OR TO WITH

These words cannot be used as variable names in SPSS because the program

already recognizes them as having meaning For example

FREQUENCIES VARIABLES=ALL

calculates a frequency table for all the variables currently stored on SPSSs

active le To have a variable called A L L would make the sense of the instruction

ambiguous to SPSS

Lists of Variables

The variables are kept in the SPSS active le in the order they are given in the

DATA lIST command and any variables generated during the session in the order

they were created This order can be utilized when instead of typing a long list of

consecutive variables you type the name of the rst variable then the TO keyword

and then the name of the last variable in the list These inclusive ranges of variables

can be combined with the names of other variables to produce a larger variable list

where needed For example if we had the following variables in a system le-

LIST OF VARIABLES ON THE ACTIVE FILE

1 AGE 6 HHLDTYPE 11 OCPENAMT 16 SPSCLASS

2 CHRONILL 7 HOMEHELP 12 REBTOWN 17 TOTINCID

3 CLASS 8 KINNEAR 13 REBTRENT 18 UNEARNED

4 EMPLOYED 9 MARITAL 14 GENDER 19 CASWGT

5 HEALTH 10 NIRAMT 15 SPSAGE

In SPSS variable lists individual variable names andor variable ranges can

be separated by either spaces or commas However any keywords have to be

separated from variable names by spaces ie use spaces with TO or All

AGE TO CASWGT implies

AGE CHRONILL CLASS EMPLOYED HEALTH HHLDTYPEHOMEHELP KINNEAR MARITAL NIRAMT OCPENAMT REBTOWNREBTRENT GENDER SPSAGE SPSCLASS TOTINCID UNEARNEDCASWGT

This rst list is all the variables in the active le since AGE is the rst variable on the

list and CASWGT is the last

hhlDTYPE TO GENDER implies

HHLDTYPE HOMEHELP KINNEAR MARITAL NIRAMT OCPENAMTREBTOWN REBTRENT GENDER

Notice in this list 7 of the 9 variables are implied in the same order as the active le

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2454

Module 5 Starting with SPSS Syntax

18

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e ClASS hEAlTh TO NIRAmT implies

CLASS HEALTH HHLDTYPE HOMEHELP KINNEAR MARITALNIRAMT

A list can be combined with individual variables

AGE SPAGE ClASS SPClASS All implies

AGE SPSAGE CLASS SPSCLASS CHRONILLEMPLOYED HEALTH HHLDTYPE HOMEHELP KINNEARMARITAL NIRAMT OCPENAMT REBTOWN REBTRENT GENDERTOTINCID UNEARNED CASWGT

The reserved keyword ALL in this context means all the variables in the active le

It can be used in a variable list for the KEEP subcommand on a GET or SAVE

command to change the order the variables are saved in whilst making sure no

variables are missed Variables not specically listed will appear after all the listed

ones in active le order

If new variables are created during a session they will be added to the active le in

the order they were created after the variables already stored in the active le You

can always check the variable order in the active le using the DISPlAY command -

it may not be the same order they will appear in dialog boxes

Syntax Charts

The Sow synta cart button as seen in the previous session in Figure 11 will let

you look at command syntax charts If you have no command selected in the syntax

window then the button will give you an alphabetic list of all the command keywords

Look at the syntax chart for each of these commands to see what subcommands and keywords are available

Use the Search button to look for more information about each

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2554

Module 5 SPSS command Structure

19

S e s s i onB S P S S C om

m an d S t r u c t ur e

Some SPSS Command examples (from slides)

Te GET coand

The GET command reads a sav le into SPSS

GET FILE = cardiacsav

Te GET coand - KEEP and DROP subcoands

Use the KEEP subcommand to select only some of the variables

GET FILE=cardiacsavKEEP=famhist chd

Use the DROP subcommand to select all of the le except certain

variables

GET FILE=cardiacsavDROP=height weight

Te DATASET NAmE coand

The DATASET NAME command give the active dataset a short name

which can be used in later commands

GET FILE = mspssworkcardiacsav

DATASET NAME cardiac

Te SAVE coand

Use the SAVE command to save your data in a sav le

SAVE OUTFILE=myfilesav

Te SAVE coand wi

save all your cases together with variable names values

missing values variable and value labels and formats

Saved in the state they are in when the command is issued

Any further changes can only be saved with another SAVE command

It will NOT save the results of any analyses results are saved in an

output window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2654

Module 5 Starting with SPSS Syntax

20

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Te SAVE coand KEEP and DROP subcoands

The KEEP and DROP subcommands are available with the SAVE

command and perform the same function as with GET

Using KEEP will save only the named variables

SAVE OUTFILE=filesav KEEP=AGE HEIGHT

Using DROP will save all variables except named ones (here it means

all variables except WEIGHT2)

SAVE OUTFILE=filesav DROP=WEIGHT2

FREQUENCIES coand

FREQUENCIES VARIABLES=HEIGHT

FREQUENCIES - STATISTICS subcoand

FREQ VAR=HEIGHT AGE STATISTICS

Gives four default statistics

MEAN STDDEV MINIMUM MAXIMUM

FREQUENCIES - more statistics

MEDIAN MODE SUM RANGE SEMEAN VARIANCESKEWNESS SESKEW KURTOSIS SEKURT

STATISTICS=DEFAULT

Use to get the default four statistics MEAN STDDEV MINIMUM

MAXIMUM together with others you ask for

STATISTICS=ALL

Gives all available statistics - can be wasteful

STATISTICS=NONE

Equivalent to omitting STATISTICS altogether

FREQUENCIES - FORmAT subcoand

To suppress the frequency table use

FORMAT=NOTABLE

To suppress tables over a certain length

FORMAT=LIMIT(50)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2754

Module 5 SPSS command Structure

21

S e s s i onB S P S S C om

m an d S t r u c t ur e

FREQUENCIES - subcoands for carts

There are three subcommands to produce type of charts named

HISTOGRAM

BARCHART

PIECHART

Using ore tan one subcoand

To use more than one subcommand string them together and nish

the command with the terminating full stop

FREQUENCIES VARIABLES=AGESEX STATISTICS=RANGEBARCHART

CROSSTABS coand

CROSSTABS TABLES=AGE BY SEX

CROSSTABS AGE BY SEX

SEX BY WEIGHT

The next three crosstabs commands produce the same three tables

CROSSTABS AGE BY SEX

AGE BY PARTY

AGE BY INCOME

CROSSTABS AGE BY SEX PARTY INCOMECROSSTABS AGE BY SEX TO INCOME

CROSSTABS - STATISTICS subcoand

CROSSTABS HEIGHT BY AGE

STATISTICS

Gives the Chi-square statistic tests

CROSSTABS - more statistics

Others available

CHISQ PHI CC LAMBDA UC BTAU CTAU GAMMA DETA CORR ALL NONE

and KAPPA RISK MCNEMAR CMH(1)

Usage

CROSSTABS TABLES=AGE BY WEIGHT

STATISTICS=BTAU CORR

Only gives those statistics asked for

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 23: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2354

Module 5 SPSS command Structure

17

S e s s i onB S P S S C om

m an d S t r u c t ur e

Reserved keywords

With most procedures it does not matter if there is a variable name the same as a

keyword since there are certain places in a command specication where a variable

or variable list is expected and others where keywords are expected However there

are some reserved keywords which cannot be used as names These are

ALL AND BY EQ GE GT LE LT NE NOT OR TO WITH

These words cannot be used as variable names in SPSS because the program

already recognizes them as having meaning For example

FREQUENCIES VARIABLES=ALL

calculates a frequency table for all the variables currently stored on SPSSs

active le To have a variable called A L L would make the sense of the instruction

ambiguous to SPSS

Lists of Variables

The variables are kept in the SPSS active le in the order they are given in the

DATA lIST command and any variables generated during the session in the order

they were created This order can be utilized when instead of typing a long list of

consecutive variables you type the name of the rst variable then the TO keyword

and then the name of the last variable in the list These inclusive ranges of variables

can be combined with the names of other variables to produce a larger variable list

where needed For example if we had the following variables in a system le-

LIST OF VARIABLES ON THE ACTIVE FILE

1 AGE 6 HHLDTYPE 11 OCPENAMT 16 SPSCLASS

2 CHRONILL 7 HOMEHELP 12 REBTOWN 17 TOTINCID

3 CLASS 8 KINNEAR 13 REBTRENT 18 UNEARNED

4 EMPLOYED 9 MARITAL 14 GENDER 19 CASWGT

5 HEALTH 10 NIRAMT 15 SPSAGE

In SPSS variable lists individual variable names andor variable ranges can

be separated by either spaces or commas However any keywords have to be

separated from variable names by spaces ie use spaces with TO or All

AGE TO CASWGT implies

AGE CHRONILL CLASS EMPLOYED HEALTH HHLDTYPEHOMEHELP KINNEAR MARITAL NIRAMT OCPENAMT REBTOWNREBTRENT GENDER SPSAGE SPSCLASS TOTINCID UNEARNEDCASWGT

This rst list is all the variables in the active le since AGE is the rst variable on the

list and CASWGT is the last

hhlDTYPE TO GENDER implies

HHLDTYPE HOMEHELP KINNEAR MARITAL NIRAMT OCPENAMTREBTOWN REBTRENT GENDER

Notice in this list 7 of the 9 variables are implied in the same order as the active le

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2454

Module 5 Starting with SPSS Syntax

18

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e ClASS hEAlTh TO NIRAmT implies

CLASS HEALTH HHLDTYPE HOMEHELP KINNEAR MARITALNIRAMT

A list can be combined with individual variables

AGE SPAGE ClASS SPClASS All implies

AGE SPSAGE CLASS SPSCLASS CHRONILLEMPLOYED HEALTH HHLDTYPE HOMEHELP KINNEARMARITAL NIRAMT OCPENAMT REBTOWN REBTRENT GENDERTOTINCID UNEARNED CASWGT

The reserved keyword ALL in this context means all the variables in the active le

It can be used in a variable list for the KEEP subcommand on a GET or SAVE

command to change the order the variables are saved in whilst making sure no

variables are missed Variables not specically listed will appear after all the listed

ones in active le order

If new variables are created during a session they will be added to the active le in

the order they were created after the variables already stored in the active le You

can always check the variable order in the active le using the DISPlAY command -

it may not be the same order they will appear in dialog boxes

Syntax Charts

The Sow synta cart button as seen in the previous session in Figure 11 will let

you look at command syntax charts If you have no command selected in the syntax

window then the button will give you an alphabetic list of all the command keywords

Look at the syntax chart for each of these commands to see what subcommands and keywords are available

Use the Search button to look for more information about each

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2554

Module 5 SPSS command Structure

19

S e s s i onB S P S S C om

m an d S t r u c t ur e

Some SPSS Command examples (from slides)

Te GET coand

The GET command reads a sav le into SPSS

GET FILE = cardiacsav

Te GET coand - KEEP and DROP subcoands

Use the KEEP subcommand to select only some of the variables

GET FILE=cardiacsavKEEP=famhist chd

Use the DROP subcommand to select all of the le except certain

variables

GET FILE=cardiacsavDROP=height weight

Te DATASET NAmE coand

The DATASET NAME command give the active dataset a short name

which can be used in later commands

GET FILE = mspssworkcardiacsav

DATASET NAME cardiac

Te SAVE coand

Use the SAVE command to save your data in a sav le

SAVE OUTFILE=myfilesav

Te SAVE coand wi

save all your cases together with variable names values

missing values variable and value labels and formats

Saved in the state they are in when the command is issued

Any further changes can only be saved with another SAVE command

It will NOT save the results of any analyses results are saved in an

output window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2654

Module 5 Starting with SPSS Syntax

20

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Te SAVE coand KEEP and DROP subcoands

The KEEP and DROP subcommands are available with the SAVE

command and perform the same function as with GET

Using KEEP will save only the named variables

SAVE OUTFILE=filesav KEEP=AGE HEIGHT

Using DROP will save all variables except named ones (here it means

all variables except WEIGHT2)

SAVE OUTFILE=filesav DROP=WEIGHT2

FREQUENCIES coand

FREQUENCIES VARIABLES=HEIGHT

FREQUENCIES - STATISTICS subcoand

FREQ VAR=HEIGHT AGE STATISTICS

Gives four default statistics

MEAN STDDEV MINIMUM MAXIMUM

FREQUENCIES - more statistics

MEDIAN MODE SUM RANGE SEMEAN VARIANCESKEWNESS SESKEW KURTOSIS SEKURT

STATISTICS=DEFAULT

Use to get the default four statistics MEAN STDDEV MINIMUM

MAXIMUM together with others you ask for

STATISTICS=ALL

Gives all available statistics - can be wasteful

STATISTICS=NONE

Equivalent to omitting STATISTICS altogether

FREQUENCIES - FORmAT subcoand

To suppress the frequency table use

FORMAT=NOTABLE

To suppress tables over a certain length

FORMAT=LIMIT(50)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2754

Module 5 SPSS command Structure

21

S e s s i onB S P S S C om

m an d S t r u c t ur e

FREQUENCIES - subcoands for carts

There are three subcommands to produce type of charts named

HISTOGRAM

BARCHART

PIECHART

Using ore tan one subcoand

To use more than one subcommand string them together and nish

the command with the terminating full stop

FREQUENCIES VARIABLES=AGESEX STATISTICS=RANGEBARCHART

CROSSTABS coand

CROSSTABS TABLES=AGE BY SEX

CROSSTABS AGE BY SEX

SEX BY WEIGHT

The next three crosstabs commands produce the same three tables

CROSSTABS AGE BY SEX

AGE BY PARTY

AGE BY INCOME

CROSSTABS AGE BY SEX PARTY INCOMECROSSTABS AGE BY SEX TO INCOME

CROSSTABS - STATISTICS subcoand

CROSSTABS HEIGHT BY AGE

STATISTICS

Gives the Chi-square statistic tests

CROSSTABS - more statistics

Others available

CHISQ PHI CC LAMBDA UC BTAU CTAU GAMMA DETA CORR ALL NONE

and KAPPA RISK MCNEMAR CMH(1)

Usage

CROSSTABS TABLES=AGE BY WEIGHT

STATISTICS=BTAU CORR

Only gives those statistics asked for

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 24: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2454

Module 5 Starting with SPSS Syntax

18

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e ClASS hEAlTh TO NIRAmT implies

CLASS HEALTH HHLDTYPE HOMEHELP KINNEAR MARITALNIRAMT

A list can be combined with individual variables

AGE SPAGE ClASS SPClASS All implies

AGE SPSAGE CLASS SPSCLASS CHRONILLEMPLOYED HEALTH HHLDTYPE HOMEHELP KINNEARMARITAL NIRAMT OCPENAMT REBTOWN REBTRENT GENDERTOTINCID UNEARNED CASWGT

The reserved keyword ALL in this context means all the variables in the active le

It can be used in a variable list for the KEEP subcommand on a GET or SAVE

command to change the order the variables are saved in whilst making sure no

variables are missed Variables not specically listed will appear after all the listed

ones in active le order

If new variables are created during a session they will be added to the active le in

the order they were created after the variables already stored in the active le You

can always check the variable order in the active le using the DISPlAY command -

it may not be the same order they will appear in dialog boxes

Syntax Charts

The Sow synta cart button as seen in the previous session in Figure 11 will let

you look at command syntax charts If you have no command selected in the syntax

window then the button will give you an alphabetic list of all the command keywords

Look at the syntax chart for each of these commands to see what subcommands and keywords are available

Use the Search button to look for more information about each

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2554

Module 5 SPSS command Structure

19

S e s s i onB S P S S C om

m an d S t r u c t ur e

Some SPSS Command examples (from slides)

Te GET coand

The GET command reads a sav le into SPSS

GET FILE = cardiacsav

Te GET coand - KEEP and DROP subcoands

Use the KEEP subcommand to select only some of the variables

GET FILE=cardiacsavKEEP=famhist chd

Use the DROP subcommand to select all of the le except certain

variables

GET FILE=cardiacsavDROP=height weight

Te DATASET NAmE coand

The DATASET NAME command give the active dataset a short name

which can be used in later commands

GET FILE = mspssworkcardiacsav

DATASET NAME cardiac

Te SAVE coand

Use the SAVE command to save your data in a sav le

SAVE OUTFILE=myfilesav

Te SAVE coand wi

save all your cases together with variable names values

missing values variable and value labels and formats

Saved in the state they are in when the command is issued

Any further changes can only be saved with another SAVE command

It will NOT save the results of any analyses results are saved in an

output window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2654

Module 5 Starting with SPSS Syntax

20

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Te SAVE coand KEEP and DROP subcoands

The KEEP and DROP subcommands are available with the SAVE

command and perform the same function as with GET

Using KEEP will save only the named variables

SAVE OUTFILE=filesav KEEP=AGE HEIGHT

Using DROP will save all variables except named ones (here it means

all variables except WEIGHT2)

SAVE OUTFILE=filesav DROP=WEIGHT2

FREQUENCIES coand

FREQUENCIES VARIABLES=HEIGHT

FREQUENCIES - STATISTICS subcoand

FREQ VAR=HEIGHT AGE STATISTICS

Gives four default statistics

MEAN STDDEV MINIMUM MAXIMUM

FREQUENCIES - more statistics

MEDIAN MODE SUM RANGE SEMEAN VARIANCESKEWNESS SESKEW KURTOSIS SEKURT

STATISTICS=DEFAULT

Use to get the default four statistics MEAN STDDEV MINIMUM

MAXIMUM together with others you ask for

STATISTICS=ALL

Gives all available statistics - can be wasteful

STATISTICS=NONE

Equivalent to omitting STATISTICS altogether

FREQUENCIES - FORmAT subcoand

To suppress the frequency table use

FORMAT=NOTABLE

To suppress tables over a certain length

FORMAT=LIMIT(50)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2754

Module 5 SPSS command Structure

21

S e s s i onB S P S S C om

m an d S t r u c t ur e

FREQUENCIES - subcoands for carts

There are three subcommands to produce type of charts named

HISTOGRAM

BARCHART

PIECHART

Using ore tan one subcoand

To use more than one subcommand string them together and nish

the command with the terminating full stop

FREQUENCIES VARIABLES=AGESEX STATISTICS=RANGEBARCHART

CROSSTABS coand

CROSSTABS TABLES=AGE BY SEX

CROSSTABS AGE BY SEX

SEX BY WEIGHT

The next three crosstabs commands produce the same three tables

CROSSTABS AGE BY SEX

AGE BY PARTY

AGE BY INCOME

CROSSTABS AGE BY SEX PARTY INCOMECROSSTABS AGE BY SEX TO INCOME

CROSSTABS - STATISTICS subcoand

CROSSTABS HEIGHT BY AGE

STATISTICS

Gives the Chi-square statistic tests

CROSSTABS - more statistics

Others available

CHISQ PHI CC LAMBDA UC BTAU CTAU GAMMA DETA CORR ALL NONE

and KAPPA RISK MCNEMAR CMH(1)

Usage

CROSSTABS TABLES=AGE BY WEIGHT

STATISTICS=BTAU CORR

Only gives those statistics asked for

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 25: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2554

Module 5 SPSS command Structure

19

S e s s i onB S P S S C om

m an d S t r u c t ur e

Some SPSS Command examples (from slides)

Te GET coand

The GET command reads a sav le into SPSS

GET FILE = cardiacsav

Te GET coand - KEEP and DROP subcoands

Use the KEEP subcommand to select only some of the variables

GET FILE=cardiacsavKEEP=famhist chd

Use the DROP subcommand to select all of the le except certain

variables

GET FILE=cardiacsavDROP=height weight

Te DATASET NAmE coand

The DATASET NAME command give the active dataset a short name

which can be used in later commands

GET FILE = mspssworkcardiacsav

DATASET NAME cardiac

Te SAVE coand

Use the SAVE command to save your data in a sav le

SAVE OUTFILE=myfilesav

Te SAVE coand wi

save all your cases together with variable names values

missing values variable and value labels and formats

Saved in the state they are in when the command is issued

Any further changes can only be saved with another SAVE command

It will NOT save the results of any analyses results are saved in an

output window

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2654

Module 5 Starting with SPSS Syntax

20

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Te SAVE coand KEEP and DROP subcoands

The KEEP and DROP subcommands are available with the SAVE

command and perform the same function as with GET

Using KEEP will save only the named variables

SAVE OUTFILE=filesav KEEP=AGE HEIGHT

Using DROP will save all variables except named ones (here it means

all variables except WEIGHT2)

SAVE OUTFILE=filesav DROP=WEIGHT2

FREQUENCIES coand

FREQUENCIES VARIABLES=HEIGHT

FREQUENCIES - STATISTICS subcoand

FREQ VAR=HEIGHT AGE STATISTICS

Gives four default statistics

MEAN STDDEV MINIMUM MAXIMUM

FREQUENCIES - more statistics

MEDIAN MODE SUM RANGE SEMEAN VARIANCESKEWNESS SESKEW KURTOSIS SEKURT

STATISTICS=DEFAULT

Use to get the default four statistics MEAN STDDEV MINIMUM

MAXIMUM together with others you ask for

STATISTICS=ALL

Gives all available statistics - can be wasteful

STATISTICS=NONE

Equivalent to omitting STATISTICS altogether

FREQUENCIES - FORmAT subcoand

To suppress the frequency table use

FORMAT=NOTABLE

To suppress tables over a certain length

FORMAT=LIMIT(50)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2754

Module 5 SPSS command Structure

21

S e s s i onB S P S S C om

m an d S t r u c t ur e

FREQUENCIES - subcoands for carts

There are three subcommands to produce type of charts named

HISTOGRAM

BARCHART

PIECHART

Using ore tan one subcoand

To use more than one subcommand string them together and nish

the command with the terminating full stop

FREQUENCIES VARIABLES=AGESEX STATISTICS=RANGEBARCHART

CROSSTABS coand

CROSSTABS TABLES=AGE BY SEX

CROSSTABS AGE BY SEX

SEX BY WEIGHT

The next three crosstabs commands produce the same three tables

CROSSTABS AGE BY SEX

AGE BY PARTY

AGE BY INCOME

CROSSTABS AGE BY SEX PARTY INCOMECROSSTABS AGE BY SEX TO INCOME

CROSSTABS - STATISTICS subcoand

CROSSTABS HEIGHT BY AGE

STATISTICS

Gives the Chi-square statistic tests

CROSSTABS - more statistics

Others available

CHISQ PHI CC LAMBDA UC BTAU CTAU GAMMA DETA CORR ALL NONE

and KAPPA RISK MCNEMAR CMH(1)

Usage

CROSSTABS TABLES=AGE BY WEIGHT

STATISTICS=BTAU CORR

Only gives those statistics asked for

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 26: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2654

Module 5 Starting with SPSS Syntax

20

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e Te SAVE coand KEEP and DROP subcoands

The KEEP and DROP subcommands are available with the SAVE

command and perform the same function as with GET

Using KEEP will save only the named variables

SAVE OUTFILE=filesav KEEP=AGE HEIGHT

Using DROP will save all variables except named ones (here it means

all variables except WEIGHT2)

SAVE OUTFILE=filesav DROP=WEIGHT2

FREQUENCIES coand

FREQUENCIES VARIABLES=HEIGHT

FREQUENCIES - STATISTICS subcoand

FREQ VAR=HEIGHT AGE STATISTICS

Gives four default statistics

MEAN STDDEV MINIMUM MAXIMUM

FREQUENCIES - more statistics

MEDIAN MODE SUM RANGE SEMEAN VARIANCESKEWNESS SESKEW KURTOSIS SEKURT

STATISTICS=DEFAULT

Use to get the default four statistics MEAN STDDEV MINIMUM

MAXIMUM together with others you ask for

STATISTICS=ALL

Gives all available statistics - can be wasteful

STATISTICS=NONE

Equivalent to omitting STATISTICS altogether

FREQUENCIES - FORmAT subcoand

To suppress the frequency table use

FORMAT=NOTABLE

To suppress tables over a certain length

FORMAT=LIMIT(50)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2754

Module 5 SPSS command Structure

21

S e s s i onB S P S S C om

m an d S t r u c t ur e

FREQUENCIES - subcoands for carts

There are three subcommands to produce type of charts named

HISTOGRAM

BARCHART

PIECHART

Using ore tan one subcoand

To use more than one subcommand string them together and nish

the command with the terminating full stop

FREQUENCIES VARIABLES=AGESEX STATISTICS=RANGEBARCHART

CROSSTABS coand

CROSSTABS TABLES=AGE BY SEX

CROSSTABS AGE BY SEX

SEX BY WEIGHT

The next three crosstabs commands produce the same three tables

CROSSTABS AGE BY SEX

AGE BY PARTY

AGE BY INCOME

CROSSTABS AGE BY SEX PARTY INCOMECROSSTABS AGE BY SEX TO INCOME

CROSSTABS - STATISTICS subcoand

CROSSTABS HEIGHT BY AGE

STATISTICS

Gives the Chi-square statistic tests

CROSSTABS - more statistics

Others available

CHISQ PHI CC LAMBDA UC BTAU CTAU GAMMA DETA CORR ALL NONE

and KAPPA RISK MCNEMAR CMH(1)

Usage

CROSSTABS TABLES=AGE BY WEIGHT

STATISTICS=BTAU CORR

Only gives those statistics asked for

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 27: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2754

Module 5 SPSS command Structure

21

S e s s i onB S P S S C om

m an d S t r u c t ur e

FREQUENCIES - subcoands for carts

There are three subcommands to produce type of charts named

HISTOGRAM

BARCHART

PIECHART

Using ore tan one subcoand

To use more than one subcommand string them together and nish

the command with the terminating full stop

FREQUENCIES VARIABLES=AGESEX STATISTICS=RANGEBARCHART

CROSSTABS coand

CROSSTABS TABLES=AGE BY SEX

CROSSTABS AGE BY SEX

SEX BY WEIGHT

The next three crosstabs commands produce the same three tables

CROSSTABS AGE BY SEX

AGE BY PARTY

AGE BY INCOME

CROSSTABS AGE BY SEX PARTY INCOMECROSSTABS AGE BY SEX TO INCOME

CROSSTABS - STATISTICS subcoand

CROSSTABS HEIGHT BY AGE

STATISTICS

Gives the Chi-square statistic tests

CROSSTABS - more statistics

Others available

CHISQ PHI CC LAMBDA UC BTAU CTAU GAMMA DETA CORR ALL NONE

and KAPPA RISK MCNEMAR CMH(1)

Usage

CROSSTABS TABLES=AGE BY WEIGHT

STATISTICS=BTAU CORR

Only gives those statistics asked for

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 28: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2854

Module 5 Starting with SPSS Syntax

22

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e CROSSTABS - CEllS subcoand

Prints additional information in tables

COUNT Cell counts (default)

ROW Row percentages

COLUMN Column percentages

TOTAL Two-way total percentages

EXPECTED Expected frequencies

RESID Residuals

ASRESID Adjusted standardised residuals

Alternatively two control words

ALL all of the previously listed information

NONE no cell information

CROSSTABS - BARChART subcoand

CROSSTABS HEIGHT BY AGE BARCHART

Will produce a barchart for each table specication

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 29: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 2954

Module 5 SPSS command Structure

23

S e s s i onB S P S S C om

m an d S t r u c t ur e

Try yourself 1 Start an ordinary SPSS session and try to generate a syntax leusing the Paste button on each dialog box to cover the following actions

Opening the data le1 banksav in my Docuentsspsswork

(creating Get and DATASET commands)

frequencies2 for categorical variables (such as sex minority jobcatsexrace)

frequencies3 for the continuous variables (such as salbeg

salnow) change the format of the frequencies table to its

condensed form Also specify some summary statistics to nd out

more about the data

Crosstabs4 to nd the percentage of women employed in each job

category

Crosstabs5 to nd the percentage of minority employees in each job

category

Crosstabs6 to nd the percentage of non-white women in each job

category You will need to use the Ces button for percents and the

Statistics button if you want a chi square test to test differences

within each job category

2 Save the syntax le and exit from SPSS

3 Double-click on the saved syntax le and run the commands

The last part is the tricky bit errors in running SPSS commands come from spelling

mistakes or not nding the datale in the rst place Always start off at the beginning

of the output window when checking for errors as subsequent errors are often due to

a simple mistake at the start

Ask the tutor for help if you cant get the commands - itssometimes quite hard to workout where the mistake is when

youre getting started

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 30: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3054

Module 5 Starting with SPSS Syntax

24

S e s s i o n B S P

S S c o m m a n d S t r u c t u r e

Session summaryThis session covered looking at how SPSS commands are struture

Commands start with a Command keyword end with a full stopbull

Command subcommand and reserved keywordsbull

Seen examples of some simple SPSS commandsbull

The next session C concentrates on SPSS calculation

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 31: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3154

Session C

Syntax forCalculations

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 32: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3254

Module 5 Syntax for Calculations

26

S e s s i o n C S y n a x f o r C a l c u l a t i o n s Presentation slides for calculations

This section contains mostly the slides from the course presentations Most of

concepts underlying these commands have been covered as procedures in SPSS 3

Chaniging Data in SPSS course Course Code 1303 using the menus

More information about each procedure can be found in the SPSS syntax guide

which comes with SPSS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 33: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3354

Module 5 Syntax for Calculations

27

S e s s i on C S y n t ax f or

S P S S C omm an d s

RECODE commandRECODE variable (old value(s) = new value)

Examples

RECODE SEX (1=0)

RECODE WORK(123 = 1) (456 =2)

RECODE WORK (1 THRU 3=1) (4 THRU 6=2)

RECODE PARTY CODE (2 = 1)

RECODE coand using ranges

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(ELSE = 3)

RECODE coand using hI amp lO keywords

RECODE AGE (LOWEST THRU 30 = 1)

(30 THRU 60 = 2) (60 THRU HIGHEST = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)

(60 THRU HI = 3)

RECODE AGE (LO THRU 30 = 1)(30 THRU 60 = 2)(ELSE = 3)

RECODE coand ndash INTO keyword

Recoding into different variables can be done using the INTO

keyword

RECODE AGE (LO THRU 45 = 1)

(45 THRU HI = 2) INTO AGEGRP

Puts the new values into AGEGRP and leaves AGE with the original

old values

Recoding from alphabetic to numeric variables must be done using

the INTO keyword

RECODE EDUC (SEC = 1)(POLY = 2)(UNIV = 3) INTO NEWED

RECODE ALPHA(0=0)(1=1)(2=2) (9=9)

INTO NUMRECODE ALPHA(CONVERT) INTO NUM

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 34: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3454

Module 5 Syntax for Calculations

28

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COMPUTE command

COMPUTE Target_Variable = Numeric Expression

COMPUTE INCOME = WAGES + BONUS

COMPUTE F = C 9 5 + 32

OPERATORS USED WITh COmPUTE

+ ADDITION

- SUBTRACTION

MULTIPLICATION

DIVISION

EXPONENTIATION

( ) BRACKETS

COmPUTE coand functions

Arithmetic and trig functions

Statistical summary functions

Date and time functions

Distribution functions amp Random numbers

Stringtext functions

System variables

A good place to browse the functions in menus is Transform gt

Compute

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 35: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3554

Module 5 Syntax for Calculations

29

S e s s i on C S y n t ax f or

S P S S C omm an d s

Copute function eapes

SQRT square root

COMPUTE DIAG = SIDE SQRT(2)

LG10 log (base 10)

COMPUTE LOGPOP = LG10(POPULATION)

MOD remainder

COMPUTE YEAR2 = MOD(YEAR4 100)

SIN COS ATAN etc trig functions (radians)

COMPUTE ANGLE = ATAN(VAL)

MEAN mean of arguments

COMPUTE AVSCORE=MEAN3(SCORE1 to SCORE30)

MISSING returns 1 if value is missing 0 if not

COMPUTE NO_AGE=MISSING(AGE)

INDEX returns 1st location of a string value

COMPUTE SPACE=INDEX(POSTCODE )

IF (SPACE = 0) SPACE=5

SUBSTR returns a substring of a string value

COMPUTE DISTRICT=SUBSTR(POSTCODE 1 SPACE-1)

DATEDMY creates a date value

COMPUTE DOB=DATEDMY(DAYBMONBYEARB)

XDATEYEAR extracts the year from a date variable

COMPUTE MOVE_YEAR=XDATEYEAR(MOVEDATE)

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 36: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3654

Module 5 Syntax for Calculations

30

S e s s i o n C S y n a x f o r C a l c u l a t i o n s COUNT command

Counts the number of occurrences of a given value across a range of

variables

COUNT target variable = variable list (value)

COUNT READ = SCOTSTIMESGUARD HERALD(3)

SCOTS TIMES GUARD HERALD READ

2 3 1 1 1

3 3 2 3 3

Also counts from a range of values or a list of individual values

COUNT PASSES = HIST GEOG MATHS (50 THRU 100)

HIST GEOG MATHS PASSES

32 63 41 1

60 74 32 2

67 54 63 3

lIST coand

LIST VARIABLES = AGE SEX HEIGHT

LIST CASES FROM 50 TO 100 BY 5

LIST VARIABLES = AGE HEIGHT CASES 20

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 37: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3754

Module 5 Syntax for Calculations

31

S e s s i on C S y n t ax f or

S P S S C omm an d s

IF command

IF (Logical expression) Target = Expression

IF (AGE GE 45) INCOME = INCOME + 2000

IF (A B GT 2 (C + 1)) B = 4

IF coand - Reationa Operators

gt= or GE greater than or equal to

lt= or LE less than or equal to

gt or GT greater than

lt or LT less than

= or EQ equal to

ltgt or NE not equal to

or ~

or not

IF coand - logica Operators

AND or amp

OR or |

NOT or not

(or ~)

IF coand use of bracketsIF ( HEIGHT GT 6 OR HEIGHT LT 5 AND CODE GT 20 )

RESULT=1

IF ( HEIGHT GT 6 OR (HEIGHT LT 5 AND CODE GT 20))

RESULT=1

SElECT IF coand

SELECT IF (AGE gt 45)

SELECT IF (SEX = F AND HEIGHT gt 66)SELECT IF (JOB EQ 2 OR JOB EQ 4)

Suppose we wish to select people over 6 feet and people under 5 feet

for our analysis We use the following SELECT IF commands

SELECT IF (HEIGHT GT 6)

SELECT IF (HEIGHT LT 5)

What happens

No-one is left

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 38: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3854

Module 5 Syntax for Calculations

32

S e s s i o n C S y n a x f o r C a l c u l a t i o n s TEmPORARY coand

Normally SELECT IF will throw away cases which are not selected

and they will be lost

The TEMPORARY command can prevent this

It causes unselected cases to be ltered out until the end of the next

procedure command (Frequencies Crosstabs etc)

TEMPORARY example

TEMPORARY

SELECT IF (SEX = F)

CROSSTABS TABLES = AGE BY HEIGHT

TEMPORARY

SELECT IF (SEX = M)

CROSSTABS TABLES = AGE BY HEIGHT

TEmPORARY aso affects te foowing coands not

just SElECT IF

COMPUTE RECODE IF COUNT

PRINT FORMATS MISSING VALUES

VARIABLE LABELS VALUE LABELS

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 39: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 3954

Module 5 Syntax for Calculations

33

S e s s i on C S y n t ax f or

S P S S C omm an d s

Filter command

FILTER can be used to work with subsets of data instead of

Temporary and Select IF

FILTER BY logical variable

Uses cases with variable value =1

Excludes all other cases from analysis

Doesnrsquot delete cases from working data le

FILTER OFF

Reinstates excluded cases

Filter command example

COMPUTE over60=(age ge 60)

FILTER BY over60

CROSSTABS INCOME BY AREA

FILTER OFF

CROSSTABS INCOME BY AREA

Te SAmPlE coand

To select an approximate percentage of casesSAMPLE 25

To obtain an exact number of cases

SAMPLE 60 FROM 200

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 40: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4054

Module 5 Syntax for Calculations

34

S e s s i o n C S y n a x f o r C a l c u l a t i o n s DO REPEAT command

DO REPEAT Q=Q1 TO Q5 N=00000

COMPUTE Q = N

END REPEAT

DO REPEAT - PRINT keywordDO REPEAT Q = Q1 TO Q5 R = R1 TO R5

COMPUTE Q = 0

COMPUTE R = 1

END REPEAT PRINT

PRINT will cause SPSS to print the commands generated by DO

REPEAT preceded by a + sign

DO REPEAT coand PRINT output+COMPUTE Q1 = 0

+COMPUTE R1 = 1

+COMPUTE Q2 = 0

+COMPUTE R2 = 1

+COMPUTE Q3 = 0

+COMPUTE R3 = 1

+COMPUTE Q4 = 0

+COMPUTE R4 = 1+COMPUTE Q5 = 0

+COMPUTE R5 = 1

These variables will be added to the active le in the order in which

they are created ie Q1 R1 Q2 R2 etc

This means that any future references such as Q1 TO Q5 will also

include R1 to R4 This may not be what was expected

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 41: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4154

Module 5 Syntax for Calculations

35

S e s s i on C S y n t ax f or

S P S S C omm an d s

DO IF - END IF commandsDO IF (X EQ 1)

RECODE Y (1=2) (2=1)

RECODE Z (3 5 7 = 1) (2 4 6 = 2)

END IF

DO IF - END IF coands instead of using IFs

IF (AGE GE 65) PENSION = SALARY2

IF (AGE GE 65) PASS = 1

IF (AGE GE 65) CODE = 0

equivalent to

DO IF (AGE GE 65)

COMPUTE PENSION = SALARY2

COMPUTE PASS = 1

COMPUTE CODE = 0

END IF

These 2 bits of code do the same thing The rst requires 3 passes of

the data the second only one pass and so is more efcient

DO IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE

COMPUTE Y = 2

END IF

DO IF - ElSE IF - ElSE - END IF coands

DO IF (X EQ 0)

COMPUTE Y = 1

ELSE IF (X LE 9)

COMPUTE Y = 9

ELSE

COMPUTE Y = 2

END IF

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 42: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4254

Module 5 Syntax for Calculations

36

S e s s i o n C S y n a x f o r C a l c u l a t i o n s

Session summaryThis session covered the commands

RECODEbull to group values COmPutE amp COuNt for calculations

IFbull SElECt IF SAmPlE tEmPORARy and FIltER used in various ways

to select subsets of data

DO REPEAtbull - END REPEAt DO IF - ElSE IF - ElSE - END IF

command groups for more complex calculations

The next session D is about running SPSS syntax remotely

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 43: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4354

Session D

Running Syntaxin Batches

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 44: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4454

Module 5 Starting with SPSS Syntax

38

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Production Facility

It is possible to issue SPSS a le of commands to do without having to be involved

interactively with SPSS It is known as running SPSS in Production ode also

called batch mode - since you sent the commands to SPSS in batches In the past

SPSS was run by issuing a set of commands to the program and the results of the

commands For example a le of SPSS commands may look like the following-

Figure 16 Collection of SPSS commands

get FILE=MspssworkCardiacsav

VaLue LabeLs

famhist N No family history of CHD Y CHD in family

smokes 0 Non smoker 1 Light smoker 2 Heavy smoker

c h d 0 N o C H D 1 C H D

B a s i c T a b l e s

temPorarY

VariabLe LabeLs c h d s m o k e s f a m h i s t

tabLes

F O R M A T = M I S S I N G ( )

F T O T A L $ t T o t a l

T A B L E S ( c h d + $ t )

B Y ( s m o k e s gt ( S T A T I S T I C S ) + $ t )

B Y ( f a m h i s t + $ t )

S T A T I S T I C S

c o u n t ( ( F 5 0 ) C o u n t )

c p c t ( ( P C T P A R E N 5 0 ) P e r c e n t s m o k e s f a m h i s t )

sort cases B Y f a m h i s t

sPLit iLe B Y f a m h i s t

graPH B A R ( G R O U P E D ) =C O U N T B Y s m o k e s B Y c h d

sPLit iLe O F F

inisH

If you want to follow this example type in these commands or paste themfrom dialog boxes into a syntax window and save them as batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 45: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4554

Module 5 Starting with SPSS Syntax

39

S e s s i onD R unni n g S y n t ax i nB a t c h e s

This le was mostly generated from using menus to produce a report quality table

and the matching bar charts The differences are that a DATASET NAmE command

was deleted after the GET command and a FINISh command - only used with the

production facility - was added to the end of the le to complete the batch le The

commands were saved to a le called batc1sps for use in the next section

The le path in the GET command should work in the training

rooms on the course However it may not work on your own PC

If you are not sure of the le path for a data le then it is a good

idea to generate the GET command using the menus

Running a job with SPSS Production facility

The Production Faciity is available through the Start menu in the same place as

the SPSS for Windows entry As the Production Facility starts up SPSS when a job

is run make sure you have closed any other SPSS sessions you have running

Figure 17 Start menu showing SPSS 14 for Windows and the SPSS 140

Production Mode Facility

Choose Fie gt Eit from SPSS if SPSS is still running

Then choose SPSS 140 Production mode Faciity from the Start menu

This will open the SPSS Production Facility By default SPSS will open a new job to

work with Below in Figure 18 you can see that the job is called SPSSjob1

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 46: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4654

Module 5 Starting with SPSS Syntax

40

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 18 SPSS 140 Production facility dialog box with SPSSJob1

At the bottom there is a Browsehellip button beside Foder for output space use it to

change the output directory

Alternatively you can type in the folder path if you know it It is a bad idea to put

your work in the same place SPSS is installed so make sure it is pointing to your

documents folder or another working folder

In order to run a syntax le we have to add it to the job le by clicking on the Addhellip

button and nding the saved syntax le

Either type the output path mspsswork into the Foder for Output box

Or use the Browse button to nd and select the spsswork folder in my

Docuents so output will go there

Use the Add button to nd and select the syntax le batc1sps

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 47: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4754

Module 5 Starting with SPSS Syntax

41

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 19 Attaching a syntax le to a job

Figure 20 Running the syntax window

Once the le is in the list of syntax les you can make simple changes by using the

Edithellip button to open and edit the le in the windows application Notepad

The Run button will run the syntax les in the job

Click on the Run button to run the example le

Notice once you have submitted the job you will get the following message and the

Production Facility will be minimized so you can get on with other things while SPSS

runs the job

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 48: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4854

Module 5 Starting with SPSS Syntax

42

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Figure 21 Running SPSS message

You may see SPSS start up and run the commands and close down when itrsquos

nished Donrsquot try to use SPSS while itrsquos working in batch mode you could cause

the job to fail Once the job has run the production facility will come to the front

again If you have asked for the output to be printed then it should start to be printed

at this point

Click on the Save button to save the job for another time Close the SPSS

Production Facility window

An output le with the results of the job will be stored in whatever working directory

has been specied

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 49: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 4954

Module 5 Starting with SPSS Syntax

43

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Output from an SPSS job

If you open the working directory there should be a new output le with the same

name as the job but with the spo extension SPSS associates with its output les

Here in Figure 26 the le is called SPSSJob1spo

Figure 22 The SPSSJob1spo le

Find and click on the output le to open the output in SPSS and see how the job went

Figure 23 End of the SPSSJob1spo output le

Figure 23 shows the end of the output window and the last bit of the log for the job

The log will also display any errors you might in running the job If something goes

wrong near the beginning of the job you may nd a lot of errors later on because

SPSS hasnrsquot processed the rst command

To nd out what went wrong with a job always start from thebeginning of the output le Correct the rst error and see if the

job will run then

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 50: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5054

Module 5 Starting with SPSS Syntax

44

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Output in html format - a web page

Instead of having to open the output results in SPSS it is possible to specify that you

want the output le exported to another format Since output viewer les can only be

opened in SPSS exporting output means you can share the results with colleagues

who do not have SPSS

The available formats for export are the same as the export options available for

output viewer les in an interactive SPSS session by choosing Fie gt ExporFormats available include htm word powerpoint and excel (tables only)

Open the saved job in SPSS Production Facility

Click on the Eport Options button and from the Eport drop down

menu choose to export the Output Docuent

Leave Fie Type as t and image format as jpeg to export the output to

a web page

Figure 28 Specifying html job output

Click Ok and then run the job again

Instead of a single le this time besides an spssjob1t le there will be a series

of sequential numbered lenames with jpg extension for each separate graph

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 51: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5154

Module 5 Starting with SPSS Syntax

45

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Figure 30 Output le SPSSJOB1HTM and graphics SPSSJOB10JPG SPSSJOB11

JPG amp SPSSJOB12JPG

Double click on the htm output le will open it in the default web browser

Figure 31 Viewing SPSSJOB1HTM in the web browser

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 52: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5254

Module 5 Starting with SPSS Syntax

46

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s Logging commands and SPSS Journal

W hv l hw wh h l v

p 3 a wll h p l sPss l kp jl ll h ly

wll ll h w h hy

by l h jl fl ll pjl kp y h p

lly cWwtppjl ll lw Y ll wh h jl

wh h l p

Figure 32 General Options session journal detail

To inspect the contents of the SPSS journal le exit from SPSS rst and then open

spssjnl in notepad or another text editor For example here is a section of the

journal le

Figure 33 Journal le detail

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 53: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5354

Module 5 Starting with SPSS Syntax

47

S e s s i onD R unni n g S y n t ax i nB a t c h e s

Try for yourself Review what youve learnt using SPSS own documentation

Chapter 9 from the SPSS 14 Brief Guide also covers getting started with SPSSbull

syntax as does chapter 13 of the SPSS 14 Base User Guide

Chapter 46 of the SPSS Base User Guide covers production modebull

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull

Page 54: Getting Started With SPSS Syntax

7292019 Getting Started With SPSS Syntax

httpslidepdfcomreaderfullgetting-started-with-spss-syntax 5454

48

S e s s i o n D R u n

n i n g S y n t a x i n B a t c h e s

Session summaryThis session covers

saving SPSS commands to a batch lebull

Setting up a job in SPSS Production facilitytbull

Running a job in SPSS Production facilitybull

Inspecting output from an SPSS jobbull