/****************************************************************************************************************/ /* This SAS macro conducts a robust mediation analysis for single mediation model based on median regression /* robustmed(y, m, x, dataset, nboot, alphalev) /* where /* x: indepdent variable; /* y: dependent variable; /* m: mediating variable; /* dataset: the dataset contains variables x, y and m /* nboot: number of bootstrap samples /* alphalev: alpha level (or type I error rate) for 95% confidence interval /* /* Output of the marco: /* A and B are estimate of alpha and beta; /* AB is the estimate of mediation effect alpha*beta; /* ab_lb and ab_ub are the lower and upper bounds of the 95% confidence interval when alphalev=.05 (default value) /****************************************************************************************************************/ %macro robustmed(y, m, x, dataset, nboot=1000, alphalev = .05); /* median regression */ proc iml ; use &dataset; read all; one = J(nrow(&x),1); mcovariate = one || &x ; ycovariate = one || &m || &x; opt= { . 0 . 1 }; call lav(rc1, xr1, mcovariate, &m,,opt); /*regress m on x*/ call lav(rc2, xr2, ycovariate, &y,,opt); /*regress y on m and x*/ ab = xr1[2]||xr2[2]||xr1[2]*xr2[2]; CREATE abest from ab[COLNAME={a b ab}]; APPEND from ab; quit; /* generate bootstrap samples */ ods listing close; proc surveyselect data= &dataset out=bootsample seed = 1347 method = urs samprate = 1 outhits rep = &nboot; run; /* fit bootstrap samples using median regression */ proc iml; use bootsample; read all; one = J(nrow(&x),1); mcovariate = one || &x ; ycovariate = one || &m || &x; opt= { . 0 . 1 }; n = nrow(&x)/&nboot; r1 = 1; r2 = n; abboot = j(&nboot, 1, -99); do rep=1 to &nboot; call lav(rc1, xr1, mcovariate[r1:r2,], &m[r1:r2,],,opt); /*print out the estimates*/ call lav(rc2, xr2, ycovariate[r1:r2,], &y[r1:r2,],,opt); /*print out the estimates*/ abboot[rep, 1] = xr1[2]*xr2[2]; r1 = r1 + n; r2 = r2 + n; end; CREATE estimates from abboot[COLNAME={ab}]; APPEND from abboot; quit; /* generate summary statistics from bootstrap estimates */ %let a1 = %sysevalf(&alphalev/2*100); %let a2 = %sysevalf((1 - &alphalev/2)*100); /* creating confidence interval, percentile method; */ proc univariate data = estimates alpha = .05; var ab; output out=pmethod mean = r2hat pctlpts=&a1 &a2 pctlpre = ab pctlname = _lb _ub ; run; data output; merge abest pmethod; run; ods listing; proc print data = output; var a b ab ab_lb ab_ub; run; %mend; /*end of macro */ /********************************************************************************/ /* Illustration of the macro /* assume that you have imported the data file "heavytails.txt" /* into the SAS as the SAS dataset "heavytails". /********************************************************************************/ %robustmed(y, m, x, heavytails, nboot=1000, alphalev = .05)