0

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.

Joe
  • 62,789
  • 6
  • 49
  • 67
Wes McClintick
  • 497
  • 5
  • 14

1 Answers1

4

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

Reeza
  • 20,510
  • 4
  • 21
  • 38
  • Well, perhaps I should have said 'broke the program'. It's still seems odd that the displayed output was only the last results. The reset came after the sgplot. I suppose it's just that the output to the Results Viewer comes after the processing of the do loop. – Wes McClintick Feb 19 '15 at 02:42
  • 1
    That's correct: the results viewer is only updated at the end of processing one batch of code. – Joe Feb 19 '15 at 16:04