Introduction to SAS/GRAPH

Michael Friendly

What is SAS/GRAPH ?

SAS/GRAPH is a computer graphics system for displaying data in the form of high resolution color plots, charts, maps, and slides on graphics terminals and hardcopy printing & plotting devices.

SAS/GRAPH programs are SAS procedures like any other SAS procedures. To produce a graph, you usually place your data into a SAS dataset and call the graphic procedure to produce your plot. For two-dimensional plots and charts, SAS/GRAPH procedures are quite similar to the SAS procedures which do plots and charts on the line printer. For example, to produce a printer scatter plot of WEIGHT against HEIGHT for a dataset CLASS, you would use PROC PLOT:

Title 'Weight in relation to Height';
Proc PLOT data=CLASS;
     Plot WEIGHT * HEIGHT;

To produce the same graph with SAS for Windows (or on a graphics terminal or the Zeta plotter), you could simply use PROC GPLOT instead of PROC PLOT:

Title 'Weight in relation to Height';
     Plot WEIGHT * HEIGHT;

SAS/GRAPH procedures have extensive capabilities for controlling the features of the plots and for enhancing graphic output. Most of these have reasonable defaults, so if graphics is new to you, you can get immediate results without knowing many of the details. As you become more experienced, you can begin to specify some of the available options to tailor the displays to suit your needs.

Device-independent graphics with SAS/GRAPH

Producing graphics displays on display terminals and hardcopy devices such as plotters and laser printers is highly dependent on the particular device. For example, terminals, printers and plotters differ in resolution, available colors, built-in character sets, etc. To provide a device-independent facility for producing graphics, SAS/GRAPH uses "device drivers", which translate generic graphics instructions into the specific codes required for a particular device.

To select the device driver used by SAS/GRAPH, specify the DEVICE=devicedrivername option with the GOPTIONS statement. All SAS/GRAPH procedures then use the selected device driver. When developing a graph, it is usually convenient to preview the graph on the screen with the same formatting that will be used when the graph is printed. You can do this by specifying the hardcopy device driver with the TARGETDEVICE= option.

PsychLab facilities for SAS/GRAPH

With SAS for Windows, the WIN device driver formats a graph for display on the screen; The WINPRTG driver formats a graph for printing on an 8.5"x11" sheet using the HP/4M laser printer. In the PsychLab the default is
goptions device=win targetdevice=winprtg;
which means that the graph is automatically displayed on the screen and you can print a hardcopy simply by selecting PRINT from the FILE menu in the GRAPH window (not the main SAS window). (The TARGETDEVICE= option formats the graph for the printer, so what you see on-screen approximates what will print.) Before you attempt to print a graph, first use the Windows Print Manager or FILE --> Printer Setup from the SAS pulldown menu to select the HP/4M as the Window printer.

Other device drivers are also available which make it easy to export a graph from SAS to WordPerfect or other Windows applications. E.g., WP and Harvard Graphics accept computer graphics metafiles (CGM) for import. The drivers CGMWPWA (for WP) and CGMHGWA (for HG) are set up to produce the proper graphic format for input to these programs. (The advantage of using these special drivers, as opposed to using Copy/Paste, is that the CGM files can be manipulated or edited graphically, whereas graphs copied to the clipboard are stored as a bitmap, which cannot be easily edited or resized.)

There is also local information on facilities for using SAS/GRAPH with the Zeta plotter on CMS, MVS and other computers.

Sample SAS/GRAPH program

A SAS/GRAPH program typically consists of the following steps:
  1. Select device driver and set graphics options with the %GSTART macro or the GOPTIONS statement. [The default drivers are selected automatically with SAS for Windows. However, you may wish to specify other graphic options, such as fonts, plot dimensions, etc.]
  2. Create the dataset to be plotted with DATA step(s).
  3. Plot with SAS/GRAPH procedures.

For example, the program below plots a "serial position curve" depicting percent recall of words in a list as a function of the position of each word in that list. In addition to plotting individual points, a smooth curve is drawn using a spline technique.

