Skip to content
Shane Rosanbalm edited this page Mar 22, 2017 · 15 revisions

Contents

  1. Overview
  2. Plot Statements
  3. Marker Customization
  4. Axis Customization
  5. Legend Customization
  6. Saving Output
  7. Compatible Plot Types

Overview

SGPLOT is the sliver of ODS Graphics technology that provides the right balance between ease of use and customizability. The majority of the content discussed in this wiki revolves around SGPLOT. Once you master this content, then you can step up to GTL and really get serious (we're talking PIE charts!!!). But, for now, we will content ourselves with SGPLOT.

If you're fond of conference papers, there is an excellent paper from 2015 titled Graphing Made Easy with SGPLOT and SGPANEL Procedures. But, this is a wiki, so we will be briefer than a conference paper. Maybe you can read the paper after you've read the wiki.

Plot Statements

The SGPLOT procedure is capable of producing scatter plots, series plots, bar charts, and many other chart types. A gallery of SGPLOT examples is available here. A list of the complete set of SGPLOT plot types for v9.4 is available here.

The pseudo-syntax for producing a plot is:

proc sgplot data=mydata;
   plot_name required_args / optional_args;
run;

Moving from the theoretical to the concrete, here is the code and output for a basic scatter plot:

proc sgplot data=sashelp.cars;
   scatter y=enginesize x=horsepower;
run;

sgplot scatter

Marker Customization

Basic customization can be performed using options on the plot statements. For instance, the appearance of the markers is controlled by the markerattrs= option. In this example we use the color= sub-option to make the markers purple.

proc sgplot data=sashelp.cars;
   scatter y=enginesize x=horsepower / markerattrs=(color=purple);
run;

sgplot markerattrs

Axis Customization

Use the yaxis and xaxis statements to customize the appearance of axes. Note that there is no slash on an axis statement.

proc sgplot data=sashelp.cars;
   scatter y=enginesize x=horsepower / markerattrs=(color=purple);
   yaxis label="Log Engine Size (L)" type=log;
run;

sgplot yaxis

Legend Customization

If you add the group= option to your scatter statement, you will get a legend under your plot.

proc sort data=sashelp.cars out=cars;
   by cylinders;
   where n(cylinders);
run;

proc sgplot data=cars;
   scatter y=enginesize x=horsepower / group=cylinders;
run;

sgplot group

Use the keylegend statement to customize the appearance of the legend.

proc sort data=sashelp.cars out=cars;
   by cylinders;
   where n(cylinders);
run;

proc sgplot data=cars;
   scatter y=enginesize x=horsepower / group=cylinders;
   keylegend / down=2;
run;

sgplot keylegend

Saving Output

There are a couple of different ways to save your output. If you want to create a PDF, RTF, or HTML file, then you just need to make yourself an ODS sandwich.

%let path = _your_path_here_;
ods pdf file="&path\sgplot_scatter.pdf";

proc sgplot data=sashelp.cars;
   scatter y=enginesize x=horsepower;
run;

ods pdf close;

sgplot pdf

On the other hand, if you want to create a PNG file, you need to use a combination of the ods listing and ods graphics statements.

ods listing gpath="_your_path_here_";
ods graphics / imagename="sgplot_png";

proc sgplot data=sashelp.cars;
   scatter y=enginesize x=horsepower;
run;

sgplot png

Compatible Plot Types

Certain plot types can be combined in a single plot. For instance, histogram plots can be combined with density plots.

proc sgplot data=sashelp.cars;
   histogram horsepower;
   density horsepower;
run;

sgplot compatible

Not that you often have occasion to combine these two plot types, but it's an example.

Next page: High-value Statements and Options