UCLA Academic Technology Services HomeServicesClassesContactJobs
Search

SAS Code Fragments
Sobel Test of Mediated Effects

This program 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, written by William Dudley PhD and Jose Benuzillo MA, University of Utah College of Nursing, 12/13/2002.  We thank the authors of this program very kindly for sharing it with us for posting at our site.

libname sobel 'c:\temp';   /* Change path */

data test;
	set sobel.employeedata;  /* Change data name */
	iv= educ ;           /* your IV */
	mediator= prevexp ;  /*your MEDIATOR */
	dv= salary ;         /* your DV */
run;

/***********************************************************/
/* Regression analysis:   IV predicting MEDIATOR           */
/*                        IV and MEDIATOR predicting DV    */
/*                        IV predicting DV                 */
/***********************************************************/

proc reg data=test;
  model mediator=iv;
  model dv=iv mediator;
  model dv=iv;
  ods output ParameterEstimates=reg1;     /* Saving estimates into a data set */     
run;
quit;

/* Here we select only the statistics required to compute the Sobel test */
 
data sobel;
	set reg1;
	if model = 'MODEL1' and variable = 'iv' then a  = Estimate;
	if model = 'MODEL1' and variable = 'iv' then sa = StdErr;
	if model = 'MODEL2' and variable = 'mediator' then b = Estimate;
	if model = 'MODEL2' and variable = 'mediator' then sb = StdErr;
	if model = 'MODEL3' and variable = 'iv' then te = Estimate;
run;

/**********************************************************************************/
/*   Here we compute the sobel test,calculate the percentage of the total         */
/*   effect that is mediated, calculate the ratio of the indirect to the direct   */
/*   effect and create the final report                                           */
/*  Further information about these tests, may be found in MacKinnon & Dwyer      */
/*  (1993) Estimating mediated effects in prevention studies                      */
/**********************************************************************************/

options  nodate nonumber;
title1 'Mediation analysis Ressults';
proc report data=sobel nowd headline headskip;
   column a sa b sb te sobel p_value toteff ratio;
   define a  /sum width=8 'Reg Coeff IV and MEDIATOR';
   define sa /sum width=8 'Standard error of a';
   define b  /sum width=8 'Reg Coeff MEDIATOR and IV on DV';
   define sb /sum width=8 'Standard error of b';
   define te /sum width=8 'Reg Coeff IV and DV';	
   define sobel   /computed;
   define p_value /computed;
   define toteff  /computed;
   define ratio   /computed; 
        compute sobel;
   sobel = (a.sum*b.sum)/sqrt (( (b.sum*b.sum)*(sa.sum*sa.sum) ) + ( (a.sum*a.sum)*(sb.sum*sb.sum) ))  ;
   endcomp;
   compute p_value;
        P_value = 2*(1-CDF('NORMAL',sobel));
   endcomp;
   compute toteff;
   	toteff =(a.sum*b.sum/((a.sum*b.sum)-te.sum)); 
   endcomp;
   compute ratio;
        ratio =((a.sum*b.sum)/te.sum);
   endcomp; 
run;

 

How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


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