Introduction to SAS/GRAPH
Michael Friendly
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';
Proc GPLOT data=CLASS
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.
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.
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.
A SAS/GRAPH program typically consists of the following steps:
- 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.]
- Create the dataset to be plotted with DATA step(s).
- 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 % */
cards;
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.
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]
The available SAS/GRAPH procedures are described below in
application categories:
- Charting & plotting
- GPLOT
- Produces two-dimensional scatter plots,
line plots (a smooth curve, with no points),
bubble plots (size of bubble proportional to a
third variable). For scatter plots, PROC
GPLOT can also fit and draw a regression curve
(linear, quadratic or cubic) together with a
confidence band for the regression curve. [ SAS/GRAPH Software: Reference
Chap. 31]
- GCHART
- Vertical & horizontal bar charts
(histograms), pie, star and block (3-D)
charts. [ SAS/GRAPH Software:
Reference Chap. 23]
- Three-dimensional plotting
- G3D
- Three-dimensional graphs of functions
defined over a grid of X - Y values, or 3D
scatter plots. [ SAS/GRAPH Software:
Reference Chap. 39]
- G3GRID
- Interpolates values from an irregularly
spaced set of X - Y points, generating a
rectangular grid of values for PROC G3D.
[ SAS/GRAPH Software: Reference]
- GCONTOUR
- Produces a contour plot of a
three-dimensional relationship, in which the
height of a function is represented by contour
curves on a two-dimensional plot. [
SAS/GRAPH Software: Reference Chap. 24]
- Mapping
- GMAP
- Draws two-dimensional (chloropleth) maps
in which the color or shading in each region
is determined by the value of some variable,
or three-dimensional (surface, block, and
prism) maps which depict the value of a
variable by height of the map. [
SAS/GRAPH Software: Reference Chap. 29]
- GPROJECT
- Applies one of several map projection
methods to a map data set consisting of
coordinates expressed in latitude and
longitude. [ SAS/GRAPH Software:
Reference Chap. 33]
- GREDUCE
- Thins out a map coordinate data set so you
can reduce the number of data points needed to
draw the map, so the map requires less storage
and drawing time. [ SAS/GRAPH Software:
Reference Chap. 34]
- GREMOVE
- Used to remove map borders and combine
unit areas in a map into larger areas (e.g.,
amalgamating counties into states or
provinces) producing a new map data set.
[ SAS/GRAPH Software: Reference
Chap. 35]
- Printing and displaying
- GPRINT
- Can turn the output of any SAS procedure
into a graphic, enhanced with TITLEs,
FOOTNOTEs and NOTEs. [ SAS/GRAPH
Software: Reference Chap. 32]
- GREPLAY
- A full-screen tool that helps manage a
catalog of graphs produced by SAS/GRAPH and
saved as disk files. PROC GREPLAY can also be
used to combine several separate graphics into
one, using a "template" facility, or
to scale or rotate other graphics. [
SAS/GRAPH Software: Reference Chap. 36]
- GSLIDE
- Graphic output consisting of lines of
text, as in slides, overheads and posters.
[ SAS/GRAPH Software: Reference
Chap. 37]
- GANNO
- Produces a graphic defined entirely from
an ANNOTATE dataset. Similar to GSLIDE, but
TITLE, FOOTNOTE, and NOTE statements are not
used. [ SAS/GRAPH Software:
Reference Chap. 22]
- Graphing utilities
- GOPTIONS
- PROC GOPTIONS displays the SAS/GRAPH
GOPTIONS which are in effect. [
SAS/GRAPH Software: Reference]
- GFONT
- Used to create a character set or font for
use with SAS/GRAPH procedures, and to display
a font created with PROC GFONT. [
SAS/GRAPH Software: Reference]
- GIMPORT
- Converts graphics output produced by other
applications to a form which can be used by
SAS/GRAPH [ SAS/GRAPH Software:
Reference Chap. 27]
- GTESTIT
- A diagnostic procedure to display a test
pattern on a device to determine if the device
is correctly configured. [ SAS/GRAPH
Software: Reference Chap. 38]
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
Email<friendly@yorku.ca>
Last Updated:Friday, July 19, 1995
SAS/Graph Syntax Summry.