Macro looping over columns in SAS broken by ods

∥☆過路亽.° 提交于 2019-12-24 22:17:09

问题


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 &currentvalue; 
  %put &i ; 
ods graphics on / width=12.5 in height=12.5in imagemap ;   
title "&currentvalue  &i "; 
proc sgplot data=sashelp.cars;
series x=EngineSize y=&currentvalue ; 
scatter x=EngineSize y=&currentvalue
;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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!