Search code examples
plotsassas-ods

SAS: PROC SGPLOT BY GROUP auto file names


I am plotting some data using BY GROUP option. While I am able to use #byval option to automatically put BY GROUP value in title of each plot, but I want to save each plot individually and want to name it after #byval instead of calling it - SGPLOT01, SGPLOT02 ...

e.g. Lets say I have:

data xyz;
input type$ x y1 y2@@;
cards;
A 1 5 7
A 2 7 9
A 3 8 10
B 1 5 7
B 2 7 9
B 3 8 10
;;
RUN;

PROC SGPLOT DATA=xyz;
by type;
series1 x=x y=y1/markers;
series2 x=x y=y2/markers;
title "#byval";
RUN;

In this example, two plots will be created one each for type A and B. But program will automatically name them as SGPLOT1.pdf and SGPLOT2.pdf. I would rather want to name them A.pdf and B.pdf, and want to save them to directory "C:/SGPLOTS/".

Thanks for your help.


Solution

  • One option is to use ODS and put use a macro to print each TYPE separately, like in the following example.

    data xyz;
    input type$ x y1 y2 @@;
    cards;
    A 1 5 7
    A 2 7 9
    A 3 8 10
    B 1 5 7
    B 2 7 9
    B 3 8 10
    ;
    RUN;
    
    ods listing close;
    
    %macro plot_it(type=);
    
       goptions reset
          device = sasprtc
          target = sasprtc
          ;
    
       ods pdf file="C:/SGPLOTS/&type..pdf" notoc;
    
       PROC SGPLOT DATA=xyz;
       by type;
       where type = "&type";
       series x=x y=y1/markers;
       series x=x y=y2/markers;
       title "#byval";
       RUN;
    
       ods pdf close;
    
    %mend plot_it;
    
    %plot_it(type=A);
    %plot_it(type=B);