data wanted;
set test end=last;
if _n_=1 then call execute("data wanted; retain d1-d4;");
call execute("d2="||cats(d2)||";d3="||cats(d3)||";do d1=1 to "||cats(d2-1)||";do d4="||cats(d3+1)||" to 20;output;end;end;");
if last then call execute("run;");
run;