0%

How to save graphs in SAS

Recently, I'm a little confused how to create or save PNG graphs in SAS. Normally, we would have been to create RTF or PDF instead but there was sometimes a specific requestment to save as PNG directly. So we need to know how to complete it in SAS when I have a graph generated by SGPLOT or GTL procedure.

Given that I want to plot a scatter plot with regression line for sashelp.iris dataset by the GTL(Graph Template Language) process. So I define a GTL template firstly.

proc template;
    define statgraph ScatterRegPlot;
    begingraph/ backgroundcolor=white border=false datacontrastcolors=(orange purple blue) datasymbols=(circlefilled trianglefilled DiamondFilled);
        layout overlay;
            scatterplot x=SepalLength y=SepalWidth /group=Species name='points';
            regressionplot x=SepalLength y=SepalWidth / group=Species degree=3 name='reg';
            discretelegend 'points';
        endlayout;
    endgraph;
    end;
run;

Now let's see how to create RTF or PDF with this graph.

For PDF as below:

ods escapechar="^";
ods listing close;
options nonumber nodate;
ods pdf file="C:/Users/Desktop/example.pdf";

proc sgrender data = sashelp.iris template = ScatterRegPlot; run;

ods pdf close;
ods listing;

For RTF just change ods pdf above to ods rtf.

If we just want to save as PNG, as follows:

ods listing gpath='C:/Users/TJ0695/Desktop' image_dpi = 300 style=Journal;
ods graphics / imagename="example" imagefmt=png width = 20cm height = 15cm;

proc sgrender data = sashelp.iris template = ScatterRegPlot; run;

ods graphics off;
example

If we increase DPI to 600, it will cause an error, like ERROR: Java virtual machine exception. java.lang.OutOfMemoryError: Java heap space.. So we should modify the configuration file of SAS to fix this error.

  • Run proc options option=config; run; to find the certain configuration file.
  • Open that file and find the specific text started with -Xms or -Xmx, and change both of them to 1024m from 128m.
  • Reboot SAS and rerun the code.

After that the error doesn't appear again but the warning is still there.


Reference

Generating PNG files in SAS using ODS Graphics