BookmarkSubscribeRSS Feed
desireatem
Pyrite | Level 9

Hello I wish to call the following macro and run the analysis below. The macro runs well but the last part to simulate a data and runs does not work. Any help?

The problem is from the data test (marked bold below) , no problem with the macro.

/*** I want to used the macro called indirect*******/

/* Macro begins*/;

%macro indirect(data=,y=,x=,m=,c=0,boot=1000,conf=95,percent=0,bc=1,bca=0, normal=0,contrast=0);

proc iml;

use &data;

read all var{&y &x &m} into dd;

nm={&y &x &m};

xx=(dd = .);xx=xx[,+];

j=1;do i = 1 to nrow(dd);if xx[i,1]=0 then;do;dd[j,]=dd[i,];j=j+1;end;end;

dd=dd[1:j-1,];

nm = nm`;

n = nrow(dd);

nv = ncol(dd);

nc = &c;

con=j(n,1,1);

dt2 = dd;

dt = dd;

resid = j(n,(nv-nc),0);

info = j((2*(nv-nc-2)+1),(2*(nv-nc-2)+1),0);

imat = j(ncol(info),4,1);

imat[1:(nv-nc-2),1]=(2:(nv-nc-1))`;

imat[1:(nv-nc-2),3]=(2:(nv-nc-1))`;

imat[(nv-nc-1):(ncol(info)-1),2]=(2:(nv-nc-1))`;

imat[(nv-nc-1):(ncol(info)-1),4]=(2:(nv-nc-1))`;

imat[(nv-nc-1):(ncol(info)-1),1]=j((nv-nc-2),1,(nv-nc));

imat[(nv-nc-1):(ncol(info)-1),3]=j((nv-nc-2),1,(nv-nc));

imat[ncol(info),1:4]={1 1 1 1};

imat[ncol(info),1]=nv-nc;

imat[ncol(info),3]=nv-nc;

bzx = j(nv-2-nc,1,0);

bzxse = j(nv-2-nc,1,0);

b=j((nv-1-nc),(nv-1-nc),0);

cname={"C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10", "C11", "C12", "C13", "C14", "C15", "C16"};

cname=cname//{"C17", "C18", "C19", "C20", "C21", "C22", "C23", "C24", "C25", "C26", "C27", "C28", "C29"};

cname=cname//{"C30", "C31", "C32", "C33", "C34", "C35", "C36", "C37", "C38", "C39", "C40", "C41", "C42"};

cname=cname//{"C43", "C44", "C45"};

p0 = -0.322232431088;

p1 = -1;

p2 = -0.342242088547;

p3 = -0.0204231210245;

p4 = -.0000453642210148;

q0 = 0.0993484626060;

q1 = 0.588581570495;

q2 = 0.531103462366;

q3 = 0.103537752850;

q4 = 0.0038560700634;

conf=round(&conf);

lowalp = 0.5*(1-(conf/100));

upalp = 0.5*(1+(conf/100));

zbca = lowalp//upalp;

btn = 1;

if (&boot > 999) then;

  do;

  btn = floor(&boot/1000)*1000;

  end;

blowp = floor(lowalp*btn);

if (blowp < 1) then;

  do;

  blowp = 1;

  end;

bhighp = floor((upalp*btn)+1);

if (bhighp > btn) then;

  do;

  bhighp = btn;

  end;

indeff = j((n+1+btn),(nv-1-nc),0);

do d = 1 to (n+1+btn);

  if (d = (n+2)) then;

    do;

  dt = dt2;

  con = j(n,1,1);

  end;

  if (d > 1) then if (d < (n+2)) then;

    do;

    if (d = 2) then;

   do;

   con = j((n-1),1,1);

   dt = dt2[2:n,];

   end;

  if (d = (n+1)) then;

   do;

      dt = dt2[1:(n-1),];

   end;

    if (d > 2) then if (d < (n+1)) then;

   do;

      dt = dt2[1:(d-2),]//dt2[(d:n),];

      end;

  end;

  if (d > (n+1)) then;

    do;

   do nn = 1 to n;

      v = int(ranuni(0)*n)+1;

   dt[nn,1:nv]=dt2[v,1:nv];

   end;

  end;

x = dt[,2];

m = dt[,3:(nv-nc)];

y = dt[,1];

xz = dt[,2:nv];

xo = con||x;

if (nc > 0) then;

   do;

   c = dt[,(nv-nc+1):nv];

   xo = xo||c;

   end;

do k = 3 to (nv-nc);

  ytmp = dt[,k];

  bzxt = inv(xo`*xo)*xo`*ytmp;

  bzx[(k-2),1]=bzxt[2,1];

  if (d = 1) then;

    do;

  resid[,(k-1)]=ytmp-(xo*bzxt);

  mse = sum((ytmp-(xo*bzxt))##2)/(n-2-nc);

  olscm = (mse*inv(xo`*xo));

  bzxse[(k-2),1]=sqrt(olscm[2,2]);

  end;

end;

