### SPSS FAQ How can I perform a Sobel test on a single mediation effect in SPSS?

This page uses code provided by William Dudley PhD and Jose Benuzillo MA, University of Utah College of Nursing, 2/11/04. It estimates the percentage of the total effect that is mediated and the ratio of the indirect to the direct effect by using the Sobel test. It is for the simplest model in which there is only one mediator, one independent variable and one outcome variable, all of which are continuous variables.

We thank the authors of this program for very kindly sharing it with us and for allowing us to post it on our site. You can download a word document explaining their program here.

All of the code on this page can be found in this SPSS syntax file. We use this example SPSS data file for running their program.

To begin, we indicate which of our variables are the dependent, independent, and mediator variables. We then run three regression models representing the paths of interest for the Sobel test. From each regression, we create and save a dataset. We will only show the output generated by the Report sections of code.

NOTE: When using this code, you can use the first three lines to indicate which of your variables play which role in the model and then run the rest of the code without making any more changes.  We have included all of the code in this page to illustrate the steps in the program. But you can certainly just download the syntax, change the first three lines, and be ready to run all the analysis code!

compute indepvar	= Pain .    /*your IV*/
compute depenvar	= Depress.  /*your DV*/
compute mediator	= Function. /*you MEDIATOR*/
exe.

REGRESSION
/MISSING LISTWISE
/STATISTICS COEFF OUTS R ANOVA
/CRITERIA=PIN(.05) POUT(.10)
/NOORIGIN
/DEPENDENT mediator
/METHOD=ENTER indepvar
/OUTFILE=COVB('reg1.sav').

REGRESSION
/MISSING LISTWISE
/STATISTICS COEFF OUTS R ANOVA
/CRITERIA=PIN(.05) POUT(.10)
/NOORIGIN
/DEPENDENT depenvar
/METHOD=ENTER indepvar mediator
/OUTFILE=COVB('reg2.sav').

REGRESSION
/MISSING LISTWISE
/STATISTICS COEFF OUTS R ANOVA
/CRITERIA=PIN(.05) POUT(.10)
/NOORIGIN
/DEPENDENT depenvar
/METHOD=ENTER  indepvar
/OUTFILE=COVB('reg3.sav').


Next, these three regression datasets are combined after indicating which is which. A subset of observations are kept corresponding to estimates, standard errors, degrees of freedom, and p-values


get file='reg1.sav'.
compute reg=1.
exe.
SAVE OUTFILE='reg1.sav'
/COMPRESSED.

get file='reg2.sav'.
compute reg=2.
exe.
SAVE OUTFILE='reg2.sav'
/COMPRESSED.

get file='reg3.sav'.
compute reg=3.
exe.
SAVE OUTFILE='reg3.sav'
/COMPRESSED.

/file=='reg2.sav'
/file=='reg3.sav'
/keep=  reg DEPVAR_ ROWTYPE_ VARNAME_  CONST_ indepvar mediator.
exe.

sel if rowtype_ = 'EST' or rowtype_ = 'SE' or rowtype_ = 'DFE'
or rowtype_ = 'SIG'.
exe.


Next, the data is reshaped from wide to long.


numeric a.
numeric sa.
numeric siga.
numeric b.
numeric sb.
numeric sigb.
numeric df.
numeric t.
numeric st.
numeric sig.
if reg = 1 and rowtype_ = 'EST' a		= indepvar.
if reg = 1 and rowtype_ = 'SE'  sa		= indepvar.
if reg = 1 and rowtype_ = 'SIG' siga	= indepvar.
if reg = 2 and rowtype_ = 'EST' b		= mediator.
if reg = 2 and rowtype_ = 'SE'  sb		= mediator.
if reg = 2 and rowtype_ = 'SIG' sigb	= mediator.
if reg = 2 and rowtype_ = 'DFE' df		= mediator.
if reg = 2 and rowtype_ = 'EST' tprime	= indepvar.
if reg = 3 and rowtype_ = 'EST' t		= indepvar.
if reg = 3 and rowtype_ = 'SE'  st		= indepvar.
if reg = 3 and rowtype_ = 'SIG' sig		= indepvar.
exe.

compute regx=1.
exe.

aggregate outfile 'sobel.sav'
/break regx
/a = max(a)
/sa=max(sa)
/siga=max(siga)
/b=max(b)
/sb=max(sb)
/sigb=max(sigb)
/df=max(df)
/sig=max(sig)
/tprime = max(tprime)
/t=max(t)
/st=max(st).
get file 'sobel.sav'.
format a b tprime t (F8.4).

compute ab = a*b.
compute ttprime = t-tprime.

format ab ttprime  (F8.4).

exe.


From these values, our statistics of interest are calculated, formatted, and labeled.


compute sobel= ttprime/(sqrt (( (b*b)*(sa*sa) ) + ( (a*a)*(sb*sb) ))).
compute absobel= abs(sobel).
compute p_val=2*(1-cdfnorm(absobel)).
compute t1=(t-(a*b)).
compute toteff=(a*b/((a*b)+t1)).
compute ratio=((a*b)/t1).
compute toteff = 100* toteff.
compute goodman = ttprime/sqrt(((b*b)*(sa*sa))+((a*a)*(sb*sb))+((sa*sa)*(sb*sb))).
compute absgood = abs(goodman).
compute goodman2 = ttprime/sqrt(((b*b)*(sa*sa))+((a*a)*(sb*sb))-((sa*sa)*(sb*sb))).
compute absgood2 = abs(goodman2).
compute p_val2 = 2*(1-cdfnorm(absgood)).
compute p_val3 = 2*(1-cdfnorm(absgood2)).
exe.