goptions border vsize=7in  hsize=7.5in;*-- omit in PsychLab;
title h=1 c=red 'Serial position curve for Free Recall Lab';
data serial;
     position = _n_;
     input recall  @@;
     recall = 100 * (recall / 23);     /* convert to % */
20 19 19 16 13 17 15 10 10 15 13 15 12 14 9 13 8 11 15 18 17

symbol i=sm50   v=star; /* smooth spline for noisy data */
proc gplot data=serial;
     plot recall * position
          / vminor=0 vaxis=0 to 100 by 10
            hminor=0 haxis=1 to 21 by 1;
%GFINISH;*-- omit in PsychLab;

The GOPTIONS statement specifies that a BORDER is to be drawn around the plot, and indicates that the plots are to be 7.5 by 7 inches rather than the default (8.5 by 11 for the WINPRTG driver).

The data step reads in the recall data, converts it to percent, and creates the POSITION variable. The TITLE statement gives a title for the plot. The options H=1 C=RED indicate the height and color of characters in the title.

PROC GPLOT is called to plot RECALL against POSITION. Options on the PLOT statement specify how the values on the axes are to be labelled. The SYMBOL statement specifies V=STAR to indicate that the points are plotted with a * character, and I=SM50 indicates the interpolation method to draw a smooth curve should be drawn through the points; SM50 uses a smooth spline curve which is appropriate for noisy data.

Overview of SAS/Graph General Statements

A number of SAS/GRAPH general statements are used to define the characteristics of graphics displays, including titles, plotting symbols, axes, labels, etc. SAS/GRAPH provides reasonable defaults for most of these characteristics. Using these statements allows you to tailor graphics output to meet your needs.

Among the SAS and SAS/GRAPH general statements available, the most commonly used are:

GOPTIONS options;
Select general graphics initializations, including DEVICE, defaults for colors, font and height to use for TITLES, etc. [ SAS/GRAPH Software: Reference Chap. 5]

TITLEn options 'text';
Produces titles; can be used with all all GPROC. As with all SAS procedures, titles defined with TITLE statements continue to be used in all subsequent output until redefined. [ SAS/GRAPH Software: Reference Chap. 17]

FOOTNOTEn options 'text';
Footnotes are like titles, but are written at the bottom of the page or graph. FOOTNOTE1, FOOTNOTE2, etc. are "pushed up" from the bottom. The space remaining between TITLEs and FOOTNOTEs defines the plot window, i.e., the actual plot is scaled to fit the space remaining. Footnotes can be used in all GPROC; like titles, they remain in effect until redefined. [ SAS/GRAPH Software: Reference Chap. 11]

NOTEn options 'text';
Notes are written inside the plot window. Usually used with GSLIDE, but can be used with all GPROC. [ SAS/GRAPH Software: Reference Chap. 14]

SYMBOLn options;
Used with PROC GPLOT. SYMBOL statements give PROC GPLOT information about plot characters, plot lines, color and interpolation or smoothing of lines between points. [ SAS/GRAPH Software: Reference Chap. 16]

AXISn options;
Used to define the appearance of an axis in a plot or chart. You can control the axis label, values plotted on the axis, number and spacing of tic marks, etc. [ SAS/GRAPH Software: Reference Chap. 9]

LEGENDn options;
Legends are used in plots, charts and maps to identify points, lines and regions which are plotted. The LEGEND statement controls the appearance of these legends. [ SAS/GRAPH Software: Reference Chap. 13]

PATTERNn options;
Pattern statements are used to define colors and "fill patterns" such as cross-hatching that are used with the GCHART, GCONTOUR, GMAP, GPLOT procedures. [ SAS/GRAPH Software: Reference Chap. 15]

Overview of SAS/GRAPH Procedures

The available SAS/GRAPH procedures are described below in application categories: The general format for the SAS/GRAPH procedures is:
  PROC G____ DATA=SASdataset
  gplot_spec  request/options;
  other specifications;
the "other specifications" include:
   AXIS   how to draw and label the axes
   SYMBOL specify symbols and lines in the plot
   TITLE  plot titles

© 1995 Michael Friendly

Author Michael Friendly
Last Updated:Friday, July 19, 1995

[Next] SAS/Graph Syntax Summry.