turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Modifying a Mediation SAS Program

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-29-2010 03:11 PM

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 */

x1=rannor(seed);

x2=x1/5+rannor(seed);

/* create an outcome that is correlated with both */

y = x1 + x2 + rannor(seed);

output;

end;

/* 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';

run;

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';

run;

/* 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;

output;

end;

end;

run;

/* 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;

run;

/* 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;

run;

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';

run;

/* step 4 - find the standard deviation of the mediated differences */

proc means data=boot_both;

var coeff_diff;

output out=standarderror std=se;

run;

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

data test;

merge both standarderror;

z=abs(coeff_diff)/se;

p=2*(1-probnorm(z));

proc print data=test;

title 'P is the p-value for whether there is significant mediation';

run;

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 */

x1=rannor(seed);

x2=x1/5+rannor(seed);

/* create an outcome that is correlated with both */

y = x1 + x2 + rannor(seed);

output;

end;

/* 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';

run;

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';

run;

/* 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;

output;

end;

end;

run;

/* 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;

run;

/* 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;

run;

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';

run;

/* step 4 - find the standard deviation of the mediated differences */

proc means data=boot_both;

var coeff_diff;

output out=standarderror std=se;

run;

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

data test;

merge both standarderror;

z=abs(coeff_diff)/se;

p=2*(1-probnorm(z));

proc print data=test;

title 'P is the p-value for whether there is significant mediation';

run;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-30-2010 09:19 AM

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

DATA tmp;

SET.trial;

y=outcome;

x1=sex;

x2=width;

RUN;

That's quick-n-dirty, duplicating your variables to the ones from the sample program.

DATA tmp;

SET

y=outcome;

x1=sex;

x2=width;

RUN;

That's quick-n-dirty, duplicating your variables to the ones from the sample program.