format  p_val p_val2 p_val3
sig siga sigb
sobel goodman goodman2
toteff ratio st sb sa(F8.6).
exe.

variable label
sig    'P value of c'
siga   'P value of a'
sigb   'P value of b'
a      'Reg coeff for the association between IV and MEDIATOR'
sa     'Standard error of a'
b      'Reg coeff for the association between the MEDIATOR and IV on DV'
sb     'Standard error of b'
df     'Degrees of freedom'
t      'Reg coeff for the association between IV and DV'
st     'Standard error of c'
sobel  'Sobel'
p_val  'P value'
goodman 'Goodman test'
p_val2  'P value'
goodman2 'GoodmanII test'
p_val3 'P Value'
toteff 'Percentage of the total effect that is mediated'
ratio  'Ratio of the indirect to the direct effect'.
exe.


Results are presented with Report.


Report
/FORMAT= CHWRAP(ON) PREVIEW(OFF) CHALIGN(BOTTOM)  UNDERSCORE(ON)
ONEBREAKCOLUMN(OFF) CHDSPACE(1)  SUMSPACE(0)  AUTOMATIC  NOLIST
BRKSPACE(0)
PAGE(1) MISSING'.' LENGTH(1, 59) ALIGN(LEFT) TSPACE(1) FTSPACE(1)
MARGINS(1,110)
/TITLE=
LEFT 'Regression Analysis Results'
RIGHT 'Page )PAGE'
/VARIABLES
t	(VALUES)  (RIGHT)  (OFFSET(0)) (9)
st	(VALUES)  (RIGHT)  (OFFSET(0)) (7)
sig	(VALUES)  (RIGHT)  (OFFSET(0)) (7)
a	(VALUES)  (RIGHT)  (OFFSET(0)) (9)
sa	(VALUES)  (RIGHT)  (OFFSET(0)) (7)
siga	(VALUES)  (RIGHT)  (OFFSET(0)) (7)
b	(VALUES)  (RIGHT)  (OFFSET(0)) (9)
sb	(VALUES)  (RIGHT)  (OFFSET(0)) (7)
sigb	(VALUES)  (RIGHT)  (OFFSET(0)) (7)
/BREAK (TOTAL)  (SKIP(1)) /SUMMARY
SUM( t) SKIP(1) SUM( st ) SUM( sig ) SUM( a ) SUM( sa ) SUM( siga ) SUM( b ) SUM( sb ) SUM( sigb )   '' .

Report
/FORMAT= CHWRAP(ON) PREVIEW(OFF) CHALIGN(BOTTOM)  UNDERSCORE(ON)
ONEBREAKCOLUMN(OFF) CHDSPACE(1)  SUMSPACE(0)  AUTOMATIC  NOLIST
BRKSPACE(0)
PAGE(2) MISSING'.' LENGTH(1, 59) ALIGN(LEFT) TSPACE(1) FTSPACE(1)
MARGINS(1,100)
/TITLE=
LEFT 'Mediation Analysis Results'
RIGHT 'Page )PAGE'
/VARIABLES
sobel	(VALUES)  (RIGHT)  (OFFSET(0)) (10)
p_val	(VALUES)  (RIGHT)  (OFFSET(0)) (10)
toteff	(VALUES)  (RIGHT)  (OFFSET(0)) (10)
ratio	(VALUES)  (RIGHT)  (OFFSET(0)) (10)
goodman	(VALUES)  (RIGHT)  (OFFSET(0)) (10)
p_val2	(VALUES)  (RIGHT)  (OFFSET(0)) (10)
goodman2	(VALUES)  (RIGHT)  (OFFSET(0)) (10)
p_val3	(VALUES)  (RIGHT)  (OFFSET(0)) (10)
/BREAK (TOTAL) (SKIP(1)) /SUMMARY
SUM( sobel) SKIP(1) SUM( p_val ) SUM( toteff ) SUM( ratio) SUM( goodman ) SUM( p_val2 ) SUM( goodman2 ) SUM( p_val3 ) ''.

Report
/FORMAT= CHWRAP(ON) PREVIEW(OFF) CHALIGN(BOTTOM)  UNDERSCORE(ON)
ONEBREAKCOLUMN(OFF) CHDSPACE(1)  SUMSPACE(0)  AUTOMATIC  NOLIST
BRKSPACE(0)
PAGE(3) MISSING'.' LENGTH(1, 59) ALIGN(LEFT) TSPACE(1) FTSPACE(1)
MARGINS(1,45)
/TITLE=
LEFT 'Percent Mediated'
RIGHT 'Page )PAGE'
/VARIABLES
toteff	(VALUES)  (RIGHT)  (OFFSET(0)) (10)
ratio	(VALUES)  (RIGHT)  (OFFSET(0)) (10)
/BREAK (TOTAL) (SKIP(1)) /SUMMARY
SUM( toteff ) SKIP(1) SUM( ratio) ''.



The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.