问题
I just found that ods graphics / reset;
broke my loop. I am still curious why this happened and if there are other potential similar pitfalls.
Objective: I want to loop over columns in SAS and provide a plot where the x variable remains constant, but the y dimension varies. I could transpose and use a by statement. I don't want to do that.
Problem:
Despite the log with options mprint ;
showing the text replacement is working properly, the outputted plots only display the final plot repeatedly rather than each individual plot. To repeat - in the log everything is incrementing properly / in the output the plot and title only show the last value of the loop.
Solution:
Delete the ods graphics / reset;
Here's a toy example:
proc sgplot data=sashelp.cars;
series x=EngineSize y=Cylinders;
scatter x=EngineSize y=Cylinders;
run;
proc sql ; select distinct NAME
into :varlist separated by ' '
from dictionary.columns
where libname='SASHELP' and memname = 'CARS' AND TYPE='num';
quit;
%let n=&sqlobs;
%MACRO PLOTYA;
%do i= 1 %to &n ;
%let currentvalue = %scan(&varlist, &i);
%put ¤tvalue;
%put &i ;
ods graphics on / width=12.5 in height=12.5in imagemap ;
title "¤tvalue &i ";
proc sgplot data=sashelp.cars;
series x=EngineSize y=¤tvalue ;
scatter x=EngineSize y=¤tvalue
;run;
ods graphics / reset;
%end;
%MEND PLOTYA;
options mprint;
%plotya ;
Thanks for your time.
回答1:
It isn't breaking your loop, the loop is running, but only the last results are retained. This is because the image name is reset when you reset all the options.
From the documentation under RESET:
By default, each time you run a procedure, new images are created and numbered incrementally using a base name, such as SGRender, SGRender1, SGRender2, and so on. RESET will reset to the base name without the increment number. This is handy if you run a PROC several times and are interested only in the images from the last run (the previous ones will be overwritten). This option is positional, so it typically comes first.
You can specify an explicit different IMAGENAME for each iteration to avoid this behaviour.
http://support.sas.com/documentation/cdl/en/grstatug/62464/HTML/default/viewer.htm#p0ewg6cv4t0scfn11pj4x1t8fb04.htm
来源:https://stackoverflow.com/questions/28597475/macro-looping-over-columns-in-sas-broken-by-ods