BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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;
1 REPLY 1
Doc_Duke
Rhodochrosite | Level 12
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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 620 views
  • 0 likes
  • 2 in conversation