Contributor
Posts: 29

# Unclosed Do loop in Macro

For some reason this claims I have an unclosed DO loop. I am admittedly a SAS noob. I searched the board but didn't see anything that helped me answer the question (though I'm sure there must be this answer somewhere).

The data has two columns of interest- shortname (a groupingv variable) and the data in two datasets- ASet and CSet (they contain the same columns).

Before this code, there are macro variables vcshortname1, vcshortname2....vcshortname10 pre-defined.

%macro mac;
%do i=1 %to 10;
/* Separate by shortname, then run permutation test on each shortname*/
data G1;
set ASet;
if shortname = &&vcshortname.&i;
keep data;
run;

data G2;
set CSet;
if shortname = &&vcshortname.&i;
keep data;
run;

/*This should END the loop!*/

end;

/*Running code for permutation test. Got it from the community board*/
proc IML;

obsdiff = mean(G1) = mean(G2);
alldata = G1 // G2; /* stack data in a single vector */
N1 = nrow(G1); N = N1 + nrow(G2);
NRepl = 9999; /* number of permutations */
nulldist = j(NRepl,1); /* allocate vector to hold results */
do k = 1 to NRepl;
x = sample(alldata, N, "WOR"); /* permute the data */
nulldist[k] = mean(x[1:N1]) - mean(x[(N1+1):N]); /* difference of means */
end;

pval = (1 + sum(abs(nulldist) >= abs(obsdiff))) / (NRepl+1);
print pval;

title "Histogram of Null Distribution";

refline = "refline " + char(obsdiff) + " / axis=x lineattrs=(color=red);";
call Histogram(nulldist) other=refline;

quit;

%mend mac;

%mac

Super User
Posts: 13,947

## Re: Unclosed Do loop in Macro

@abak wrote:

For some reason this claims I have an unclosed DO loop. I am admittedly a SAS noob. I searched the board but didn't see anything that helped me answer the question (though I'm sure there must be this answer somewhere).

The data has two columns of interest- shortname (a groupingv variable) and the data in two datasets- ASet and CSet (they contain the same columns).

Before this code, there are macro variables vcshortname1, vcshortname2....vcshortname10 pre-defined.

%macro mac;
%do i=1 %to 10;
/* Separate by shortname, then run permutation test on each shortname*/
data G1;
set ASet;
if shortname = &&vcshortname.&i;
keep data;
run;

data G2;
set CSet;
if shortname = &&vcshortname.&i;
keep data;
run;

/*This should END the loop!*/

%end;

/*Running code for permutation test. Got it from the community board*/
proc IML;

obsdiff = mean(G1) = mean(G2);
alldata = G1 // G2; /* stack data in a single vector */
N1 = nrow(G1); N = N1 + nrow(G2);
NRepl = 9999; /* number of permutations */
nulldist = j(NRepl,1); /* allocate vector to hold results */
do k = 1 to NRepl;
x = sample(alldata, N, "WOR"); /* permute the data */
nulldist[k] = mean(x[1:N1]) - mean(x[(N1+1):N]); /* difference of means */
end;

pval = (1 + sum(abs(nulldist) >= abs(obsdiff))) / (NRepl+1);
print pval;

title "Histogram of Null Distribution";

refline = "refline " + char(obsdiff) + " / axis=x lineattrs=(color=red);";
call Histogram(nulldist) other=refline;

quit;

%mend mac;

%mac

Please see the highlighted text for how to end a %DO loop.

Discussion stats