libname sobel 'c:\temp'; /* Change path */ data test; set sobel.hsbdemo; /* Change data name */ iv= math ; /* your IV */ mediator= read ; /*your MEDIATOR */ dv= science ; /* 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 Results'; proc report data=sobel nowd headline headskip; column a sa b sb te ab sobel p_value toteff ratio; define a /sum width=8 'Reg Coeff IV and MEDIATOR (a)'; define sa /sum width=8 'Standard error of a'; define b /sum width=8 'Reg Coeff MEDIATOR and IV on DV (b)'; define sb /sum width=8 'Standard error of b'; define te /sum width=8 'Reg Coeff IV and DV (total effect)'; define ab / computed 'Indirect Effect (ab)'; define sobel /computed 'Sobel Test Statistic'; define p_value /computed 'Sobel p-value'; define toteff /computed 'Proportion of Total Effect Mediated'; define ratio /computed 'Ratio of Indirect to Direct Effect'; compute ab; ab = a.sum*b.sum; endcomp; compute sobel; sobel=(a.sum*b.sum)/sqrt((b.sum**2*sa.sum**2)+ (a.sum**2*sb.sum**2)); endcomp; compute p_value; P_value = 2*(1-CDF('NORMAL',sobel)); endcomp; compute toteff; toteff = (a.sum*b.sum)/te.sum; endcomp; compute ratio; ratio =a.sum*b.sum/(te.sum-a.sum*b.sum); endcomp; run;