SPSS Syntax Examples
All examples are from an SPSS short course .
This page contains syntax examples for running the following SPSS commands:
*******Example Using Split File Command
sort cases by gender.
split file by gender.
execute.
DESCRIPTIVES
VARIABLES=educ
/STATISTICS=MEAN STDDEV MIN MAX .
split file off.
execute.
********Example Using Compute Command with Arithmetic Operators (averaging across columns)
compute meanhlth=(hlth1 + hlth2 + hlth3 + hlth4 + hlth5 + hlth6 + hlth7 + hlth8 +
hlth9)/9.
execute.
compute hlthtot = hlth1 + hlth2 + hlth3 + hlth4 + hlth5 + hlth6 + hlth7 + hlth8 +
hlth9.
execute.
********Example Using Compute Command with Statistical Functions (averaging across columns)
compute meanhlt2=mean(hlth1, hlth2, hlth3, hlth4, hlth5, hlth6, hlth7, hlth8, hlth9).
execute.
compute hlthtot2=sum(hlth1, hlth2, hlth3, hlth4, hlth5, hlth6, hlth7, hlth8, hlth9).
execute.
******Example Using the Recode Command Recoding into Same Variable and Different Variable
RECODE
work3 (1=2) .
EXECUTE .
RECODE
work3
(1=2) INTO work3b .
EXECUTE .
************Example Using the Aggregate Procedure (aggregating across rows)
AGGREGATE
/OUTFILE=*
/BREAK=gender
/age_1=MEAN(age)
/N_BREAK=N.
***********Example Using Select-If Statement with the Descriptives Procedure by Creating a Filter Variable
USE ALL.
COMPUTE filter_$=(age < 50).
VARIABLE LABEL filter_$ 'age < 50 (FILTER)'.
VALUE LABELS filter_$ 0 'Not Selected' 1 'Selected'.
FORMAT filter_$ (f1.0).
FILTER BY filter_$.
EXECUTE .
DESCRIPTIVES
VARIABLES=age
/STATISTICS=MEAN STDDEV MIN MAX .
***********Example Using Select-If Statement with the Descriptives Procedure with the TEMPORARY Command
temporary.
select if (age<50).
DESCRIPTIVES
VARIABLES=age
/STATISTICS=MEAN STDDEV MIN MAX .
***********Example Using Select-if Statement with Permanent Selection of Cases
FILTER OFF.
USE ALL.
SELECT IF(age < 50).
EXECUTE .
******Example Using the Do If-Else If-End If Structure
do if (occcat80=7 or occcat80=5 or occcat80=6).
compute subsidy=0.
else if (occcat80=1).
compute subsidy=salary*.1.
else if (occcat80=2).
compute subsidy=salary*.12.
else if (occcat80=3).
compute subsidy=salary*.08.
else if (occcat80=4).
compute subsidy=salary*.14.
end if.
execute.
*******Example Using the Do Repeat-End Repeat Structure
do repeat X=hlth1 to work9.
recode X (1=2) (2=1).
end repeat.
*********Example Concatenating Two SPSS System Files
ADD FILES /FILE=*
/FILE='A:\Census B.sav'.
EXECUTE.
**********Example Merging Two SPSS System Files Via Parallel Matching
MATCH FILES /FILE=*
/FILE='A:\Sorted Pcvote8x.sav'
/RENAME (age educ = d0 d1)
/BY id
/DROP= d0 d1.
EXECUTE.