Beruflich Dokumente
Kultur Dokumente
Contents
1. Introduction ................................................................................................................................................. 3
1.1
About This Text ................................................................................................................................. 3
1.2
Objectives ......................................................................................................................................... 3
1.3
Example Files ................................................................................................................................... 3
2. Graphical Facilities in the SAS System........................................................................................................ 4
3. Bar-charts and Histograms .......................................................................................................................... 4
3.1
The GCHART Procedure .................................................................................................................. 4
3.2
Graph Refinement............................................................................................................................. 6
3.3
Grouped Bar-Charts ........................................................................................................................ 14
4. Scatter Plots and Line Plots ...................................................................................................................... 15
4.1
The GPLOT Procedure ................................................................................................................... 15
4.2
Multiple Plots .................................................................................................................................. 18
5. Surface Plots and Contour Plots................................................................................................................ 20
5.1
The G3D Procedure ........................................................................................................................ 20
5.2
The GCONTOUR Procedure........................................................................................................... 22
6. Maps ........................................................................................................................................................ 23
6.1
The GMAP Procedure ..................................................................................................................... 23
7. Graphics Output ........................................................................................................................................ 25
7.1
Graphics Catalogues ...................................................................................................................... 25
7.2
Graphics Stream Files..................................................................................................................... 25
7.3
Creating Output for Inclusion in Documents and Slides................................................................... 25
7.4
Creating Output for Display on the Web .......................................................................................... 26
8. Annotate Data Sets ................................................................................................................................... 31
8.1
Annotate Data Set Variables ........................................................................................................... 31
9. Guide to Further Study .............................................................................................................................. 34
Annex 1: Summary of Examples .................................................................................................................... 35
Annex 2: Fonts ............................................................................................................................................... 36
Format Conventions
In this document the following format conventions are used:
Menu items and commands that you must type Name
in are shown in bold
<Cancel> or <OK>
Keys that you press and options that you
select are enclosed in angle brackets.
Feedback
If you notice any mistakes in this document please contact the Information Officer. Email should be
sent to the address info-officer@leeds.ac.uk
Copyright
This document is copyright University of Leeds. Permission to use material in this document should be
obtained from the Information Officer (email should be sent to the address info-officer@leeds.ac.uk)
Page 2 of 36
Version 1.1 (Feb 2007)
tut122.doc
1.
Introduction
1.1
This text describes the basics of using SAS for graphics and data visualisation. It is one of a series of
documents produced at Leeds describing the SAS system. It is assumed that the user has already acquired a
basic familiarity with the SAS system at least to the level offered either by the introductory course SAS1 or
covered by the document TUT120 An Introduction to SAS: Part 1 - Basics.
1.2
Objectives
1.3
Example Files
A variety of SAS programs and data sets will be used for these exercises. These files are stored in a zip file
named sasgraph.zip on the ISS web site. Before starting, it is advised that you download the zip file to your
hard disk. To download the file, open a web browser and go to http://iss.leeds.ac.uk/software
Click on PDF files (suitable for printing), then scroll down SAS example files and right-click on sasgraph.zip.
Select Save Link As (Save Target As in Internet Explorer). When the Save As dialog box appears,
select a suitable directory (see below) and click Save. Go into Windows Explorer and double click the zip file.
Click File/Extract All to unzip the files.
A directory such as C:\courses\sas\graph is recommended as the location in which to save the data files. If
you are working in a public cluster, you may find it necessary to use the Temp directory instead (eg use
C:\temp\sas\graph). This is the path used in the example programs. If you store your files in a different
directory, you will need to change the Filename and Libname statements used in the example files
accordingly.
To start SAS, locate SAS from the Statistics menu and double-click to open SAS. After a short period, the SAS
data editor window will be displayed.
Page 3 of 36
Version 1.1 (Feb 2007)
tut122.doc
2.
A variety of facilities for producing graphs and for visualising data are provided in the SAS system.
SAS/GRAPH is the primary graphical facility and consists of a range of graphical procedures catering for
specific types of graph. These include bar-charts, histograms, scatter plots, line plots, surface plots, contour
plots and maps. In addition, SAS/GRAPH offers a range of facilities to support the customisation of graphs.
SAS/INSIGHT is an interactive tool for statistical analysis and data exploration. Graphical tools include
distribution fitting, curve fitting, confidence ellipses for bi-variate data and three-dimensional rotating plots.
Enterprise GUIDE is a menu-driven module for analysis and graphics which may be installed on a client PC
which is not equipped with its own SAS installation. GUIDE provides a point-and-click alternative to
SAS/GRAPH for users requiring standard graphical facilities.
In addition to SAS/GRAPH, SAS/INSIGHT and Enterprise GUIDE, a number of other components of the SAS
system offer graphics capabilities. These include the DATA step, SAS/IML (the interactive matrix language),
certain statistical procedures in SAS/STAT which offer options to produce graphical output and specialised
modules such as SAS/QC and SAS/OR which have their own built-in graphics capabilities.
For the majority of users, SAS/GRAPH should cover most of their requirements. The emphasis of the course is
therefore concentrated on SAS/GRAPH. However, a number of locally produced documents are also available
covering a variety of specialist topics. These include documents giving brief introductions to SAS/INSIGHT and
Enterprise GUIDE. For further details see Chapter 9.
In order to use any of these products, your data must reside in SAS data sets. Thus, familiarity with the tools
for creating SAS data sets is a pre-requisite for using these products. (Note: SAS/INSIGHT offers the ability to
create data sets by entering data into a worksheet interactively, thus side-stepping the need to use either the
SAS Import Wizard or SAS DATA steps, which are the main means of creating SAS data sets).
The text is primarily task oriented. Each Chapter corresponds to a specific graphic application and introduces
tools as and when appropriate. To avoid burdening the user with detail, simplified forms of the syntax of the
various procedures are used. The user is referred to either the SAS manuals or the on-line documentation for
complete details of all commands.
3.
3.1
Bar-charts and histograms are used to display univariate frequency distributions of random variables. For a
discrete random variable (one for which it is meaningful to identify the specific values that the variable may
take) a bar-chart is the appropriate device, allowing one bar to be displayed for each different value observed
in the variable. For variables which take on a continuum of possible values, or for discretely valued variables
for which some grouping of values into ranges is required, the histogram is more appropriate.
The SAS/GRAPH procedure GCHART provides for both of these displays.
The basic syntax of the procedure is as follows:
PROC GCHART DATA=<data set> ;
VBAR (or HBAR or VBAR3D or HBAR3D) <variable> / <options>;
The sub-commands vbar and hbar specify vertical and horizontal charts respectively. The alternatives vbar3d
and hbar3d produce 3-dimensional charts.
<variable> represents the name of the variable being charted.
<options> represents a list of one or more keywords representing optional actions.
Page 4 of 36
Version 1.1 (Feb 2007)
tut122.doc
3.1.1 Examples
Example 1: A Simple Histogram
The data set Iris, described in Annex 1, contains various measurements on three varieties of iris plant. The file
histo1.sas contains the following program to produce a simple histogram showing the distribution of values of
the variable sepallen across all varieties of iris.
(NOTE: If you have stored the example programs in the directory c:\temp\sas\graph, you should be able to
execute all the programs without modification. Otherwise, you will need to alter the directory specified by the
LIBNAME statement to that which you have used in order for them to work).
Step 1: Open the file histo1.sas. Check that the code corresponds to that shown below (heeding the note
above).
Step 2: Submit the program by pressing F8.
The result should correspond to that shown below.
libname sasgraph c:\temp\sas\graph;
proc gchart data=sasgraph.iris;
vbar sepallen;
run;
The midpoints of the bars are determined
automatically by the GCHART procedure.
The label on the X-axis was defined by a
LABEL statement in the DATA step used to
create the data set. Since no specification of
colour was made, the choice of colour was
determined from a default list of colours.
Page 5 of 36
Version 1.1 (Feb 2007)
tut122.doc
In both of these charts, attributes such as colour and axis details have been determined automatically by SAS.
The next section illustrates the process of graph refinement and describes the tools available for the process.
3.2
Graph Refinement
A variety of options are offered by SAS/GRAPH for refinement of a graph. They fall into two categories
global options which can apply to graphs produced by different procedures and local options specific to
individual procedures. Examples of the former include options specified by GOPTIONS, PATTERN, SYMBOL,
AXIS, TITLE, FOOTNOTE, NOTE and LEGEND statements. All global statements may appear anywhere
within a SAS program. They may be located either within the scope of the procedure step or they may be
located external to and before the step to which they apply.
3.2.1
The GOPTIONS statement may be used to specify options affecting graphs produced by any procedure. The
statement has the form:
GOPTIONS
<option list>;
Page 6 of 36
Version 1.1 (Feb 2007)
tut122.doc
BORDER
|NOBORDER
CBACK=<colour>
COLORS=
( list of colour names)
DEVICE=
DISPLAY
|NODISPLAY
GUNIT
HSIZE=
VSIZE=
HPOS=
VPOS=
RESET
ROTATE
|NOROTATE
Page 7 of 36
Version 1.1 (Feb 2007)
tut122.doc
3.2.3
There is one particular global statement that applies to procedures that plot points, such as PROC GPLOT,
PROC G3D and others, and that is the SYMBOL statement. Its purpose is to describe the attributes of the
points and lines such as colour and size.
The syntax of the SYMBOL statement (in simplified form) is as follows:
SYMBOL
C=<colour>
V=<plot symbol> I=<Interpolation style>
L=<line style> W=<width> ;
where
<colour> is a legitimate colour name (see section 2.2 for a description of colour names)
<plot symbol> is either the name of a recognised symbol or a single character enclosed in quotes (eg *).
Recognised symbol names are: BALLOON, CLUB, CROSS, CUBE, CYLINDER, DIAMOND, FLAG, HEART,
PILLAR, POINT, PRISM, PYRAMID, SPADE, SQUARE and STAR.
<Interpolation style> defines how points are to be connected. The options are as follows:
I=JOIN
I=NONE
I=SPLINE
I=SMnn
I=HILO
I=R<xxxxxx>
<line style> specifies a number indication one of 40 different lines styles. These are described in the
SAS/GRAPH User Guide, Volume 1.
<width> specifies a number indicating the thickness of lines.
The options may be specified in any order.
3.2.4
It is possible to use options specific to procedures to define axis attributes, However, maximum control over
the attributes of axes is obtained by the use of AXIS statements. The AXIS statement allows the user to
specify things such as labels, tick mark positions, logarithmic axes, colours and sizes of characters collectively
in one statement.
Page 8 of 36
Version 1.1 (Feb 2007)
tut122.doc
keyword1= specification
keyword2= specification
..
;
The range of options available is too extensive to describe in full in a summary document of this kind. Instead,
the examples that follow illustrate the use of some of the more commonly used options. For a full description of
the AXIS statement and its options the reader is referred to the SAS/GRAPH User Guide, Volume 1 or to the
SAS Online Documentation.
3.2.5
Titles, footnotes and, in certain types of graph, other notes may be added to graphs using the TITLE,
FOOTNOTE and NOTE statements. They share a common syntax illustrated here (in simplified form) using
the TITLE statement:
TITLE F=font C=colour H=height J=position A=angle R= angle Text
M=location D=location;
or
TITLEn F=font C=colour H=height J=position A=angle R= angle Text
M=location D=location;
where, in the second form of the statement, n is an integer in the range 1 to 19, and
<font> specifies a character font. Examples of some commonly used fonts are provided in Annex 3.
<colour> is a legitimate colour name
<height> is a number indicating the text size
<position> is L, C or R indicating left-justified, centred or right justified.
A=<angle> specifies an angle through which the line of text is to be rotated.
R=<angle> specifies an angle through which each character within the line of text is to be rotated.
M=<location> indicates that the starting point for the text to be drawn should begin at the location specified.
D=<location> requests that a line be drawn from the current position to the location specified.
For both the A= and R= options, angles are specified as degrees in the range x to y.
For the M= and D= parameters, locations can be specified either in absolute terms (eg (3,8)) or relative to the
current position (eg (+3,+0)).
If the second form of the statement is used, multiple statements with different values of n may be used. Thus,
the use of TITLE and TITLE2 would yield a main title followed by a sub-title.
Titles and footnotes may be cleared at any stage by re-issuing the corresponding statement without
parameters (eg TITLE; or TITLE3;). Issuing a TITLEn; (or FOOTNOTEn;) statement will clear all titles (or
footnotes) of level 3 or below.
Not all of the parameters are required. In addition, some parameters may be repeated. Thus it is possible to
change the colour of text within the line by using more than one C= parameter.
Page 9 of 36
Version 1.1 (Feb 2007)
tut122.doc
3.2.6
Legends are provided automatically by most SAS/GRAPH procedures. However, customised legends may be
produced using the LEGEND statement. The full form of the LEGEND statement is too extensive to describe
here. The reader is advised to consult the SAS/GRAPH User Guide, Volume 1, for a full discussion. However,
the following is a very simplified form of the statement :
LEGEND
where
ACROSS=n specifies the number of legend entries in a row
DOWN=n specifies the number of legend entries in a column
SHAPE=<shape> specifies the size and shape of the legend values displayed in the legend. Valid values for
<shape> are BAR(width, height), LINE(length) and SYMBOL(width, height).
POSITION=(<position details>) positions the legend on the graph. The <position details> consists of a triple
of keywords, one from each of the three sets (BOTTOM, MIDDLE, TOP), (LEFT, CENTER, RIGHT) and
(OUTSIDE, INSIDE). The default is (BOTTOM CENTER OUTSIDE).
Additional options allow the style of text and values in legends to be specified in detail.
Examples of the use of these global statements, and descriptions of procedure-specific options, are provided
in the examples below and in subsequent sections.
3.2.7
(ii)
Submit the program (F8) to create the data set called cities.
The following sequence of steps illustrates the use of some of the options described above for graph
refinement.
Step 2: Create a Chart Using Default Options
The simplest specification requires just one option, the SUMVAR option, to specify the variable to represent on
the vertical axis.
(i)
Open the file cities1.sas. Check that the code corresponds to that shown below.
Page 10 of 36
Version 1.1 (Feb 2007)
tut122.doc
(ii)
Step 3:
Using GOPTIONS and procedure options to control the appearance of the chart
The appearance of the graph can be improved by the use of appropriate procedure options.
(i)
Open the file goptions.sas. Check that the code corresponds to that shown below.
(ii)
The graphics options HSIZE and VSIZE define a plot size of 6 inches by 4 inches. These values will
remain in force for all graphs, unless re-set by a new specification.
The DISCRETE option forces each value of the graph variable to appear in the graph and causes a barchart to be produced instead of a histogram.
The SUBGROUP statement forces a different colour to be used for each bar. The choice of colour for the
bars has been left for SAS to decide. (The choice of colours is determined by the colours specified by the
SAS/GRAPH system option COLORS= option. The GOPTIONS statement can be used to change the
default values assigned by SAS).
The ASCENDING option orders the bars in increasing order of the response variable.
The SPACE and WIDTH options of the VBAR statement control the spacing and width of the bars.
Page 11 of 36
Version 1.1 (Feb 2007)
tut122.doc
The graph still leaves much to be desired. Labels explaining the meaning of the bars are required and some
attention to attributes of the response axis is required. Also, we may want to change the colour and the
shading styles used for the bars.
Step 4:
Since there are five bars in the chart, five PATTERN statements are required to define individual colours and
shading styles for each bar.
(i)
Open the file pattern.sas. Check that the code corresponds to that shown below.
(ii)
Step 5:
The use of numerical codes on the horizontal axis is not informative. The use of self-explanatory labels is
recommended instead. There are several ways to achieve this. One way is to format the labels using a user
defined format.
(i)
(ii)
(iii)
Open the file format.sas. Check that the code corresponds to that shown below.
(iv)
A format statement is included in the GCHART step to associate the labels with the values of the variable
city. (Note the existence of a full stop directly after the format name. This is essential to distinguish the format
name from the name of a SAS variable). The nolegend option is also added to suppress the automatic legend
which is redundant now that meaningful labels have been added.
Page 12 of 36
Version 1.1 (Feb 2007)
tut122.doc
The resulting graph is a big improvement on our first attempt but could still be improved. Note the tick marks
on the response axis. Do we really need such detail? Also, the response axis label is not helpful.
Step 6: Axis refinement using AXIS statements
As indicated in 3.2.4, maximum control over axes is obtained by the use of AXIS statements. We can improve
on the previous chart by using an AXIS statement for each of the axes.
(i)
Open the file axis.sas. Check that the code corresponds to that shown below.
axis1 value = (f=duplex h=1 c=blue)
order = (0 to 70 by 10)
minor = (N=1)
label =(f=duplex h=1 c=blue Cases);
axis2 value = (f=duplex h=1 c=blue)
label =(f=duplex h=1 c=blue City);
The AXIS statements specify the font, the character size, the colour and the labelling of the Y-axis. The
AXIS1 statement also controls the position of tick marks on the Y-Axis.
(ii)
(iii)
Open the file refine.sas. Check that the code corresponds to that shown below. The raxis and
maxis options are added to use the AXIS statements defined above.
Page 13 of 36
Version 1.1 (Feb 2007)
tut122.doc
It perhaps should be added that the use of strong and distinct primary colours is not always appropriate. The
use of different shades of a single colour or simply black and white may often provide a better alternative.
Also, the need to use colours that remain distinct when viewed by a web browser is a vital consideration when
producing graphics for display on the Web. Some advice on this topic is provided in Chapter 5.
3.3
Grouped Bar-Charts
Simple bar-charts and histograms display the information relating to a single variable. Often, similar
information is available for two or more groups. In this case, the GROUP option can be used to produce a set
of bar charts or histograms side-by-side.
Example 4: A Grouped Bar Chart
The file sales.sas contains the following program to create a SAS data set containing data relating to sales
figures from three different cities over three successive years.
It is required to create a Grouped Bar Chart displaying this data.
data cities;
input city $char8. year sales;
cards;
London
1990
5000
London
1991
8000
London
1992
21000
Paris
1990
18000
Paris
1991
26000
Paris
1992
36000
New York
1990
7000
New York
1991
20000
New York
1992
27000
run;
Step 1:
Open the file sales.sas. Check that the code corresponds to that shown above.
Step 2:
Check the LOG window to make sure that there are no errors and that the data set has been created
successfully.
Step 3:
Open the file groupbar.sas. Check that the code corresponds to that shown below.
Page 14 of 36
Version 1.1 (Feb 2007)
tut122.doc
Step 4:
Sales
40000
30000
20000
10000
0
1990 1991 1992
London
19901991 1992
New York
1990 19911992
Paris
year
city
Either of the variables year and city could serve as the GROUP variable. In this example, year is chosen as
the GROUP variable. The SUBGROUP option is used to force a different colour to be used for each city.
If a second categorical variable is available, the SUBGROUP option can be used to sub-divide the verticals of
each bar, although the readability of resulting graphs may leave something to be desired. Alternatively, a
further option of the GCHART procedure, the BLOCK chart, may be used. It is also possible to use a third
variable as a sub-group variable to sub-divide the blocks within a block chart. However, the conventional
wisdom of graphics experts is that, even with the two-dimensional form of the chart, interpretation of block
charts can be difficult even when the number of levels of each variable is very small.
4.
4.1
The SAS/GRAPH procedure GPLOT caters for a variety of plot types including scatter plots, line plots and
bubble plots.
For scatter plots and line plots, there are three basic styles of syntax:
(i)
(ii)
/ <options>;
(iii)
Page 15 of 36
Version 1.1 (Feb 2007)
tut122.doc
<group variable> represents the name of a variable whose values distinguish one or more groups into which
the data falls. A separate plot is produced for each group.
<options> represents a list of one or more keywords representing optional actions.
Commonly used options are:
OVERLAY
REF=
NOLEGEND
HAXIS=
VAXIS=
HREF=
VREF=
In addition to procedure options, other options may also be specified using the variety of global statements
described in section 2.2 above.
4.1.1 Examples
Example 5: A line-plot of experimental data
The file curves.sas contains the following program to read four sets of experimental measures taken at
monthly intervals. The corresponding file curveg.sas contains a program to plot one set of measures together
with a smooth curve showing the general pattern in the data. (A later example illustrates how all four sets of
measures can be plotted on one graph).
data curves;
input x y1 y2
cards;
1
10
14
2
2.5 13.5
3
1.0
13
4
1.5
12
5
3.5
11
6
6 10.5
7
7.2
10
8
8 10.5
9
8.5 11.5
10 9.5
13
11 10.5
14
12 12
15
run;
y3 y4;
5
4
3.5
3
3
3.5
4
4.5
6
7
8
8.5
16
12
8.7
6
4
3
2
1.5
2
3
4
5
Step 1:
Open the file curves.sas. Check that the code corresponds to that shown above.
Step 2:
Check the LOG window to make sure that there are no errors and that the data set has been created
successfully.
Step 3:
Open the file curveg.sas. Check that the code corresponds to that shown below.
Step 4:
The resulting plot (shown below) contains the data values together with a smoothed curve superimposed.
This is obtained by replicating the plot specification (Y*X) and using the OVERLAY option.
Page 16 of 36
Version 1.1 (Feb 2007)
tut122.doc
SYMBOL1 specifies that blue stars are to be used for the point. The interpolation option I=NONE suppresses
any connection between the points. In the second SYMBOL statement, the points are suppressed using
V=NONE but a smooth red curve, specified by C=RED, connecting the points is requested using a spline
smoothing algorithm, specified by I=SPLINE. For further details on the use of splines for smoothing data, see
Fitting Curves and Surfaces Using SAS Software. Copies of this document may be downloaded from the ISS
web site.
Example 6: A logarithmic plot with multiple axes
The following example is taken from the SAS sample library and illustrates the use of the logstyle option to
plot on a logarithmic scale.
The example illustrates how two plot sub-commands can be used to show the data values on both the original
data scale and on the log scale.
Step 1:
Open the file logplot.sas. Check that the code corresponds to that shown below.
Note the data set used sampsio.enprod2. This is part of the SAS sample library of data sets. You can
inspect this data by issuing the LIB command from the command box and browsing the contents of the
sampsio library.
Step 2:
Page 17 of 36
Version 1.1 (Feb 2007)
tut122.doc
4.2
Multiple Plots
The second and third styles of the PLOT statements are appropriate if multiple plots are required on the same
graph. The choice of style of PLOT statement depends upon the structure of the data. If the data for different
plots are stored in separate pairs of variables, style (ii) is appropriate. Style (iii) caters for an alternative
structure in which the data are stored in one pair of variables and a third variable, the group variable, is used
to indicate the particular plot to which an individual case belongs.
Example 7: Multiple plots using individual pairs of variables
The data introduced in example 5 contains monthly observations on four variables. GPLOT can be used to
produce curves for each of the four variables superimposed on the same graph.
Step 1:
Open the file mplot1.sas. Check that the code corresponds to that shown below.
Step 2:
Page 18 of 36
Version 1.1 (Feb 2007)
tut122.doc
The OVERLAY option is essential to force GPLOT to print multiple graphs in the same frame.
The L= option determines the line style of each line. The width of each line is assigned a value of 2.
A limitation of this style of the PLOT statement is the absence of an automatic legend. There are two ways to
overcome this. The first way is to re-shape the data so that the third style of the PLOT statement can be used.
The next example illustrates this style. The second way is to use the ANNOTATE data set facility. This is
described later in the Chapter describing the use of annotate data sets. (Also, note that the Y axis label could
be improved by specifying a more appropriate label using an AXIS statement).
Example 8: Multiple plots using a grouping variable
If the data is stored in one pair of variables with group membership indicated in a third variable, the third style
of PLOT statement can be used. If the data is not already in group format, a preliminary DATA step can be
used to re-shape the data into the required format. The program in file mplot2.sas illustrates what is required.
Starting with the data from Example 5, the following DATA step re-shapes the data into a new data set with
three variables. The third variable, line, indicates the line or plot to which the corresponding data point
belongs.
data lines (keep = x y line);
set curves;
array dummy(line) y1 y2 y3 y4;
do line=1 to 4;
y=dummy;
output;
end;
run;
Step 1:
Step 2:
The plot legend can be improved by using a format to replace the numerical values of line by meaningful
labels. This can be achieved by defining a format using PROC FORMAT and using a FORMAT statement
within the GPLOT step to associate labels with the values of the variable line.
The following code, stored in file mplotft.sas, defines a format lineft for this purpose.
proc format;
value lineft
1=Control
2=Low
3=Medium
4=High;
run;
Step 3:
Step 4:
Step 5:
Open the file mplotg.sas. Check that the code corresponds to that shown below.
Step 6:
Page 19 of 36
Version 1.1 (Feb 2007)
tut122.doc
5.
5.1
The G3D procedure produces surfaces plots, needle plots or scatter plots of three-dimensional numerical data.
There are two basic styles of syntax:
(i)
Additional options such as HREF, VREF, HAXIS and VAXIS, as defined for the GPLOT procedure, may also
be used.
Example 9: Surface plot
(i)
(ii)
Press F8 to execute the program. The GRAPH window should contain the 3-dimensional (saddleshaped) surface shown below.
Page 20 of 36
Version 1.1 (Feb 2007)
tut122.doc
data swirl;
do x=-5 to 5 by 0.25;
do y=-5 to 5 by 0.25;
if x+y=0 then z=0;
else z=(x*y)*((x*x-y*y)/(x*x+y*y));
output;
end;
end;
run;
proc g3d data=swirl;
plot y*x=z /ctop=blue cbottom=red;
run;
(ii)
NONEEDLE
SHAPE=
SIZE=
NOAXIS
Open the file scatter1.sas and submit the program. The DATA step, shown below, reads the data
and assigns values to variables colorval and shapeval to control the colour and shape of the plot
symbols.
filename ina c:\temp\sas\graph\iris.dat;
data iris;
length shapeval colorval $ 8;
infile ina;
input sepallen sepalwid petallen petalwid species;
if species=1 then
Page 21 of 36
Version 1.1 (Feb 2007)
tut122.doc
do;
shapeval='balloon'; colorval='blue' ;
end;
if species=2 then
do;
shapeval='cube' ;
colorval='red';
end;
if species=3 then
do;
shapeval='pyramid'; colorval='green';
end;
run;
Open the file scatter2.sas (shown below) and submit the program by pressing F8.
PROC G3D produces a scatter plot using sepallen as the Z-axis variable and petallen and petalwid as the Y
and X axis variables respectively. The noneedle option of G3D suppresses lines connecting the points to the
x-y and the size option controls the size of the symbols. The color and shape options specify the names of
the variables containing colour and shape specifications.
goptions reset=global ftext=swiss ;
colors = (black blue green red) ;
proc g3d data=iris;
scatter
petallen*petalwid=sepallen/
noneedle size=1
color = colorval
shape = shapeval;
run;
5.2
The GCONTOUR procedure produces contour plots representing three-dimensional relationships in two
dimensions. The syntax, which is similar to that of the G3D procedure, takes the form:
proc gcontour data=<data set> ;
plot x*y=z / <options>;
Commonly used options are:
CLEVELS=
LEVELS=
LLEVELS=
PATTERN
Page 22 of 36
Version 1.1 (Feb 2007)
tut122.doc
Open the file contour.sas and submit the program by pressing F8. The program and resulting graph
are shown below.
goptions reset=global
colors=(CX0000FF CX00FFFF CXCCFFFF
CX00FF00 CXFFFF00 CXFFCCCC
CXFF00FF)
ftext=swissb cback=white;
proc gcontour data=swirl;
plot y*x=z / pattern
coutline=gray
ctext=blue;
run;
For a further example of how to obtain contour plots, see the document Curve Fitting Using SAS Software.
6.
Maps
6.1
The GMAP procedure produces two-dimensional and three-dimensional maps. Types of maps available are
choropleth (2D) and surface, block, and prism (3D). The syntax takes the form:
proc gmap map = <map data set> data=<response data set> ;
id <id variables>;
choro | surface | block | prism <variable> / <options>;
where,
<map data set> is a data set containing digitiesd map boundary co-ordinates
<response data set> contains response data recorded for the areas being mapped
<id variables> identifies one or more variables in the input data sets that define map areas. These variables
must be present in both the map data set and the response data.
Commonly used options are:
Page 23 of 36
Version 1.1 (Feb 2007)
tut122.doc
CEMPTY=
COUTLINE=
DISCRETE
LEVELS=
MIDPOINTS=
MISSING
NOLEGEND
Page 24 of 36
Version 1.1 (Feb 2007)
tut122.doc
7.
Graphics Output
The default destination for a graph produced by SAS is the users screen. If hard-copy is required, the File
Print menu facility allows graphs to be printed directly to a connected printer.
However, this is just one of several possible output destinations to which graphics output can be routed. In
addition to displaying graphs on screen, or printing graphs directly to a printer, graphs may be saved in SAS
catalogues, or in Graphics Stream Files in a variety of formats for subsequent display on printers, for inclusion
in documents or slide presentations or for display on the world-wide-web.
7.1
Graphics Catalogues
A catalogue is a special SAS file containing objects of a particular kind. Catalogues are used by SAS for a
variety of reasonsIn particular, SAS/GRAPH uses catalogues to store graphs created by SAS/GRAPH
procedures. SAS/GRAPH procedures have a special option, GOUT=, which allows the user to specify the
name of a graphics catalogue in which the graph will be saved. Graphs stored in graphics catalogues may
subsequently be edited or combined with other graphs using PROC GREPLAY. For a description of the use of
PROC GREPLAY see the document Producing Composite Graphs Using SAS/GRAPH.
7.2
In addition to the PC screen, SAS supports a very wide range of graphics devices for printing or displaying
graphs. Examples are colour postscript printers, flatbed plotters and web browsers. In order to use one of
these devices, SAS first stores instructions required to create the graph in a file called a graphics stream file
(GSF). The nature of the instructions is dictated by the nature of the device specified by the DEVICE= option
specified on a GOPTIONS statement (see Chapter 2). Some of the more commonly used device drivers are
listed below.
Device
ACTIVEX
CGMOF97L
CGMOF97P
CLIPSA4
JAVA
WIN
Description
ACTIVEX enabled GIF Driver
CGM for Microsoft Office 97 - Landscape Mode
CGM for Microsoft Office 97 - Portrait Mode
HP Colour Laserjet Postscript - A4 Size
JAVA enabled GIF Driver
Microsoft Windows Display
The following additional options specify the name of the output file and the mode of output to the graphics
stream file.
Goption
GSFNAME
GSFMODE
7.3
Purpose
specifies a fileref for the output file
specifies the mode of output to the file.
Options are APPEND and REPLACE
A common requirement is to be able to include a graph in a document or in a slide presentation. There are a
number of ways in which this can be done, although the methods are not equal with respect to the quality of
the resulting end product.
Page 25 of 36
Version 1.1 (Feb 2007)
tut122.doc
original image. But they are generally not suitable if an enlarged image is required since they are inherently
limited by the screen resolution of the machine upon which they were produced. For quality reproduction
capable of withstanding magnification, a vector image is preferable.
Page 26 of 36
Version 1.1 (Feb 2007)
tut122.doc
7.4.3 Examples
Example 14: Using the JAVA device driver
Step 1: Open the file java1.sas. It contains the following Data step to read the revenue accruing from three
areas of the media market for each of five individual years.
data advertising;
format revenue dollar12.;
input media $ 1-8 year revenue ;
cards;
Papers
1985
25170
Papers
1990
32280
Papers
1995
36092
Papers
1996
38075
Papers
1997
41341
Radio
1985
6490
Radio
1990
8726
Radio
1995
11338
Radio
1996
12269
Radio
1997
13491
Misc
1985
12107
Misc
1990
15955
Misc
1995
20660
Misc
1996
22263
Misc
1997
23827
run;
Step 2:
Run the program to create the data set advertising.
Step 3:
Open the file java2.sas. Check that the program corresponds to that shown below.
This step requests output to be saved in HTML format in the file specified by the ods statement. It also
specifies java as the graphics device driver.
Step 4:
The graph, shown below, is displayed automatically after the html file is closed.
Page 27 of 36
Version 1.1 (Feb 2007)
tut122.doc
ods html
file="c:\temp\sas\graph\java1.htm
parameters=("DRILLDOWNMODE"="LOCAL");
goptions reset=all device=java
xpixels=500 ypixels=350
cback=white border;
title1 "Advertising Revenue Growth";
pattern1 c=CX7074C7;
pattern2 c=CX90D9D7;
pattern3 c=CXDE8DCE;
proc gchart data=advertising;
hbar3d year/ discrete group=media
sumvar=revenue
subgroup=media
gspace=.5 coutline=black
cframe=CXE2F7FE;
run;
ods html close;
ods listing;
Note the use of the DRILLDOWNMODE parameter. This allows drill-down to be used when the chart is
displayed.
Step 5:
(i)
A pop-up menu will appear allowing colours, bar-shapes, orientation and other attributes of the graph to be
changed. Use this menu in steps (iv) to (viii) below.
(ii)
(iii)
(iv)
(v)
Select GraphColorsScheme and change the colour scheme for the bars.
Select WallsBack and change the colour of the background to light blue.
Select WallsFloor and change the colour of the floor to light blue.
(xiii)
The specification of the drilldownmode parameter also enables you to select a part of the chart and look at
the data for that portion in finer detail.
Step 6:
Click the left mouse button on the first bar in any of the three groups.
Page 28 of 36
Version 1.1 (Feb 2007)
tut122.doc
Step 7:
Using the right-mouse button, select OptionsDrilldownClear All to exit drill-down mode
and return to the original display.
The Data step generates a variable z computed as a function of variables x and y on a lattice defined by
varying both x and y from -5 to +5 in steps of 0.25.
Step 2:
Step 3:
This program plots the data as a surface using PROC G3D (for details on the use of PROC G3D for surface
plotting, see Chapter 5).
Step 4:
The initial display is shown alongside the code used to generate the graph. Note the effect of the options
contour and style. The contour option allows contours for the surface to be projected on either of the planes
above and below the surface (0=no contours, 1=below, 2=above). The style option determines the
appearance of the surface (1=solid color (default), 2=wireframe, 3= solid band levels, 4 = gradient band
levels). Note also that the order option has been used on the axis statements to restrict the view of the surface
to a single quadrant.
Page 29 of 36
Version 1.1 (Feb 2007)
tut122.doc
Click the right mouse button. A pop-up menu will be displayed allowing attributes of the graph to
be changed.
Since the Activex device driver was specified, keyboard modifiers Ctrl, Shift and Alt may be used, in
conjunction with the left-mouse button (LMB), to perform shift, rotate and zoom operations. These controls are
described in the following table:
Activex Keyboard Modifiers
Keyboard Action
Ctrl + LMB
Alt + LMB
Shift + LMB
Step 6:
Effect
Rotate
Shift
Zoom
To rotate the graph, keep the Ctrl key and left mouse button depressed and move the mouse.
To zoom-in, keep the Alt key and left mouse button depressed and move the mouse.
The first of the graphs below displays the top-level pop-up menu. The graph on the right was obtained by
rotating and zooming-in on the original graph.
Page 30 of 36
Version 1.1 (Feb 2007)
tut122.doc
8.
The use of procedure options and global options defined using statements such as GOPTIONS, TITLE,
FOOTNOTE, NOTE, AXIS and LEGEND is usually sufficient to provide the degree of customisation required
for a graph. Occasionally, however, the need to add further detailed customisation to a graph may arise.
SAS/GRAPH allows the user to specify detailed customisation to be applied to a graph using individual
drawing instructions. This is implemented using a device called an Annotate Data Set.
An annotate data set is a SAS data set containing special variables with reserved names, the values of which
provide the information used to achieve customisation. Each case in the data set corresponds to a single
instruction. Thus, a complicated customisation may require an annotate data set containing many instructions
or cases.
8.1
The table below lists the special variables used in annotate data sets and their purpose.
Variable
FUNCTION
Type
C
TEXT
X
Y
POSITION
C
N
N
C
STYLE
SIZE
XSYS
YSYS
C
N
C
C
Values
Drawing command. Commands are: DRAW, MOVE,
POLY, POLYCONT, BAR.
A text string
an x-coordinate
a y-coordinate
the position at which annotation commences. SAS uses
single character values 1, 2 etc. to indicate position with
reference to a location indicted by X and Y. For example,
3 indicates up and to the right of the x-y location.
line-styles for lines
line widths or character sizes
Reference system used for x axis
Reference system used for y axis
Any single annotate data set may use only a selection of these commands. The commands can be executed
in two ways:
(i)
(ii)
Using the ANNOTATE=<annotate dataset> option on the call of the SAS/GRAPH procedure used to
draw the main graph.
Open the file anno1.sas. The file contains the program shown below.
data shapes;
length function color style $ 8 ;
retain xsys '2' ysys '2' color 'blue' size 2 style 'empty';
x=10; y=10; function='move'; output;
x=10; y=20; function='draw'; output;
x=20; y=20; function='draw'; output;
x=20; y=10; function='draw'; output;
x=10; y=10; function='draw'; output;
color='red'; style='solid';
x=14; y=10; function='move'; output;
x=16; y=20; function='bar' ; output;
Page 31 of 36
Version 1.1 (Feb 2007)
tut122.doc
Step 3:
(i)
Issue a LIB command from the command box (top left) and select the WORK library.
(ii)
Select shapes. The content of the data set shapes should correspond with the contents of the
table below.
(iii)
Step 4:
Obs
function
color
style
xsys
ysys
1
2
3
4
5
6
7
8
9
move
draw
draw
draw
draw
move
bar
move
bar
blue
blue
blue
blue
blue
red
red
red
red
empty
empty
empty
empty
empty
solid
solid
solid
solid
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
siz
e
2
2
2
2
2
2
2
2
2
10
10
20
20
10
14
16
10
20
10
20
20
10
10
10
20
14
16
(i)
Open the file ganno1.sas. This file contains the code below to make use of the instructions
stored in the data set shapes.
(ii)
Use F8 to execute the program. The result is a red cross surrounded by a blue rectangle, as
expected.
goptions reset=all;
proc ganno annotate=shapes ;
run;
Page 32 of 36
Version 1.1 (Feb 2007)
tut122.doc
Step 1:
data labels;
length color $ 8;
set curves end=eof;
if eof
then do;
xsys='2'; ysys='2'; position='1';
y=y1; text='Control '; color='red';
y=y2; text='Low
'; color='gold';
y=y3; text='Medium '; color='green';
y=y4; text='High '; color='blue';
end;
drop eof y1 y2 y3 y4;
run;
Step 2:
output;
output;
output;
output;
The DATA step reads the data set curves created earlier (using a SET statement) and senses when the last
case has been read. At that point it outputs one record for each of the four curves to a data set called labels.
Each record contains the X and Y co-ordinates of the final point on the curve together with other variables
which collectively define the drawing instruction.
Step 3:
Issue the command VIEWTABLE labels from the command box to inspect the data set
produced. The contents should correspond to the table below.
COLOR
red
gold
green
blue
XSYS
2
2
2
2
YSYS
2
2
2
2
POSITION
1
1
1
1
Step 4:
Step 5:
X
12
12
12
12
Y
15
12
8.5
5
TEXT
Control
Low
Medium
High
goptions reset=all;
axis1 minor=NONE order=( 0 to 12 by 1);
axis2 minor=NONE label=('Y')
order=(0 to 16 by 2);
proc gplot data=curves;
plot y1*x y2*x y3*x y4*x/overlay
annotate=labels
haxis=axis1 vaxis=axis2 noframe;
symbol1 V=star C=red I=spline L=1 ;
symbol2 V=diamond C=gold I=spline L=10;
symbol3 V=plus C=green I=spline L=2 ;
symbol4 V=triangle C=blue I=spline L=20;
run;
The effect is shown on the right, above. The annotation is achieved by including the option
ANNOTATE=labels on the PLOT sub-command of the GPLOT step.
Page 33 of 36
Version 1.1 (Feb 2007)
tut122.doc
9.
The scope of SAS for producing graphics displays extends much further than what is described in this
document. In addition to further graphical procedures within SAS/GRAPH, other SAS modules also offer
graphical facilities.
The following series of documents covers a number of special graphical applications and also describes the
use of SAS/INSIGHT and SAS/Enterprise GUIDE for graphical work.
Producing Composite Graphs Using SAS/GRAPH
- This document describes how to produce graphs containing multiple images on one page using SAS/GRAPH
procedure GREPLAY.
Fitting Curves and Surfaces Using SAS Software
- This document describes the use of SAS/GRAPH interpolation routines and SAS/STAT procedures REG,
NLIN and GAM for fitting smooth curves to data. Topics covered include: parametric modelling using REG and
NLIN, non-parametric modelling using Loess smoothing, semi-parametric modelling using thin-plate splines
and generalised additive models.
Data Exploration and Visualization using SAS/INSIGHT (In preparation)
- This document describes the use of SAS/INSIGHT for exploring data using 2-D and 3-D interactive displays.
An Introduction to SAS/Enterprise Guide (In preparation)
- This document describes the use of SAS/Enterprise Guide for statistical analysis and graphics.
Copies of these documents may be downloaded from the ISS web site
www.leeds.ac.uk/iss/documentation.
Page 34 of 36
Version 1.1 (Feb 2007)
tut122.doc
Description
A Simple Histogram
A Simple Bar Chart
Refinement of a Simple Bar Chart
A Grouped Bar Chart
A Line Plot of Experimental Data
A Logarithmic Plot with Multiple Axes
Multiple Plots Using Individual Pairs of Variables
Multiple Plots Using a Grouping Variable
Surface Plot
Three-dimensional Scatter Plot
Contour Plots Using Patterns
A Choropleth Map
Saving a Graph in CGM Format
Using the JAVA Device Driver
Using the ACTIVEX Device Driver
Simple Example of Annotation Using PROC GANNO
Multiple Plots Using Annotation
Page
5
6
10
14
16
17
18
19
20
21
23
24
26
27
29
31
32
Page 35 of 36
Version 1.1 (Feb 2007)
tut122.doc
Annex 2: Fonts
The following display was produced by SAS/GRAPH and illustrates a number of commonly used fonts
available in SAS/GRAPH. For a full list consult the SAS/GRAPH Reference Manuals.
Page 36 of 36
Version 1.1 (Feb 2007)
tut122.doc