DATA Step, Macro, Functions and more

Modifying a Mediation SAS Program

Posts: 0

Modifying a Mediation SAS Program

I am a very new user to SAS and received this program who I no longer have contact with. I could use your help in helping me modify it.

My understanding is that the table tmp is being randomly generated with known mediation of the outcome y and variable x1 by variable x2. Then, the table boot is a random sampling of the real dataset (but is currently creating a random data set as a placeholder). I don't know how to integrate my dataset into this program. My dataset is now listed as work.trial and has the variables outcome, sex, and width (the corresponding variables of y, x1, and x2 respectively). If anyone could help me "load" my data into this program, I would be grateful.


/* make up some data that has confounding in it */

data tmp;
retain seed 2882727;
do obs=1 to 80;
/* create correlated predictor variables */

/* create an outcome that is correlated with both */
y = x1 + x2 + rannor(seed);


/* now the question is "is the assocaition between Y and X1 mediated by X2?" */

proc reg data=tmp outest=unadjusted;
model y = x1;
title 'Unadjusted Analysis';
proc reg data=tmp outest=adjusted;
model y = x1 x2;
title 'Adjusted analysis';
data unadjusted2;
set unadjusted;

coeff_unadj = x1;

keep coeff_unadj; run;
data adjusted2;
set adjusted;

coeff_adj = x1;

keep coeff_adj;
data both;
merge unadjusted2 adjusted2;

coeff_diff = coeff_unadj - coeff_adj;
proc print data=both;
title 'Unadjusted and adjusted coefficients and difference';

/* the question is can we get the standard error to get test if it is significant mediation

need to bootstrap */

/* step 1 create a file to pick out the bootstrapped samples - let's do 500 bootstraps */

data boot;
retain seed 828271;
do rep=1 to 500;
/* now do the same number of observations in the dataset */
do i=1 to 80;
obs = int(ranuni(seed)*80)+1;

/* step 2 - now merge with the real data and get the randomly sampled observations out */

proc sort data=boot;
by obs;
data boot2;
merge boot (in=inboot) tmp;
by obs;

if inboot;
proc sort data=boot2;
by rep obs;

/* step 3 - now do the regressions and save the 500 differences */

proc reg noprint data=boot2 outest=boot_unadj;
model y = x1;
by rep;

proc reg noprint data=boot2 outest=boot_adj;
model y = x1 x2;
by rep;

data boot_unadj2;
set boot_unadj;

coeff_unadj = x1;

keep rep coeff_unadj; run;
data boot_adj2;
set boot_adj;

coeff_adj = x1;

keep rep coeff_adj;
data boot_both;
merge boot_unadj2 boot_adj2;
by rep;
coeff_diff = coeff_unadj - coeff_adj;
proc print data=boot_both;
title 'Listing of the 500 estimates of the difference with mediation';

/* step 4 - find the standard deviation of the mediated differences */
proc means data=boot_both;
var coeff_diff;
output out=standarderror std=se;

/* step 5 - now combine with the real estimated difference and do the test of whether it is significant */

data test;
merge both standarderror;


proc print data=test;
title 'P is the p-value for whether there is significant mediation';
Trusted Advisor
Posts: 2,113

Re: Modifying a Mediation SAS Program

Assuming the sample program runs as intended, you just need to replace the first data step with something like

DATA tmp;
SET .trial;

That's quick-n-dirty, duplicating your variables to the ones from the sample program.
Ask a Question
Discussion stats
  • 1 reply
  • 2 in conversation