if (d = 1) then;

  do;

  if (nc > 0) then;

    do;

    cnt = dd[,(nv-(nc-1)):nv];

    xo = con||x||cnt;

  end;

  if (nc = 0) then;

    do;

    xo = con||x;

  end;

  byx = inv(xo`*xo)*xo`*y;

  mse = sum((y-(xo*byx))##2)/(n-2-nc);

  olscm = (mse*inv(xo`*xo));

  byxse = sqrt(olscm[2,2]);

  byx = byx[2,1];

  end;

xzo = con||xz;

byzx = inv(xzo`*xzo)*xzo`*y;

byzx2 = byzx[3:(nv-nc),1];

if (d = 1) then;

  do;

  resid[,ncol(resid)]=y-(xzo*byzx);

  mse = sum((y-(xzo*byzx))##2)/(n-nv);

  covmat = mse*inv(xzo`*xzo);

  olscm = vecdiag(covmat);

  sse = mse*(n-nv);

  sst = sum((y-(sum(y)/n))##2);

  r2 = 1-(sse/sst);

  ar2 = 1-(mse/(sst/(n-1)));

  fr = ((n-nv)*r2)/((1-r2)*ncol(xz));

  pfr = 1-probf(fr,ncol(xz),(n-nv));

  if (nc > 0) then;

    do;

    bcon = byzx[(nv-nc+1):nv,1];

  bconse = sqrt(olscm[(nv-nc+1):nv,1]);

    end;

  byzx2se = sqrt(olscm[3:(nv-nc),1]);

  cprime = byzx[2,1];

  cprimese=sqrt(olscm[2,1]);

end;

indeff2 = (bzx#byzx2);

zs = (bzx/bzxse)#(byzx2/byzx2se);

temp = t(sum(indeff2)//indeff2);

indeff[d,]=temp;

if (d = 1) then;

  do;

  vs = nm[1:(nv-nc),1];

  rn = {"DV = " "IV = " "MEDS = "};

  print "Dependent, Independent, and Proposed Mediator Variables";

  print vs [rowname = rn];

  if (nc > 0) then;

    do;

  vs = nm[(nv-nc+1):nv,1];

  print "Statistical Controls";

  rn = {"CONTROLS="};

  print vs [rowname = rn];

  end;

  print "Sample size";

  print n;

  nms = nm[3:(nv-nc),1];

  te = bzx/bzxse;

  df = n-2-nc;

  p = 2*(1-probt(abs(te),df));

  bzxmat = bzx||bzxse||te||p;

  b[2:(nv-1-nc),1]=bzx;

  se2 = bzxse#bzxse;

  cnm = {"Coeff" "se" "t" "p"};

  print "IV to Mediators (a paths)";

  print bzxmat [rowname = nms colname = cnm format = 9.4];

  te = byzx2/byzx2se;

  df = n-nv;

  p = 2*(1-probt(abs(te),df));

  byzx2mat=byzx2||byzx2se||te||p;

  print "Direct Effects of Mediators on DV (b paths)";

  print byzx2mat [rowname = nms colname = cnm format = 9.4];

  te=byx/byxse;

  df = n-2-nc;

  p=2*(1-probt(abs(te),df));

  byxmat = byx||byxse||te||p;

  xnm=nm[2,1];

  print "Total effect of IV on DV (c path)";

  print byxmat [rowname = xnm colname = cnm format = 9.4];

  te=cprime/cprimese;

  df = n-nv;

  p=2*(1-probt(abs(te),df));

  cprimmat = cprime||cprimese||te||p;

  print "Direct Effect of IV on DV (c' path)";

  print cprimmat [rowname = xnm colname = cnm format = 9.4];

  if (nc > 0) then;

    do;

  df = n-nv;

  nms = nm[(nv-nc+1):nv,1];

  te=bcon/bconse;

  p=2*(1-probt(abs(te),df));

  bconmat = bcon||bconse||te||p;

  print "Partial Effect of Control Variables on DV";

  print bconmat [rowname = nms colname = cnm format = 9.4];

  end;

  dvms=r2||ar2||fr||ncol(xz)||(n-nv)||pfr;

  print "Fit Statistics for DV Model";

  cnm = {"R-sq" "adj R-sq" "F" "df1" "df2" "p"};

  print dvms [colname = cnm format = 9.4];

  if (&normal =^ 0) then;if(&c = 0) then;do;

    do;

      bmat=j((nv-nc),(nv-nc),0);

      bmat[2:(nv-nc-1),1]=bzx;

   bmat[(nv-nc),2:(nv-nc-1)]=byzx2`;

      bmat[(nv-nc),1]=cprime;

      imbinv = inv(i(ncol(bmat))-bmat);

   imbtinv = inv(i(ncol(bmat))-bmat`);

      resid[,1]=x-(x[+]/n);

   psi = (resid`*resid)/(n-1);

   invpsi = inv(psi);

   ibpsiib = imbinv*psi*imbtinv;

   do ic = 1 to ncol(info);

     do ic2 = 1 to ncol(info);

   info[ic,ic2]=(n-1)*((imbinv[imat[ic2,4],imat[ic,1]]*imbinv[imat[ic,2],imat[ic2,3]])+(ibpsiib[imat[ic2,4],imat[ic,2]]*invpsi[imat[ic,1],imat[ic2,3]]));

        end;

   end;

   varcov=inv(info);

   varcov=varcov[1:(2*(nv-nc-2)),1:(2*(nv-nc-2))];

   ses = vecdiag(varcov);

   avar = ses[1:nrow(bzxse),1];

   bvar = ses[(nrow(bzxse)+1):nrow(ses),1];

   if ((nv-nc-2) > 1) then;do;if(&contrast = 1) then;do;

     prws=j(((nv-nc-2)*(nv-nc-3)/2),1,0);

  prwse=prws;

  kk=1;

  do ic = 1 to (nv-nc-3);

   do ic2 = (ic+1) to (nv-nc-2);

     vf2=((byzx2[ic,1]##2)*varcov[ic,ic])-(2*byzx2[ic,1]*byzx2[ic2,1]*(varcov[ic,ic2]));

  vf2=vf2+((byzx2[ic2,1]##2)*varcov[ic2,ic2])+((bzx[ic,1]##2)*(bvar[ic,1]));

  vf2=vf2-(2*bzx[ic,1]*bzx[ic2,1]*covmat[(2+ic),(2+ic2)])+((bzx[ic2,1]##2)*(bvar[ic2,1]));

  cnt=indeff2[ic,1]-indeff2[ic2,1];

  prws[kk,1]=cnt;

  prwse[kk,1]=sqrt(vf2);

  kk=kk+1;

   end;

     end;

   cnam2=cname[1:(kk-1),1];

   end;end;

   dermat=byzx2//bzx;

   totse=sqrt((dermat)`*varcov*dermat);

   specse = sqrt((byzx2#byzx2)#(avar)+(bzx#bzx)#(bvar));

   specse = totse//specse;

   indsum=indeff2[+];

      specz = (indsum//indeff2)/specse;

   ind22 = indsum//indeff2;

   nms = {"TOTAL"}//nm[3:(nv-nc),1];

   if ((nv-nc-2) > 1) then;do;if(&contrast = 1) then;do;

     ind22 = ind22//prws;

  specse = specse//prwse;

  specz2 = prws/prwse;

  specz = specz//specz2;

  nms = nms//cnam2;

  end;end;

      pspec = 2*(1-probnorm(abs(specz)));

      spec = ind22||specse||specz||pspec;

      cnm = {"Effect" "se" "Z" "p"};

      print "*****************************************************";

      print "NORMAL THEORY TESTS FOR INDIRECT EFFECTS";

      print "Indirect Effects of IV on DV through Mediators (ab paths)";

      print spec [rowname = nms colname = cnm format = 9.4];

    end;end;

  end;

end;

if (btn > 1) then;do;

  nms = {"TOTAL"}//nm[3:(nv-nc),1];

  if (nv-nc-2) > 1 then do;if (&contrast = 1) then do;

     crst=j((n+1+btn),((nv-nc-2)*(nv-nc-3)/2),0);

  kk=1;

  do ic = 2 to (nv-nc-2);

    do ic2 = (ic+1) to (nv-nc-1);

      crst[,kk]=indeff[,ic]-indeff[,ic2];

  kk=kk+1;

    end;

  end;

     indeff = indeff||crst;

  cnam2=cname[1:(kk-1),1];

  nms = nms//cnam2;

  end;

  end;

lvout = indeff[2:(n+1),];

tdotm = lvout[+,]/n;

tm = j(n,ncol(lvout),1)*diag(tdotm);

topa=(((n-1)/n)*(tm-lvout))##3;

topa=topa[+,];

bota =((((n-1)/n)*(tm-lvout))##2);

bota=bota[+,];

bota=6*sqrt(bota##3);

ahat = topa/bota;

indsam = indeff[1,]`;

boot = indeff[(n+2):nrow(indeff),];

mnboot = (boot[+,]/btn)`;

xt=boot-j(btn,1)*boot[:,];

cv=(xt`*xt)/btn;

se=sqrt(vecdiag(cv));

  create bootstp from boot [colname='indirect'];

  append from boot;

  nnn = j(1,ncol(indeff),-999);

  boot = nnn//boot;

  do e = 1 to (ncol(indeff));

  do i = 2 to (btn+1);

    ix = boot[i,e];

  do k = i to 2 by -1;

   k2 = k;

   if (boot[(k-1),e] > ix) then;

     do;

  boot[k,e]=boot[(k-1),e];

  end;

   else;

   if (boot[(k-1),e] <= ix) then;

     do;

  goto stpit;

  end;

  end;

  stpit:

  boot[k2,e]=ix;

    end;

    end;

  boot = boot[2:(btn+1),];

xp=j((nrow(mnboot)+2),1,0);

do i = 1 to (nrow(mnboot)+2);

  if (i <= nrow(mnboot)) then;

    do;

    pv = (boot[,i] < indsam[i,1]);

  pv = pv[+,]/btn;

    end;

  else;

    pv = zbca[(i-nrow(mnboot)),1];

  p=pv;

  if (pv > 0.5) then;

    do;

  p = 1-pv;

  end;

  y5 = sqrt(-2*log(p));

  xp[i,1]=y5+((((y5*p4+p3)*y5+p2)*y5+p1)*y5+p0)/((((y5*q4+q3)*y5+q2)*y5+q1)*y5+q0);

  if (pv <= 0.5) then;

    do;

  xp[i,1]=-xp[i,1];

  end;

end;

bbb = nrow(mnboot);

zz = xp[1:bbb,1];

zlo = zz + ((zz+xp[(bbb+1),1])/(1-ahat`#(zz+xp[(bbb+1),1])));

zup = zz + ((zz+xp[(bbb+2),1])/(1-ahat`#(zz+xp[(bbb+2),1])));

ahat = 0;

zlobc = zz + ((zz+xp[(bbb+1),1])/(1-ahat`#(zz+xp[(bbb+1),1])));

zupbc = zz + ((zz+xp[(bbb+2),1])/(1-ahat`#(zz+xp[(bbb+2),1])));

zlo = probnorm(zlo);

zup = probnorm(zup);

zlobc = probnorm(zlobc);

zupbc = probnorm(zupbc);

blow = int(zlo*(btn+1));

bhigh = int(zup*(btn+1))+1;

blowbc = int(zlobc*(btn+1));

bhighbc = int(zupbc*(btn+1))+1;

lowbca = j(nrow(blow),1,0);

upbca = lowbca;

do i = 1 to nrow(blow);

  if (blow[i,1] < 1) then;

    do;

  blow[i,1]=1;

  end;

  lowbca[i,1]=boot[blow[i,1],i];

  if (bhigh[i,1] > btn) then;

    do;

  bhigh[i,1]=btn;

  end;

  upbca[i,1]=boot[bhigh[i,1],i];

end;

lowbc = j(nrow(blow),1,0);

upbc = lowbca;

do i = 1 to nrow(blowbc);

  if (blowbc[i,1] < 1) then;

    do;

  blowbc[i,1]=1;

  end;

  lowbc[i,1]=boot[blowbc[i,1],i];

  if (bhighbc[i,1] > btn) then;

    do;

  bhighbc[i,1]=btn;

  end;

  upbc[i,1]=boot[bhighbc[i,1],i];

end;

print "*****************************************************";

print "BOOTSTRAP RESULTS FOR INDIRECT EFFECTS";

res = indsam||mnboot||(mnboot-indsam)||se;

cn = {"Data" "Boot" "Bias" "SE"};

print "Indirect Effects of IV on DV through Mediators (ab paths)";

print res [rowname = nms colname = cn format = 9.4];

lowperc = boot[blowp,];

upperc = boot[bhighp,];

ci = lowbca||upbca;

cn = {"Lower" "Upper"};

if (&bca ^= 0) then;

  do;

  print "Bias Corrected and Accelerated Confidence Intervals";

  print ci [rowname = nms colname = cn format = 9.4];

  end;

if (&bc ^= 0) then;

  do;

  ci = lowbc||upbc;

  print "Bias Corrected Confidence Intervals";

  print ci [rowname = nms colname = cn format = 9.4];

  end;

if (&percent ^= 0) then;

  do;

  ci = lowperc`||upperc`;

  print "Percentile Confidence Intervals";

  print ci [rowname = nms colname = cn format = 9.4];

  end;

print "*****************************************************";

print "Level of Confidence for Confidence Intervals";

print conf;

print "Number of Bootstrap Resamples";

print btn;

end;

if (&normal = 1) then;do;if (&c = 0) then;do;prt =1;end;end;

if (btn > 999) then;do;prt = 1;end;

if ((nv-nc-2) > 1) then;do;if (&contrast = 1) then;do;

if (prt = 1) then;do;

print "*****************************************************";

print "Indirect Effect Contrast Definitions: IndEff_1 minus IndEff2";

kk=1;

prwsv = j(((nv-nc-2)*(nv-nc-3)/2),2,"XXXXXXXX");

do ic = 1 to (nv-nc-3);

  do ic2 = (ic+1) to (nv-nc-2);

    prwsv[kk,1]=nm[ic+2,1];

  prwsv[kk,2]=nm[ic2+2,1];

  kk=kk+1;

  end;

end;

prwsv = cnam2||prwsv;

cn = {"Contrast" "IndEff_1" "IndEff_2"};

print prwsv [colname = cn];

end;end;end;

quit;

%mend;

/* Macro ends*/;

/*Call macro into data set*/;

data test;

seed=-1;

do i = 1 to 100;

Y=10+(.5)*rannor(1);

  X=2+(0.3)*rannor(1);

  /*** These are the mediators *****/;

  M1=1+(0.3)*rannor(1);

  M2=3+(0.4)*rannor(1);

  M2=5+(0.8)*rannor(1);

  M4=0+(.9)*rannor(1);

  /***** These are the covariates *******/;

  gender=ranbin(0,1,.5);

  age=60+(5)*rannor(1);

  edu=12+(1)*rannor(1);

    output;

  end;

run;

/*So i want to call the macro indirect and fit this model, can any one help with the last step*/

%INDIRECT [DATA = test, Y = yvar, X = xvar, M = mvlist {covlist..}]    /* Note M=Mvlist are M1, M2, M3 and M4 and covlist are age gender and edu */

{,C = {cov}(0**)}

{,BOOT = {z}(1000**)}

{,CONF = {ci}(95**)}

{,NORMAL = {t}(0**)}

{,CONTRAST = {n}(0**)}

{,PERCENT = {p}(0**)}

{,BC = {b}(1**)}

{,BCA = {d}(0)};

5 REPLIES 5
ballardw
Super User

You need to provide exactly what your macro invocation looks like.

My first feeling is that you're having issues with {} or () but it's hard to tell. I would expect to see

%INDIRECT [DATA = test, Y = yvar, X = xvar, M = M1 M2 M3 M4];

desireatem
Pyrite | Level 9

There is an issue with data test, how it is called,any correction;

NOTE: Unable to open SASUSER.REGSTRY. WORK.REGSTRY will be opened instead.

NOTE: All registry changes will be lost at the end of the session.

WARNING: Unable to copy SASUSER registry to WORK registry. Because of this,

WARNING: you will not see registry customizations during this session.

NOTE: Unable to open SASUSER.PROFILE. WORK.PROFILE will be opened instead.

NOTE: All profile changes will be lost at the end of the session.

NOTE: This SAS session is using a registry in WORK.  All changes will be lost at the end of this

NOTE: session.

NOTE: Unable to open SASUSER.PROFILE. WORK.PROFILE will be opened instead.

NOTE: All profile changes will be lost at the end of the session.

1    %macro indirect(data=,y=,x=,m=,c=0,boot=1000,conf=95,percent=0,bc=1,bca=0,

1  ! normal=0,contrast=0);

2    proc iml;

3    use &data;

4    read all var{&y &x &m} into dd;

5    nm={&y &x &m};

6    xx=(dd = .);xx=xx[,+];

7    j=1;do i = 1 to nrow(dd);if xx[i,1]=0 then;do;dd[j,]=dd[i,];j=j+1;end;end;

8    dd=dd[1:j-1,];

9    nm = nm`;

10   n = nrow(dd);

11   nv = ncol(dd);

12   nc = &c;

13   con=j(n,1,1);

14   dt2 = dd;

15   dt = dd;

16   resid = j(n,(nv-nc),0);

17   info = j((2*(nv-nc-2)+1),(2*(nv-nc-2)+1),0);

18   imat = j(ncol(info),4,1);

19   imat[1:(nv-nc-2),1]=(2:(nv-nc-1))`;

20   imat[1:(nv-nc-2),3]=(2:(nv-nc-1))`;

21   imat[(nv-nc-1):(ncol(info)-1),2]=(2:(nv-nc-1))`;

22   imat[(nv-nc-1):(ncol(info)-1),4]=(2:(nv-nc-1))`;

23   imat[(nv-nc-1):(ncol(info)-1),1]=j((nv-nc-2),1,(nv-nc));

24   imat[(nv-nc-1):(ncol(info)-1),3]=j((nv-nc-2),1,(nv-nc));

25   imat[ncol(info),1:4]={1 1 1 1};

26   imat[ncol(info),1]=nv-nc;

27   imat[ncol(info),3]=nv-nc;

28   bzx = j(nv-2-nc,1,0);

29   bzxse = j(nv-2-nc,1,0);

30   b=j((nv-1-nc),(nv-1-nc),0);

31   cname={"C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10", "C11", "C12", "C13",

31 ! "C14", "C15", "C16"};

32   cname=cname//{"C17", "C18", "C19", "C20", "C21", "C22", "C23", "C24", "C25", "C26", "C27",

32 ! "C28", "C29"};

33   cname=cname//{"C30", "C31", "C32", "C33", "C34", "C35", "C36", "C37", "C38", "C39", "C40",

33 ! "C41", "C42"};

34   cname=cname//{"C43", "C44", "C45"};

35   p0 = -0.322232431088;

36   p1 = -1;

37   p2 = -0.342242088547;

38   p3 = -0.0204231210245;

39   p4 = -.0000453642210148;

40   q0 = 0.0993484626060;

41   q1 = 0.588581570495;

42   q2 = 0.531103462366;

43   q3 = 0.103537752850;

44   q4 = 0.0038560700634;

45   conf=round(&conf);

46   lowalp = 0.5*(1-(conf/100));

47   upalp = 0.5*(1+(conf/100));

48   zbca = lowalp//upalp;

49   btn = 1;

50   if (&boot > 999) then;

51     do;

52     btn = floor(&boot/1000)*1000;

53     end;

54   blowp = floor(lowalp*btn);

55   if (blowp < 1) then;

56     do;

57     blowp = 1;

58     end;

59   bhighp = floor((upalp*btn)+1);

60   if (bhighp > btn) then;

61     do;

62     bhighp = btn;

63     end;

64   indeff = j((n+1+btn),(nv-1-nc),0);

65   do d = 1 to (n+1+btn);

66     if (d = (n+2)) then;

67       do;

68     dt = dt2;

69     con = j(n,1,1);

70     end;

71     if (d > 1) then if (d < (n+2)) then;

72       do;

73       if (d = 2) then;

74      do;

75      con = j((n-1),1,1);

76      dt = dt2[2:n,];

77      end;

78     if (d = (n+1)) then;

79      do;

80         dt = dt2[1:(n-1),];

81      end;

82       if (d > 2) then if (d < (n+1)) then;

83      do;

84         dt = dt2[1:(d-2),]//dt2[(d:n),];

85         end;

86     end;

87     if (d > (n+1)) then;

88       do;

89      do nn = 1 to n;

90         v = int(ranuni(0)*n)+1;

91      dt[nn,1:nv]=dt2[v,1:nv];

92      end;

93     end;

94

95

96   x = dt[,2];

97   m = dt[,3:(nv-nc)];

98   y = dt[,1];

99   xz = dt[,2:nv];

100  xo = con||x;

101  if (nc > 0) then;

102     do;

103     c = dt[,(nv-nc+1):nv];

104     xo = xo||c;

105     end;

106  do k = 3 to (nv-nc);

107    ytmp = dt[,k];

108    bzxt = inv(xo`*xo)*xo`*ytmp;

109    bzx[(k-2),1]=bzxt[2,1];

110    if (d = 1) then;

111      do;

112    resid[,(k-1)]=ytmp-(xo*bzxt);

113    mse = sum((ytmp-(xo*bzxt))##2)/(n-2-nc);

114    olscm = (mse*inv(xo`*xo));

115    bzxse[(k-2),1]=sqrt(olscm[2,2]);

116    end;

117  end;

118  if (d = 1) then;

119    do;

120    if (nc > 0) then;

121      do;

122      cnt = dd[,(nv-(nc-1)):nv];

123      xo = con||x||cnt;

124    end;

125    if (nc = 0) then;

126      do;

127      xo = con||x;

128    end;

129    byx = inv(xo`*xo)*xo`*y;

130    mse = sum((y-(xo*byx))##2)/(n-2-nc);

131    olscm = (mse*inv(xo`*xo));

132    byxse = sqrt(olscm[2,2]);

133    byx = byx[2,1];

134    end;

135  xzo = con||xz;

136  byzx = inv(xzo`*xzo)*xzo`*y;

137  byzx2 = byzx[3:(nv-nc),1];

138  if (d = 1) then;

139    do;

140    resid[,ncol(resid)]=y-(xzo*byzx);

141    mse = sum((y-(xzo*byzx))##2)/(n-nv);

142    covmat = mse*inv(xzo`*xzo);

143    olscm = vecdiag(covmat);

144    sse = mse*(n-nv);

145    sst = sum((y-(sum(y)/n))##2);

146    r2 = 1-(sse/sst);

147    ar2 = 1-(mse/(sst/(n-1)));

148    fr = ((n-nv)*r2)/((1-r2)*ncol(xz));

149    pfr = 1-probf(fr,ncol(xz),(n-nv));

150    if (nc > 0) then;

151      do;

152      bcon = byzx[(nv-nc+1):nv,1];

153    bconse = sqrt(olscm[(nv-nc+1):nv,1]);

154      end;

155    byzx2se = sqrt(olscm[3:(nv-nc),1]);

156    cprime = byzx[2,1];

157    cprimese=sqrt(olscm[2,1]);

158  end;

159  indeff2 = (bzx#byzx2);

160  zs = (bzx/bzxse)#(byzx2/byzx2se);

161  temp = t(sum(indeff2)//indeff2);

162  indeff[d,]=temp;

163  if (d = 1) then;

164    do;

165    vs = nm[1:(nv-nc),1];

166    rn = {"DV = " "IV = " "MEDS = "};

167    print "Dependent, Independent, and Proposed Mediator Variables";

168    print vs [rowname = rn];

169    if (nc > 0) then;

170      do;

171    vs = nm[(nv-nc+1):nv,1];

172    print "Statistical Controls";

173    rn = {"CONTROLS="};

174    print vs [rowname = rn];

175    end;

176    print "Sample size";

177    print n;

178    nms = nm[3:(nv-nc),1];

179    te = bzx/bzxse;

180    df = n-2-nc;

181    p = 2*(1-probt(abs(te),df));

182    bzxmat = bzx||bzxse||te||p;

183    b[2:(nv-1-nc),1]=bzx;

184    se2 = bzxse#bzxse;

185    cnm = {"Coeff" "se" "t" "p"};

186    print "IV to Mediators (a paths)";

187    print bzxmat [rowname = nms colname = cnm format = 9.4];

188    te = byzx2/byzx2se;

189    df = n-nv;

190    p = 2*(1-probt(abs(te),df));

191    byzx2mat=byzx2||byzx2se||te||p;

192    print "Direct Effects of Mediators on DV (b paths)";

193    print byzx2mat [rowname = nms colname = cnm format = 9.4];

194    te=byx/byxse;

195    df = n-2-nc;

196    p=2*(1-probt(abs(te),df));

197    byxmat = byx||byxse||te||p;

198    xnm=nm[2,1];

199    print "Total effect of IV on DV (c path)";

200    print byxmat [rowname = xnm colname = cnm format = 9.4];

201    te=cprime/cprimese;

202    df = n-nv;

203    p=2*(1-probt(abs(te),df));

204    cprimmat = cprime||cprimese||te||p;

205    print "Direct Effect of IV on DV (c' path)";

206    print cprimmat [rowname = xnm colname = cnm format = 9.4];

207    if (nc > 0) then;

208      do;

209    df = n-nv;

210    nms = nm[(nv-nc+1):nv,1];

211    te=bcon/bconse;

212    p=2*(1-probt(abs(te),df));

213    bconmat = bcon||bconse||te||p;

214    print "Partial Effect of Control Variables on DV";

215    print bconmat [rowname = nms colname = cnm format = 9.4];

216    end;

217    dvms=r2||ar2||fr||ncol(xz)||(n-nv)||pfr;

218    print "Fit Statistics for DV Model";

219    cnm = {"R-sq" "adj R-sq" "F" "df1" "df2" "p"};

220    print dvms [colname = cnm format = 9.4];

221    if (&normal =^ 0) then;if(&c = 0) then;do;

222      do;

223        bmat=j((nv-nc),(nv-nc),0);

224        bmat[2:(nv-nc-1),1]=bzx;

225     bmat[(nv-nc),2:(nv-nc-1)]=byzx2`;

226        bmat[(nv-nc),1]=cprime;

227        imbinv = inv(i(ncol(bmat))-bmat);

228     imbtinv = inv(i(ncol(bmat))-bmat`);

229        resid[,1]=x-(x[+]/n);

230     psi = (resid`*resid)/(n-1);

231     invpsi = inv(psi);

232     ibpsiib = imbinv*psi*imbtinv;

233     do ic = 1 to ncol(info);

234       do ic2 = 1 to ncol(info);

235     info[ic,ic2]=(n-1)*((imbinv[imat[ic2,4],imat[ic,1]]*imbinv[imat[ic,2],imat[ic2,3]])+(ibpsii

235! b[imat[ic2,4],imat[ic,2]]*invpsi[imat[ic,1],imat[ic2,3]]));

236          end;

237     end;

238     varcov=inv(info);

239     varcov=varcov[1:(2*(nv-nc-2)),1:(2*(nv-nc-2))];

240     ses = vecdiag(varcov);

241     avar = ses[1:nrow(bzxse),1];

242     bvar = ses[(nrow(bzxse)+1):nrow(ses),1];

243     if ((nv-nc-2) > 1) then;do;if(&contrast = 1) then;do;

244       prws=j(((nv-nc-2)*(nv-nc-3)/2),1,0);

245    prwse=prws;

246    kk=1;

247    do ic = 1 to (nv-nc-3);

248     do ic2 = (ic+1) to (nv-nc-2);

249       vf2=((byzx2[ic,1]##2)*varcov[ic,ic])-(2*byzx2[ic,1]*byzx2[ic2,1]*(varcov[ic,ic2]));

250    vf2=vf2+((byzx2[ic2,1]##2)*varcov[ic2,ic2])+((bzx[ic,1]##2)*(bvar[ic,1]));

251    vf2=vf2-(2*bzx[ic,1]*bzx[ic2,1]*covmat[(2+ic),(2+ic2)])+((bzx[ic2,1]##2)*(bvar[ic2,1]));

252    cnt=indeff2[ic,1]-indeff2[ic2,1];

253    prws[kk,1]=cnt;

254    prwse[kk,1]=sqrt(vf2);

255    kk=kk+1;

256     end;

257       end;

258     cnam2=cname[1:(kk-1),1];

259     end;end;

260     dermat=byzx2//bzx;

261     totse=sqrt((dermat)`*varcov*dermat);

262     specse = sqrt((byzx2#byzx2)#(avar)+(bzx#bzx)#(bvar));

263     specse = totse//specse;

264     indsum=indeff2[+];

265        specz = (indsum//indeff2)/specse;

266     ind22 = indsum//indeff2;

267     nms = {"TOTAL"}//nm[3:(nv-nc),1];

268     if ((nv-nc-2) > 1) then;do;if(&contrast = 1) then;do;

269       ind22 = ind22//prws;

270    specse = specse//prwse;

271    specz2 = prws/prwse;

272    specz = specz//specz2;

273    nms = nms//cnam2;

274    end;end;

275        pspec = 2*(1-probnorm(abs(specz)));

276        spec = ind22||specse||specz||pspec;

277        cnm = {"Effect" "se" "Z" "p"};

278        print "*****************************************************";

279        print "NORMAL THEORY TESTS FOR INDIRECT EFFECTS";

280        print "Indirect Effects of IV on DV through Mediators (ab paths)";

281        print spec [rowname = nms colname = cnm format = 9.4];

282      end;end;

283    end;

284  end;

285  if (btn > 1) then;do;

286    nms = {"TOTAL"}//nm[3:(nv-nc),1];

287    if (nv-nc-2) > 1 then do;if (&contrast = 1) then do;

288       crst=j((n+1+btn),((nv-nc-2)*(nv-nc-3)/2),0);

289    kk=1;

290    do ic = 2 to (nv-nc-2);

291      do ic2 = (ic+1) to (nv-nc-1);

292        crst[,kk]=indeff[,ic]-indeff[,ic2];

293    kk=kk+1;

294      end;

295    end;

296       indeff = indeff||crst;

297    cnam2=cname[1:(kk-1),1];

298    nms = nms//cnam2;

299    end;

300    end;

301  lvout = indeff[2:(n+1),];

302  tdotm = lvout[+,]/n;

303  tm = j(n,ncol(lvout),1)*diag(tdotm);

304  topa=(((n-1)/n)*(tm-lvout))##3;

305  topa=topa[+,];

306  bota =((((n-1)/n)*(tm-lvout))##2);

307  bota=bota[+,];

308  bota=6*sqrt(bota##3);

309  ahat = topa/bota;

310  indsam = indeff[1,]`;

311  boot = indeff[(n+2):nrow(indeff),];

312  mnboot = (boot[+,]/btn)`;

313  xt=boot-j(btn,1)*boot[:,];

314  cv=(xt`*xt)/btn;

315  se=sqrt(vecdiag(cv));

316

317

318    create bootstp from boot [colname='indirect'];

319    append from boot;

320    nnn = j(1,ncol(indeff),-999);

321    boot = nnn//boot;

322    do e = 1 to (ncol(indeff));

323    do i = 2 to (btn+1);

324      ix = boot[i,e];

325    do k = i to 2 by -1;

326     k2 = k;

327     if (boot[(k-1),e] > ix) then;

328       do;

329    boot[k,e]=boot[(k-1),e];

330    end;

331     else;

332     if (boot[(k-1),e] <= ix) then;

333       do;

334    goto stpit;

335    end;

336    end;

337    stpit:

338    boot[k2,e]=ix;

339      end;

340      end;

341    boot = boot[2:(btn+1),];

342

343

344  xp=j((nrow(mnboot)+2),1,0);

345  do i = 1 to (nrow(mnboot)+2);

346    if (i <= nrow(mnboot)) then;

347      do;

348      pv = (boot[,i] < indsam[i,1]);

349    pv = pv[+,]/btn;

350      end;

351    else;

352      pv = zbca[(i-nrow(mnboot)),1];

353    p=pv;

354    if (pv > 0.5) then;

355      do;

356    p = 1-pv;

357    end;

358    y5 = sqrt(-2*log(p));

359    xp[i,1]=y5+((((y5*p4+p3)*y5+p2)*y5+p1)*y5+p0)/((((y5*q4+q3)*y5+q2)*y5+q1)*y5+q0);

360    if (pv <= 0.5) then;

361      do;

362    xp[i,1]=-xp[i,1];

363    end;

364  end;

365  bbb = nrow(mnboot);

366  zz = xp[1:bbb,1];

367  zlo = zz + ((zz+xp[(bbb+1),1])/(1-ahat`#(zz+xp[(bbb+1),1])));

368  zup = zz + ((zz+xp[(bbb+2),1])/(1-ahat`#(zz+xp[(bbb+2),1])));

369  ahat = 0;

370  zlobc = zz + ((zz+xp[(bbb+1),1])/(1-ahat`#(zz+xp[(bbb+1),1])));

371  zupbc = zz + ((zz+xp[(bbb+2),1])/(1-ahat`#(zz+xp[(bbb+2),1])));

372  zlo = probnorm(zlo);

373  zup = probnorm(zup);

374  zlobc = probnorm(zlobc);

375  zupbc = probnorm(zupbc);

376  blow = int(zlo*(btn+1));

377  bhigh = int(zup*(btn+1))+1;

378  blowbc = int(zlobc*(btn+1));

379  bhighbc = int(zupbc*(btn+1))+1;

380  lowbca = j(nrow(blow),1,0);

381  upbca = lowbca;

382  do i = 1 to nrow(blow);

383    if (blow[i,1] < 1) then;

384      do;

385    blow[i,1]=1;

386    end;

387    lowbca[i,1]=boot[blow[i,1],i];

388    if (bhigh[i,1] > btn) then;

389      do;

390    bhigh[i,1]=btn;

391    end;

392    upbca[i,1]=boot[bhigh[i,1],i];

393  end;

394  lowbc = j(nrow(blow),1,0);

395  upbc = lowbca;

396  do i = 1 to nrow(blowbc);

397    if (blowbc[i,1] < 1) then;

398      do;

399    blowbc[i,1]=1;

400    end;

401    lowbc[i,1]=boot[blowbc[i,1],i];

402    if (bhighbc[i,1] > btn) then;

403      do;

404    bhighbc[i,1]=btn;

405    end;

406    upbc[i,1]=boot[bhighbc[i,1],i];

407  end;

408  print "*****************************************************";

409  print "BOOTSTRAP RESULTS FOR INDIRECT EFFECTS";

410  res = indsam||mnboot||(mnboot-indsam)||se;

411  cn = {"Data" "Boot" "Bias" "SE"};

412  print "Indirect Effects of IV on DV through Mediators (ab paths)";

413  print res [rowname = nms colname = cn format = 9.4];

414  lowperc = boot[blowp,];

415  upperc = boot[bhighp,];

416  ci = lowbca||upbca;

417  cn = {"Lower" "Upper"};

418  if (&bca ^= 0) then;

419    do;

420    print "Bias Corrected and Accelerated Confidence Intervals";

421    print ci [rowname = nms colname = cn format = 9.4];

422    end;

423  if (&bc ^= 0) then;

424    do;

425    ci = lowbc||upbc;

426    print "Bias Corrected Confidence Intervals";

427    print ci [rowname = nms colname = cn format = 9.4];

428    end;

429  if (&percent ^= 0) then;

430    do;

431    ci = lowperc`||upperc`;

432    print "Percentile Confidence Intervals";

433    print ci [rowname = nms colname = cn format = 9.4];

434    end;

435  print "*****************************************************";

436  print "Level of Confidence for Confidence Intervals";

437  print conf;

438  print "Number of Bootstrap Resamples";

439  print btn;

440  end;

441  if (&normal = 1) then;do;if (&c = 0) then;do;prt =1;end;end;

442  if (btn > 999) then;do;prt = 1;end;

443  if ((nv-nc-2) > 1) then;do;if (&contrast = 1) then;do;

444  if (prt = 1) then;do;

445  print "*****************************************************";

446  print "Indirect Effect Contrast Definitions: IndEff_1 minus IndEff2";

447  kk=1;

448  prwsv = j(((nv-nc-2)*(nv-nc-3)/2),2,"XXXXXXXX");

449  do ic = 1 to (nv-nc-3);

450    do ic2 = (ic+1) to (nv-nc-2);

451      prwsv[kk,1]=nm[ic+2,1];

452    prwsv[kk,2]=nm[ic2+2,1];

453    kk=kk+1;

454    end;

455  end;

456  prwsv = cnam2||prwsv;

457  cn = {"Contrast" "IndEff_1" "IndEff_2"};

458  print prwsv [colname = cn];

459  end;end;end;

460  quit;

461  %mend;

462

463

464  /* Macro ends*/;

465  /*Call macro into data set*/;

466

467

468  data test;

469  seed=-1;

470  do i = 1 to 100;

471  Y=10+(.5)*rannor(1);

472    X=2+(0.3)*rannor(1);

473    /*** These are the mediators *****/;

474    M1=1+(0.3)*rannor(1);

475    M2=3+(0.4)*rannor(1);

476    M2=5+(0.8)*rannor(1);

477    M4=0+(.9)*rannor(1);

478    /***** These are the covariates *******/;

479    gender=ranbin(0,1,.5);

480    age=60+(5)*rannor(1);

481    edu=12+(1)*rannor(1);

482      output;

483    end;

484  run;

NOTE: The data set WORK.TEST has 100 observations and 10 variables.

NOTE: DATA statement used (Total process time):

      real time           0.02 seconds

      cpu time            0.01 seconds

485

486

487  /*So i want to call the macro indirect and fit this model, can any one help with the last

487! step*/

488  %INDIRECT [DATA = test, Y = yvar, X = xvar, M = M1 M2 M3 M4{gender age edu}];    /* Note

488! M=Mvlist are M1, M2, M3 and M4 and covlist are age gender and edu */

NOTE: Writing HTML Body file: sashtml.htm

NOTE: IML Ready

NOTE: Line generated by the invoked macro "INDIRECT".

1                  use &data;

                            -

                            22

                            76

ERROR 22-322: Expecting a name.

ERROR 76-322: Syntax error, statement will be ignored.

.......

......

489  {,C = {cov}(0**)}

490  {,BOOT = {z}(1000**)}

491  {,CONF = {ci}(95**)}

492  {,NORMAL = {t}(0**)}

493  {,CONTRAST = {n}(0**)}

494  {,PERCENT = {p}(0**)}

495  {,BC = {b}(1**)}

496  {,BCA = {d}(0)};

desireatem
Pyrite | Level 9

ERROR

982

983  /*So i want to call the macro indirect and fit this model, can any one help with the last

983! step*/

984  %INDIRECT [DATA = test, Y = y, X = x, M = M1 M2 M3 M4{gender age edu}];    /* Note M=Mvlist

984! are M1, M2, M3 and M4 and covlist are age gender and edu */

NOTE: IML Ready

NOTE: Line generated by the invoked macro "INDIRECT".

1                 use &data;

                            -

                            22

                            76

ERROR 22-322: Expecting a name.

ballardw
Super User

I'm not sure about the &dat issue but if you are calling the macro with this syntax:

%INDIRECT [DATA = test, Y = y, X = x, M = M1 M2 M3 M4{gender age edu}];  

I think you are having issues because the macro parameter M resolves to: M1 M2 M3 M4{gender age edu}

In the READ statement that generates a nested set of {    {} } and I think the compiler doesn't like that construct.

Run this code snipped and see if you get errors pointing to the second { in read statement.

proc iml;

   use sashelp.class;

   read all var {sex age {height weight}};

run;

Did you ever run this without macro variables?

desireatem
Pyrite | Level 9

993  proc iml;

NOTE: IML Ready

994     use sashelp.class;

995     read all var {sex age {height weight}};

                              -              -

                              22             200

                              200

ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string,

              a numeric constant, a datetime constant, a missing value, (, (|, ), *, ',', -, =, [,

              |, }.

ERROR 200-322: The symbol is not recognized and will be ignored.

995! read all var {sex age {height weight}};

                                             -

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
  • 5 replies
  • 679 views
  • 0 likes
  • 2 in conversation