9/10/2013
1
Copyright © 2010, SAS Institute Inc. All rights reserved.
Introduction to ODS Graphics for the Non-Statistician
Mike Kalt and Cynthia ZenderSAS Institute
2
Chapter 1: ODS Graphics for the Non-Statistician
1.1 What is ODS Graphics?
1.2 Producing Non-Statistical Graphs Using Base SAS
Procedures
1.3 SAS/GRAPH “SG” Procedures
1.4 Using PROC SGPLOT to Produce Non-Statistical Charts
and Plots
1.5 Controlling Graph Appearance with SGPLOT
1.6 Using PROC SGPANEL to Display Multiple Cells Per
Page
1.7 Using PROC SGSCATTER to Produce Multiple Plots Per
Page
1.8 The ODS Graphics Designer
1.9 Editing Graphs Produced by SG Procedures
1.10 Upcoming Features and Conclusions
3
Assumptions You need to create non-statistical graphics
– Bar charts
– Scatter plots
– Line plots
You may or may not have used SAS/GRAPH in the past
to create these graphs
You are afraid of statistics
You are not here to learn detailed syntax. You are here
to learn what you can do with the SG procedures
9/10/2013
2
4
Chapter 1: ODS Graphics for the Non-Statistician
1.1 What is ODS Graphics?1.2 Producing Non-Statistical Graphs Using Base SAS
Procedures
1.3 SAS/GRAPH “SG” Procedures
1.4 Using PROC SGPLOT to Produce Non-Statistical Charts
and Plots
1.5 Controlling Graph Appearance with SGPLOT
1.6 Using PROC SGPANEL to Display Multiple Cells Per
Page
1.7 Using PROC SGSCATTER to Produce Multiple Plots Per
Page
1.8 The ODS Graphics Designer
1.9 Editing Graphs Produced by SG Procedures
1.10 Upcoming Features and Conclusions
5
What is ODS Graphics?
6
In the Beginning. . .
All SAS graphics were done with "classic" SAS/GRAPH
procedures such as PROC GCHART and PROC GPLOT
proc gchart data=orders;vbar region;
title 'Back Orders by Region';run;
proc plot data=profit;plot amount*date=type;
title 'Salaries and Profit by Month';run;
9/10/2013
3
7
Classic SAS/GRAPH Procedures
The Good
– Very flexible and customizable
The Bad
– Not so easy to use
– Statistical graphs required running a STAT
procedure to create an output data set and using a
SAS/GRAPH procedure to graph the output data
8
Example: Creating LOESS Plots
proc loess data=Melanoma;model Incidences=Year;
ods output OutputStatistics=Results;
run;
symbol1 color=black value=dot;
symbol2 color=black interpol=join value=none;
proc gplot data=Results;title1 'Melanoma Data with Default LOESS Fit';
plot DepVar*Year Pred*Year/
hminor=3 vminor=3 overlay;
run;
Step 1:
Create output
data set from
PROC LOESS.
Step 2:
Plot data from
output data set
with PROC
GPLOT.
9
ODS Graphics: Why?Designed to make it easier for statistical users to develop
commonly-used statistical graphics. For example:
proc sgplot data=Melanoma;
title1 'Melanoma Data with Default LOESS Fit';
loess x=year y=incidences;
run;
Only one step
is required.
9/10/2013
4
10
ODS Graphics: What is It?
1. Graphics capabilities added to statistical procedures
2. Graphics capabilities added to some Base SAS
procedures
3. New features added to SAS/GRAPH:
– "SG" procedures
– Graphics Template Language
– ODS Graphics Designer
This collection of features is ODS Graphics.
11
Some Terminology
The following terms are interchangable:
Classic SAS/GRAPH
Device-based Graphics
SAS/GRAPH "G" Procedures
The following terms are interchangable:
ODS Graphics
Statistical Graphics
SAS/GRAPH "SG" Procedures are a component of
ODS/GRAPHICS
12
SAS/GRAPH "Classic" vs. ODS Graphics: Major Differences
Internal architecture and how graphs are produced
Support for replaying graphs
Specification of graphics options
Support for annotation
9/10/2013
5
13
How Graphs Are Produced by "Classic" Procedures
GRAPH1
Window
(ODS Listing)
Hardcopy
Device or File
Other ODS
Destinations
Device
Driver
Replay
Stored
Graphs
(GREPLAY)
GRSEG
Catalog Entry
SAS/GRAPH
Procedure
14
How Graphs Are Produced by ODS Graphics
SAS/GRAPH,
SAS/STAT or
Base
Procedure
Image File
ODS
Destinations
Device
Driver
No catalog entry is created
No output to GRAPH1 window (ODS Listing)
15
Architectural Differences
SAS/GRAPH "Classic Procedures" and ODS Graphics
use different underlying subsystems to create their
graphs. Because of this:
You can’t combine graphs produced by the two
systems
PROC GREPLAY cannot be used to replay multiple
or individual ODS graphs
15
9/10/2013
6
16
Specifying Graphics Options
For "classic" SAS/GRAPH, many options can be
specified in a GOPTIONS statement.
For ODS Graphics, there is no GOPTIONS statement.
A limited number of options can be specified on the
ODS GRAPHICS statement. Many others are
specified in the procedure
16
goptions hsize=3 inches htext=12pt;
ods graphics/height=200px imagefmt=jpeg;
17
Annotating Graphs
For "classic" SAS/GRAPH, the Annotate facility can be
used to add data-based annotation to graphics created
by SAS/GRAPH procedures (such as labeling points on
a graph).
ODS Graphics currently do not support the Annotate
facility. Annotate support is expected to be available
in SAS 9.3.
17
18
Not Just for Statisticians!
Although the original purpose of ODS Graphics was
to make producing statistical graphics easier, it’s also
a convenient way for producing general purpose plots
and charts.
9/10/2013
7
19
ODS Graphics: How to Use It
For statistical and Base SAS procedures
Use ODS GRAPHICS statement to activate graphics
Add options to procedure code to generate graphs
Use ODS GRAPHICS OFF statement to deactivate
graphics
20
ODS Graphics Example: Base/Stat Procedures
ods graphics on;proc freq data=orion.back_orders;
tables region/plots=freqplot;run;
ods graphics off;
21
ODS Graphics: How to Use It
For SAS/GRAPH "SG" Procedures
Use SG procedure statements
No ODS GRAPHICS statement required
9/10/2013
8
22
ODS Graphics Example: "SG" Procedure
proc sgplot data=orion.back_orders;vbar region;
title 'Back Orders by Region';run;
23
ODS Graphics: Output
Output from ODS Graphics can to go to either:
An image file (default)
– ODS LISTING destination used
– Image file accessed from Results window
– Output displayed in host image viewer
An ODS destination
– HTML, PDF, RTF
– Graph is output as either a separate image (HTML)
or an embedded image (RTF, PDF) in the ODS
result file
– Specify destination on ODS statement
– Output document can be viewed in Results Viewer
24
Output to an Image File
Step 1: Submit Code
9/10/2013
9
25
Output to an Image File
Step 2: Expand entry in Results Panel—results include
text tables and graphs
Graphs
(as image
files)
Text
table
26
Output to an Image File
Step 3: Double click on graph entry in results. Graph
opens in separate viewing application.
Graphs are
created and
stored as
.png files by
default
27
Output to a HTML File
Step 1: Submit Code
9/10/2013
10
28
Output to a HTML File
Step 2: HTML page is created and automatically opened
in SAS Results Viewer window
HTML page
contains
tables and
graphs
29
SAS/GRAPH "Classic" vs. ODS Graphics
Output from "Classic" SAS/GRAPH procedures cannot be
combined in SAS with output from ODS Graphics.
29
SAS/GRAPH Classic Procedures ODS Graphics
Output goes to GRAPH1 window or
other destinations (including ODS)
Output to image file or ODS
document only
Graph created as a GRSEG entry in
a SAS catalog
No catalog entries created
GREPLAY procedure replays
graphs stored in catalogs
No GREPLAY procedure
Annotate facility available to add
elements to existing graphs
No Annotate facility (coming in SAS
9.3)
GOPTIONS statement sets general
graphics options
GOPTIONS statement not used.
30
Review: Creating ODS Result Files
The following slides are a review of ODS result file
creation.
30
9/10/2013
11
31
ODS LISTING CLOSE;
ODS destination FILE = "filename.ext" STYLE=egdefault;
…procedure code…
ODS destination CLOSE;
Basic Output File Creation with ODS
When you create output with ODS, all tabular and
graphical output is created appropriately for your method
and destination.
31
32
ODS LISTING CLOSE;
ODS destination FILE = "filename.ext" STYLE=egdefault;
ODS GRAPHICS ON;
…procedure code that uses automatic template-based graphics…
ODS GRAPHICS OFF;
ODS destination CLOSE;
Using the ODS GRAPHICS Statement
If you are using the automatic ODS GRAPHICS capability
with procedures that support template-based graphics,
you also need to use ODS GRAPHICS ON/OFF
statements in your code.
32
33
ODS LISTING CLOSE;
ODS destination FILE = "filename.ext" STYLE=egdefault;
ODS GRAPHICS / height=200px width=500px
imagefmt=jpeg;
…”SG” procedure code…
ODS destination CLOSE;
SG Procedures Syntax Model
If you are using the “SG” procedures, you only need to
use the ODS GRAPHICS statement if you want to change
the default options.
33
9/10/2013
12
34
HTML Syntax Model for Graphics Creation
HTML-based destination have PATH= and GPATH=
options, which allow you to choose the physical location
of the result file (PATH=) and the physical location of the
external image file (GPATH=). The URL=NONE suboption
instructs ODS to build relative URLs for any tags, such as
the HTML <IMG> tag to point to a graphical image
location.
34
ODS HTML PATH='<path for output>' (URL=NONE)
GPATH='<path for images>' (URL=NONE)
FILE = "filename.html" STYLE=journal;
…SAS code to generate a report…
ODS HTML CLOSE;
35
RTF and PDF Syntax Model
Images for the RTF and PDF destinations are embedded
in the ODS output file. There is no equivalent to an <IMG>
tag for the RTF and PDF destinations.
35
ODS RTF FILE = "filename.rtf" STYLE=analysis;
…SAS code to generate a report…
ODS RTF CLOSE;
ODS PDF FILE = "filename.pdf" STYLE=statistical;
…SAS code to generate a report…
ODS PDF CLOSE;
36
Some Destinations Supported by ODS Graphics
Destination Viewer Graphics File Types
HTML Web browser PNG (default), GIF, JPEG, …
RTF Word processor, such
as Microsoft Word
Embedded in RTF file
PS PostScript viewer,
such as GSview
Embedded in PostScript file
PDF PDF viewer, such as
Adobe Reader
Embedded in PDF file
LATEX PostScript or PDF
viewer after compiling
LaTeX file
PostScript (default), EPSI,
GIF, JPEG, PDF, PNG
LISTING Default viewer in your
system for file type
PNG (default), GIF, BMP, DIB,
EMF, EPSI, JFIF, JPEG, …
36
9/10/2013
13
37
Some Recommended Styles
Style Description
DEFAULT Color style intended for general-purpose work.
This is the default for the HTML destination.
STATISTICAL Color style recommended for output in Web pages
or color print media. This is the style used in
SAS/STAT 9.2 documentation.
ANALYSIS Color style with a somewhat different appearance
from STATISTICAL.
JOURNAL and
JOURNAL2
Gray-scale and pure black-and-white styles,
respectively. Recommended for graphs in
black-and-white publications.
RTF Used to produce graphs to insert into a Microsoft
Word document or a Microsoft PowerPoint slide.
37
38
Output Delivery System
You can send your SAS
procedure output to a variety
of different “viewers” or
software for rendering.
38
Procedure
Output
Web Browser
Word Processor
Acrobat Reader
LaTeX Processor
SAS Results Window
Microsoft Office
39
Output Delivery System
Each ODS invocation
statement corresponds
to a specific type of viewer.
Some ODS output files can be rendered by multiple viewers.
39
Procedure
Output
ODS HTML
ODS RTF
ODS PDF
ODS LATEX
ODS LISTING
ODS MSOFFICE2K
9/10/2013
14
40
Sending a Report to a Web Browser
These HTML-based ODS destinations create files that
can be opened in a Web browser, such as Internet
Explorer, Firefox, or Safari.
40
Procedure
Output
HTML
HTMLCSS
MSOFFICE2K
PHTML
HTML3
CHTML
Internet
Explorer
Firefox
Safari
41
Sending a Report to a Print-Based Destination
These Printer Family destinations create files that
are printer-friendly or printer-ready.
41
Procedure
Output
PCL
PRINTER
Acrobat
Reader
GhostView
Printer
PS
42
Sending a Report to a Word Processor
These destinations create RTF or HTML files that can
be opened in a word processor.
42
Procedure
Output
Microsoft
Word
OpenOffice_RTFOpenOffice
Writer
RTF
HTML3
TAGSETS.RTF
MSOFFICE2K
HTML
9/10/2013
15
43
Sending a Report to a Spreadsheet
These destinations create HTML files that can be opened
by many spreadsheet programs, including Microsoft
Excel. HTML-based files can contain references for
graphic images.
43
Procedure
Output HTML3
MSOFFICE2K
HTML
At this time, XML files, such as the kind produced by the
TAGSETS.EXCELXP destination, only support tabular
output (by Microsoft design), not graphic output.
44
Accessing Individual Graphs
ODS has other destinations which support graphics. You
might need to submit LaTeX output to a journal. Or, you
might want to insert one of your graphs in PowerPoint.
44
Procedure
Output PostScript
LaTeX
HTML
LISTING
Cut and paste single images or insert PNG
file into Word or PowerPoint document.
Incorporate output
into LaTeX
document created
with other software.
Create LaTeX
document directly.
45
Demonstration
This demonstration illustrates concepts
discussed in this section.
9/10/2013
16
46
Chapter 1: ODS Graphics for the Non-Statistician
1.1 What is ODS Graphics?
1.2 Producing Non-Statistical Graphs Using Base
SAS Procedures1.3 SAS/GRAPH “SG” Procedures
1.4 Using PROC SGPLOT to Produce Non-Statistical Charts
and Plots
1.5 Controlling Graph Appearance with SGPLOT
1.6 Using PROC SGPANEL to Display Multiple Cells Per
Page
1.7 Using PROC SGSCATTER to Produce Multiple Plots Per
Page
1.8 The ODS Graphics Designer
1.9 Editing Graphs Produced by SG Procedures
1.10 Upcoming Features and Conclusions
47
Producing Non-Statistical Graphs Using Base SAS Procedures
48
Base SAS Procedures that Produce Graphics
ODS Graphics can be used with the following Base SAS
procedures:
CORR
FREQ
UNIVARIATE
These procedures produce both statistical and non-
statistical graphics. This presentation will focus on the
non-statistical charts and plots that PROC FREQ and
PROC UNIVARIATE can produce.
9/10/2013
17
49
Producing Graphics with Base Procedures
For statistical and Base SAS procedures
1. Use ODS GRAPHICS ON statement to activate
graphics
2. Add options or statements to procedure code to
generate specific graphs
3. Use ODS GRAPHICS OFF statement to deactivate
graphics
50
PROC FREQ Graphics with One-Way Tables
If you activate graphics, but do not specify a specific plot
with a one-way frequency table request, both frequency
and cumulative frequency plots are generated:
ods graphics on;proc freq data=orion.back_orders;
tables region;run;ods graphics off;
51
PROC FREQ Graphics with Two-Way Tables
If you activate graphics, but do not specify a specific plot
with a two-way frequency table request, a single graph is
generated containing a frequency plot for each variable.
No cumulative frequency plot is produced.
ods graphics on;proc freq
data=orion.employee_payroll;tables employee_gender*
marital_status;run;ods graphics off;
9/10/2013
18
52
Using the PLOTS= Option with PROC FREQ
You can use the PLOTS= option to request specific types
of plots, including various statistical plots. By using the
PLOTS= option, you can specify some appearance
options.
ods graphics on;proc freq data=orion.employee_payroll;
tables marital_status/plots=freqplot(orient=horizontal scale=percent);
run;ods graphics off;
53
Creating a Dot Plot with PROC FREQ
The example below requests a dot plot for the frequency
plot only.
ods graphics on;proc freq data=orion.orders;
tables region/plots(only)=freqplot (type=dotplot orient=vertical);
run;ods graphics off;
54
Graphics with PROC UNIVARIATE
To produce graphics with PROC UNIVARIATE, you
must explicitly supply a statement requesting a graph.
The HISTOGRAM statement produces histograms.
ods graphics on;proc univariate data=orion.employee_payroll;
var salary;histogram salary;run;
ods graphics off;
9/10/2013
19
55
Graphics with PROC UNIVARIATE
Various options can be used on the HISTOGRAM
statement to control the appearance of the plot.
ods graphics on;proc univariate data=orion.employee_payroll;
var salary;histogram salary/barlabel=count
normal(color=blue)grid;
run;ods graphics off;
56
Demonstration
This demonstration illustrates concepts
discussed in this section.
57
Chapter 1: ODS Graphics for the Non-Statistician
1.1 What is ODS Graphics?
1.2 Producing Non-Statistical Graphs Using Base SAS
Procedures
1.3 SAS/GRAPH “SG” Procedures1.4 Using PROC SGPLOT to Produce Non-Statistical Charts
and Plots
1.5 Controlling Graph Appearance with SGPLOT
1.6 Using PROC SGPANEL to Display Multiple Cells Per
Page
1.7 Using PROC SGSCATTER to Produce Multiple Plots Per
Page
1.8 The ODS Graphics Designer
1.9 Editing Graphs Produced by SG Procedures
1.10 Upcoming Features and Conclusions
9/10/2013
20
58
Overview of SAS/GRAPH "SG" Procedures
59
ODS GRAPHICS with SAS/GRAPH
Several new procedures and features that implement
ODS Graphics have been added to SAS/GRAPH.
They are:
PROC SGPLOT
PROC SGPANEL
PROC SGSCATTER
PROC SGRENDER
Graphics Template Language (GTL)
60
The "SG" Procedures
SGPLOT, SGPANEL, and SGSCATTER Procedures
– designed to create commonly used graphs quickly.
– produce many of the same types of graphs as the
original SAS/GRAPH procedures such as GPLOT
and GCHART, but use the ODS architecture
SGRENDER and the Graphics Template Language
– used to produce more complex, customized graphs
– steeper learning curve
9/10/2013
21
61
SGPLOT vs. Graph Template Language
proc template;define statgraph sgdesign;dynamic _ORDER_MONTH;begingraph;layout lattice _id='lattice' /
columndatarange=data rowdatarange=data;layout overlay _id='overlay' /;barchart _id='bar' x=_ORDER_MONTH / name='bar' stat=freq;endlayout;
endlayout;endgraph;end;run;proc sgrender data=ORION.ORDER_SUMMARY template=sgdesign;dynamic _ORDER_MONTH="'ORDER_MONTH'n";run;
proc sgplot data=orion.back_orders;vbar region;
title "Frequency Count by Region";run;
SGPLOT
GTL and PROC SGRENDER
62
Using the New SG Procedures
ODS GRAPHICS statement is not required, but can be
used to specify some options
No GOPTIONS statement
Symbols and patterns are specified in the procedure—
no SYMBOL or PATTERN statements
Statements available to control axis and legend
appearance, but differ from those in "classic"
SAS/GRAPH
Titles and footnotes work as in "classic" SAS/GRAPH
63
Demonstration
This demonstration illustrates concepts
discussed in this section.
9/10/2013
22
64
Chapter 1: ODS Graphics for the Non-Statistician
1.1 What is ODS Graphics?
1.2 Producing Non-Statistical Graphs Using Base SAS
Procedures
1.3 SAS/GRAPH “SG” Procedures
1.4 Using PROC SGPLOT to Produce Non-
Statistical Charts and Plots1.5 Controlling Graph Appearance with SGPLOT
1.6 Using PROC SGPANEL to Display Multiple Cells Per
Page
1.7 Using PROC SGSCATTER to Produce Multiple Plots Per
Page
1.8 The ODS Graphics Designer
1.9 Editing Graphs Produced by SG Procedures
1.10 Upcoming Features and Conclusions
65
Using PROC SGPLOT to Produce Non-Statistical Charts and Plots
66
The SGPLOT Procedure
PROC SGPLOT can be used to produce the following
basic plots and charts:
Simple bar charts
Stacked bar charts
Histograms
Scatter plots
Series plots
Overlaid graphs including bar/line charts
Although the procedure is named SGPLOT, it
produces both plots and charts.
9/10/2013
23
67
PROC SGPLOT: Statistical Graphs
In addition, PROC SGPLOT can be used to produce the
following types of plots and charts:
Density plots
Dot plots
Ellipse plots
Horizontal and vertical box plots
Loess plots
Spline curves
Regression plots
67
For this presentation, we will focus on the non-
statistical plots listed on the previous slide.
68
Terminology for ODS GraphicsTerm Meaning
plot any type of plot or chart, such as a scatter plot, bar chart, etc.
cell area containing one plot, or multiple overlaid plots
graph a collection of one or more cells
graph
cell
plot
multiple plots
overlaid
69
General SGPLOT Syntax
PROC SGPLOT statement – invokes the procedure
and specifies input data set
Plot statement – specifies type of graph, variables,
and options.
Axis statements – control appearance of axes
(optional)
KEYLEGEND statement – controls appearance of
legend (optional)
Multiple plot statements can be used to overlay
multiple plots on the same axes.
9/10/2013
24
70
Vertical Bar Chart
proc sgplot data=orion.back_orders;vbar region;
title "Frequency Count by Region";run;
71
Horizontal Bar Charts
proc sgplot data=orion.back_orders;hbar region / response=number_of_ordersstat=sum;
title "Backorders by Region";run;
72
Subgrouped Bar Chart
proc sgplot data=orion.employees;where company=:'Orion';vbar company / group=department;
title 'Employees By Country/Department';run;
9/10/2013
25
73
Histogram
proc sgplot data=orion.employees;where department='Sales';histogram salary;
title 'Distribution of Employee Salaries';run;
74
Overlaying Graphs
proc sgplot data=orion.employees;histogram salary;density salary / type=normal;where department='Sales';
title 'Distribution of Employee Salaries';run;
75
Scatter Plot
proc sgplot data=orion.employees;where company='Orion Italy' and department='Sales';
scatter y=salary x=employee_birthdate;title 'Salary by Birth Date';
run;
9/10/2013
26
76
Grouped Series Plot
proc sgplot data=orion.profit;where company in ('Orion France','Orion
Italy');series y=sales x=yymm / group=company;
title 'Sales by Country';run;
77
Overlayed Series Plot (Multiple Y Variables)
proc sgplot data=orion.budget;series y=yr2003 x=month;series y=yr2004 x=month;
title 'Budget by Month';run;
78
Overlaid Bar Chart and Line Plot
proc sgplot data=orion.profit;where company='Orion France' and
year(yymm)=2004;vbar yymm / response=sales;vline yymm / response=cost;
title 'Sales vs. Cost';run;
9/10/2013
27
79
Demonstration
This demonstration illustrates concepts
discussed in this section.
80
Chapter 1: ODS Graphics for the Non-Statistician
1.1 What is ODS Graphics?
1.2 Producing Non-Statistical Graphs Using Base SAS
Procedures
1.3 SAS/GRAPH “SG” Procedures
1.4 Using PROC SGPLOT to Produce Non-Statistical Charts
and Plots
1.5 Controlling Graph Appearance with SGPLOT1.6 Using PROC SGPANEL to Display Multiple Cells Per
Page
1.7 Using PROC SGSCATTER to Produce Multiple Plots Per
Page
1.8 The ODS Graphics Designer
1.9 Editing Graphs Produced by SG Procedures
1.10 Upcoming Features and Conclusions
81
Controlling Graph Appearance with SGPLOT
9/10/2013
28
82
Using ODS Styles
ods listing style=banker;proc sgplot data=orion.profit;
where company='Orion France' and year(yymm)=2004;
vbar yymm / response=sales;vline yymm / response=cost;
title 'Sales vs. Cost';run;ods listing style=listing;
83
Controlling Fills and Labeling Bars
proc sgplot data=orion.back_orders;vbar region/response=number_of_orders
fillattrs=(color="verylightred")datalabel;
title "Back Orders by Region";run;
84
Overlaid Bars with Transparency
proc sgplot data=orion.budget;vbar month/response=yr2003
fillattrs=(color=red)transparency=.7;
vbar month/response=yr2004 fillattrs=(color=blue)transparency=.7 barwidth=.5;
run;
9/10/2013
29
85
Controlling Lines and Markers
proc sgplot data=orion.budget;series y=yr2003 x=month/ markers
lineattrs=(color=red thickness=3) markerattrs=(color=black symbol=circlefilledsize=8);
title 'Budget by Month';run;
86
Controlling Axis Attributes
proc sgplot data=orion.profit;where (company='Orion Australia' or
company='Orion Italy') and year(yymm)=2003;
series y=sales x=yymm/ markers group=company;xaxis fitpolicy=rotate
tickvalueformat=monname. grid label='Month';yaxis label='Total Sales' grid;
title '2003 Sales';run;
87
Additional Axis Options
proc sgplot data=orion.profit;where company in ('Orion Australia' 'Orion
Italy') and year(yymm)=2003;series y=sales x=yymm/ markers group=company;xaxis display=(nolabel)
tickvalueformat=monname3.offsetmin=0 offsetmax=0 grid;
yaxis values=(40000 to 240000 by 40000) gridlabel='Total Sales';
title '2003 Sales';run;
9/10/2013
30
88
Adding Top and Right Axesproc sgplot data=orion.budget;series y=yr2003 x=month;series y=yr2004 x=month/x2axis y2axis;xaxis type=discrete;yaxis display=(nolabel) grid offsetmax=.1;x2axis type=discrete display=(nolabel);y2axis display=(nolabel) values=(1e6 to 3e6 by
500000)offsetmax=.1;title 'Budget by Month';run;
89
Controlling the Legend
proc sgplot data=orion.budget;series y=yr2003 x=month;series y=yr2004 x=month;xaxis type=discrete;keylegend / location=inside down=2
position=topleft;title 'Budget by Month';run;
90
Adding a Text Box With INSET Statement
proc sgplot data=orion.budget;series y=yr2003 x=month / name='2003';series y=yr2004 x=month / name='2004';xaxis type=discrete;inset 'This plot illustrates the monthly'
'budget values for 2003 and 2004'/position=bottom border title='Explanation: ';
title 'Budget by Month';run;
9/10/2013
31
91
The ODS GRAPHICS Statement
You can specify options on the ODS GRAPHICS statement
to control some aspects of your graphics output. Note that
you do not need to use the ODS GRAPHICS statement
with the SG procedures unless you want to specify options.
Some of the options that you can specify are:
Height and width of the graph
Format (file type) and name of the graphics file
Whether a border is drawn around the graph
92
Using the ODS GRAPHICS Statement
ods graphics / height=200px width=500px imagefmt=jpeg;proc sgplot data=orion.back_orders;
vbar region/response=number_of_ordersfillattrs=(color="verylightred")datalabel;
title "Back Orders by Region";run;
93
The ODS GRAPHICS Statement
Options specified on the ODS GRAPHICS statement remain
in effect until they are reset or the SAS session ends. To
reset the options, submit the following statement:
ods graphics/reset;
9/10/2013
32
94
Demonstration
This demonstration illustrates concepts
discussed in this section.
95
Reference: ODS GRAPHICS Options
The following section provides reference information
about using the ODS GRAPHICS statement to change
options.
96
Using the ODS GRAPHICS Statement
The ODS GRAPHICS statement enables you to alter
settings for your graphics output. The kinds of changes
you might wish to make are:
alter image size, and/or DPI
specify image format
create tool tips for ODS HTML output
specify image name
reset ODS GRAPHICS statement options.
96
9/10/2013
33
97
ODS GRAPHICS Statement
You have already seen this usage of the ODS
GRAPHICS statement, which enables and disables
the graphics environment:
However, the ODS GRAPHICS statement also provides
options for changing your output. Remember that the
traditional SAS/GRAPH GOPTIONS statement will not
work with the ODS Graphics system.
97
ods graphics on;
. . .SAS code . . .
ods graphics off;
98
Common ODS Graphics Options
Common ODS Graphics options are
98
Use This Option To…
RESET Reset one or more ODS Graphics options
to its default.
IMAGENAME Specify the base image filename.
IMAGEFMT Specify the image format used to generate
image files.
WIDTH/HEIGHT Specify the width or height of any graph.
BORDER/NOBORDER Specify whether to draw a border around
each graph.
ANTIALIASMAX=
with ANTIALIAS=ON
Specify the maximum number of markers
or lines to be anti-aliased before anti-
aliasing is disabled. The default value is
600.
IMAGEMAP/NOIMAGEMAP Specify whether data tips are generated.
99
Using the RESET Option
To reset all the options to their defaults, use:
To reset all the reset options to their defaults, use:
To reset specific options, use:
99
ods graphics / reset;
ods graphics / reset=all;
ods graphics / reset=index;ods graphics / reset=border;
ods graphics / reset=indexreset=widthreset=height;
9/10/2013
34
100
Using IMAGENAME and IMAGEFMT Options
By default, the image filename is the name of the graph
object. The default path is the current output directory.
A file extension for filename is automatically generated
based on the IMAGEFMT= option. If more than one
image is generated, each is assigned a filename as a
base name followed by a number in order to create
unique names. This numbering can be reset with the
RESET=INDEX option.
100
ods graphics / imagename='wombat'imagefmt=png;
101
More About the IMAGEFMT Option
The IMAGEFMT option specifies the image format to be
used. If the image format is not valid for the active output
destination, the format is automatically changed to the
default image format for that destination. The default
IMAGEFMT value is STATIC, which means that the best
quality static image format is used to create the image for
the active output destination.
101
ods graphics / imagename='wombat'imagefmt=png;
ods graphics / imagefmt=static;
102
Setting Image Width and Height
The WIDTH and HEIGHT options set the width and height
of the graph image. The default aspect ratio is typically
4/3 or 640px by 480px.
The default width dimension is the value of the SAS
registry entry for “Design Width”. Typically, the value
is 640px. If HEIGHT= is not also supplied, the graph
maintains its default aspect ratio (4 / 3).
The default height dimension is the value of the SAS
registry entry for “Design Height”. Typically, the value
is 480px. If WIDTH= is not also supplied, the graph
maintains its default aspect ratio (4 / 3).
102
ods graphics / width=1280pxheight=960px;
9/10/2013
35
103
Setting the ANTIALIASMAX= Option
Anti-aliasing smooths the appearance of diagonal lines
and some markers. Text displayed in the graph is always
anti-aliased. If ANTIALIAS=ON, the ANTIALIASMAX=
option specifies the maximum number of markers or lines
to be anti-aliased before anti-aliasing is disabled. For
example, if there are more than 400 scatterpoint markers
to be anti-aliased and ANTIALIASMAX=400, then no
markers will be anti-aliased.
103
ods graphics / antialias=onantialiasmax=1000;
104
Creating Tool Tips with the IMAGEMAP Option
The IMAGEMAP option is used with the HTML destination
and controls the generation of data tips. Data tips are
pieces of explanatory text that appear when you mouse-
over the data portions of a graph contained in an HTML
page. The TIPMAX option specifies the maximum number
of distinct tooltips permitted before tooltips are disabled.
104
ods graphics / imagemap tipmax=1500;
ods html path='path-for-output' (url=none)gpath='path-for-image'file='myfile.html' style=analysis;
. . . code that generates ODS GRAPHICS image . . .
ods html close;
105
Data Tip Example
105
Cholesterol = 375
Age CHD Diagnosed = 67Observation Number = 5186
9/10/2013
36
106
Reference: SGPLOT Procedure
The following section contains reference material on the
SGPLOT material. For a comprehensive syntax guide,
refer to the documentation topic, entitled: “The SGPLOT
Procedure” in the SAS/GRAPH(R) 9.2: Statistical
Graphics Procedures Guide, Second Edition.
107
PROC SGPLOT Categories
PROC SGPLOT statements can be organized into
categories. The SGPLOT procedure enables you to
combine or overlay different plots on top of each other.
107
Type Plots in This Category Can Combine with
Basic scatter, series, step, needle,
vector and band plots
Basic, Fit and Confidence
Fit and
Confidence
regression, loess, penalized
B-spline curves, and ellipses
Basic, Fit and Confidence
Distribution box plots, histograms, and
normal and kernel density
estimates
Distribution (box plots cannot be
combined with any other plot type)
Categorization bar charts, line charts and dot
plots
Categorization
108
PROC SGPLOT Statement Model Syntax
PROC SGPLOT statements conform to different syntax
models, depending on plot type.
Basic, Fit and Confidence Plots:
Plots for Categorical Data:
Band Plots:
108
PLOTNAME X=xvar Y=yvar / OPTIONS;
BAND X=xvar UPPER=uvar-or-uval LOWER=lvar-or-lval
/ OPTIONS;
PLOTNAME response-or-category-var / OPTIONS;
9/10/2013
37
109
Basic Plot Statements
Type Description
SCATTER Creates a scatter plot of observations for two variables.
SERIES Creates a line plot. Useful where x-axis values are
meaningfully related, such as time.
STEP Creates a step plot. Useful where y-axis values reflect
a step function, such as integers.
NEEDLE Creates a plot with needles connecting each point to
the baseline. Useful for showing deviations from a
constant.
VECTOR Creates a vector plot that draws arrows from a point of
origin to each data point. Useful for showing bivariate
deviations from an origin such as a centroid.
BAND Creates a band that highlights part of the plot. (Band
limits can be a variable or a numeric constant.) useful
for showing confidence or prediction limits.
109
110
Fit and Confidence Plot Statements
Type Description
REG Creates a fitted regression line or curve. Enables
visualization of a least squares fit.
ELLIPSE Adds a prediction (or confidence) ellipse to a plot.
Enables visualization of a region where
observations (or regression fit) are likely to fall.
Prediction is default.
LOESS Creates a fitted loess curve. Useful for visualizing
a nonlinear relationship between two variables.
Smoothness is optionally controlled by a
smoothing parameter and polynomial degree.
PBSPLINE Creates a fitted penalized B-spline curve. Useful
for visualizing a nonlinear relationship between two
variables. Smoothness is optionally controlled by
a smoothing parameter and the number of knots.
110
111
Plot Statements for Continuous Data
Type Description
HBOX Creates a horizontal box plot that shows the
distribution of your data. Especially useful for
comparing distributions of grouped data.
VBOX Creates a vertical box plot that shows the
distribution of your data. Especially useful for
comparing distributions of grouped data.
HISTOGRAM Creates a histogram that displays the frequency
distribution of a numeric variable. Especially useful
for evaluating the distribution of ungrouped data.
DENSITY Creates a density curve that shows the distribution
of values for a numeric variable. Overlay a density
curve on a histogram to compare the data to a
normal distribution or see a smoothed density
function for a variable.
111
9/10/2013
38
112
Plot Statements for Categorical DataType Description
HBAR Creates a horizontal bar chart that summarizes the
values of a category variable. Useful for comparing
groups when seeing the minimum on the plot is
important.
VBAR Creates a vertical bar chart that summarizes the values
of a category variable. Useful for comparing groups
when seeing the minimum on the plot is important.
HLINE Creates a horizontal line chart. Especially useful when
the y-axis values are ordered. (Use with the HBAR
statement to create a horizontal bar-line chart.)
VLINE Creates a vertical line chart. Especially useful when the
x-axis values are ordered. (Use with the VBAR
statement to create a vertical bar-line chart.)
DOT Creates a dot plot that summarizes the values of a
category variable. Useful for comparing groups where
seeing the minimum or origin on the plot is not useful.112
113
Multiple Plotting Statements in PROC SGPLOT
PROC SGPLOT draws the plots in your graph in the
same order that you specify the plot statements.
Order your plot statements so that your plots do not
obscure one another.
You can also avoid obscuring your data by using
the TRANSPARENCY= option or the
CLMTRANSPARENCY= option to make your plots
partially transparent.
113
114
PROC SGPLOT Axis Concepts
With the SGPLOT procedure, you have the ability
to control four separate axes for your graphs.
114
xaxis
x2axis
y2axisyaxis
secondary
primary
9/10/2013
39
115
PROC SGPLOT AXIS Options
AXIS statements support these selected options.
115
Option Usage
DISPLAY= Specify which axis features should be displayed.
LABEL= Specify an axis label.
GRID Specify placement of a grid line at each tick on the axis.
TYPE= Specify the type of axis: DISCRETE, LINEAR, LOG or TIME.
DISCRETEORDER= Specify the order in which discrete tick values are placed on
the axis: DATA, FORMATTED, or UNFORMATTED.
INTERVAL= Specify the tick interval for a TIME axis: AUTO, DAY, MONTH
or YEAR are selected examples.
FITPOLICY= Specify the method used to fit tick mark values on a horizontal
axis when there is not enough room to draw them normally:
ROTATE, STAGGER or THIN are selected examples.
VALUES= Specify values for the tick marks on the axis:values=(0 25 50 100)
values=(0 to 200 by 50)
values=('25MAY08'd '04JUL08'd '23AUG08'd)
116
Controlling the Axis Features
Use the DISPLAY= option to control which axis features
appear.
116
Option Usage
DISPLAY=ALL Displays all of the features (default).
DISPLAY=NONE Displays none of the features.
DISPLAY=(options) Controls which features are displayed (list one feature or
more than one):
NOLABEL hide the axis label.
NOLINE hide the axis line.
NOTICKS hide the tick marks on the axis.
NOVALUES hide the tick mark values on the axis.
XAXIS | YAXIS | X2AXIS | Y2AXIS /
DISPLAY=<value> <other options>;
117
Using X2AXIS and Y2AXIS
You cannot use the X2AXIS and Y2AXIS statements with
HBAR, VBAR or DOT plots.
117
Statement
S
C
A
T
T
E
R
S
E
R
I
E
S
S
T
E
P
N
E
E
D
L
E
B
A
N
D
R
E
G
L
O
E
S
S
P
B
S
P
LI
N
E
E
L
L
I
S
P
E
H
B
O
X
-
V
B
O
X
HI
S
T
O
G
R
A
M
D
E
N
S
I
T
Y
H
B
A
R
-
V
B
A
R
H
L
I
N
E
-
V
L
I
N
E
D
O
T
V
E
C
T
O
R
XAXIS/YAXIS x x x x x x x x x x x x x x x x
X2AXIS/Y2AXIS x x x x x x x x x x x x x x
You must link the X2AXIS and Y2AXIS statements to the selected
plot statement with the X2AXIS option or the Y2AXIS option.
9/10/2013
40
118
Understanding Axis Type
The axis type depends on the variable placed on an axis.
118
Type Value Used For
LINEAR specifies a linear scale for the axis. This is the default axis type for
numeric variables that do not have date or time formats.
DISCRETE specifies an axis with discrete values. If a character variable is
assigned to an axis, then the default type for that axis is discrete.
All categorization plots use a discrete axis for the category
variable.
TIME specifies a time scale for the axis. If the variable assigned to an
axis has a time, date, or datetime format associated with it, then
time is the default axis type.
INTERVAL= values for a TIME axis:
AUTO, SECOND, MINUTE, HOUR, DAY, WEEK, TENDAY,
SEMIMONTH, MONTH, QUARTER, SEMIYEAR, YEAR
LOG specifies a logarithmic scale for the axis. This axis type is never a
default. (A logarithmic scale cannot be used with linear regression
(REG statement) plots where DEGREE=1.)
119
Using TYPE=DISCRETE and DISCRETEORDER
By default, categorical variables use an axis type of
DISCRETE. You can control the order of discrete axis tick
values with the DISCRETEORDER option.
119
Option Usage with DISCRETE Axis
DISCRETEORDER=DATA Displays the values in the order that
they appear in the data.
DISCRETEORDER=UNFORMATTED Sorts the unformatted values in
ascending character order. (Default)
DISCRETEORDER=FORMATTED Sorts the formatted values in
ascending character order.
This option affects only box plots, dot plots, bar
charts, and line charts, or for any axis where
TYPE=DISCRETE.
120
Using a Logarithmic Axis
Several options are related to logarithmic axis
specification. To use these options the TYPE value
must be set to LOG:
120
Option Used with TYPE=LOG Option
LOGBASE specifies the base value for the logarithmic scale.LOGBASE=10
LOGBASE=2
LOGBASE=E
LOGSTYLE specifies how to scale and format the values for the major tick
marks for logarithmic axes. Specify one of the following
values:LOGSTYLE=LOGEXPAND
LOGSTYLE=LOGEXPONENT
LOGSTYLE=LINEAR
9/10/2013
41
121
Selected Options
SGPLOT Procedure
Statement Option
(Option works with the
SGPLOT procedure statement
if an ‘x’ is in the column)
S
C
A
T
T
E
R
S
E
R
I
E
S
S
T
E
P
N
E
E
D
L
E
V
E
C
T
O
R
B
A
N
D
R
E
G
L
O
E
S
S
P
B
S
P
L
I
N
E
E
L
L
I
P
S
E
H
B
O
X
V
B
O
X
H
I
S
T
O
G
R
A
M
D
E
N
S
I
T
Y
H
V
A
R
V
B
A
R
H
L
I
N
E
V
L
I
N
E
D
O
T
/GROUP=var; x x x x x x x x x x x x
/LEGENDLABEL='label-string'; x x x x x x x x x x x x x x x x
/MARKERS; x x x x
/NOMARKERS; x x x
/TRANSPARENCY=value; x x x x x x x x x x x x x
/FILLATTRS=(attribute=value); x x x x
121 continued...
122
Selected Options
SGPLOT Procedure
Statement Option
(Option works with the
SGPLOT procedure statement
if an ‘x’ is in the column)
S
C
A
T
T
E
R
S
E
R
I
E
S
S
T
E
P
N
E
E
D
L
E
V
E
C
T
O
R
B
A
N
D
R
E
G
L
O
E
S
S
P
B
S
P
L
I
N
E
E
L
L
I
P
S
E
H
B
O
X
V
B
O
X
H
I
S
T
O
G
R
A
M
D
E
N
S
I
T
Y
H
V
A
R
V
B
A
R
H
L
I
N
E
V
L
I
N
E
D
O
T
/MARKERATTRS=(attribute=value); x x x x x x x x x
/LINEATTRS=(attribute=value); x x x x x x x x x x x
/DATALABEL<=variable>; x x x x x x x x x x x x
/CATEGORY= x
/STAT= x x x
/RESPONSE= x x x
122
123
Additional SGPLOT Procedure Statements
These additional SGPLOT procedure statements can be
combined with all other SGPLOT procedure statements.
123
Type Description
INSET Adds a text box inside the plot axes.
REFLINE Creates a horizontal or vertical reference line.
XAXIS
X2AXIS
Specify options for the x-axis.
YAXIS
Y2AXIS
Specify options for the y-axis.
KEYLEGEND Adds a legend to the plot. (Use with NAME= in the
plot statement to identify which plots to include in
the legend.)
9/10/2013
42
124
Types of Customization
You can perform customization using additional
statements within your statistical procedure programs.
124
To … Use Statement Use With
Add a text box inside
of the axes of the plot
INSET PROC SGPLOT
Create a horizontal or
vertical reference line
REFLINE PROC SGPLOT
Position and
customize legends
KEYLEGEND PROC SGPLOT
125
INSET Statement Model Syntax
INSET statement text can take the form of single or
multiple text strings or can be specified as a label list.
Some examples of the INSET statement are:
125
INSET "text-string1" "text-string2" / <options>;
INSET "label1"="value1" "label2"="value2" / <options>;
Type of String Results in…
Single or multiple
text strings
Each string is placed on a separate line in the text
box.
Label/value list Each label/value pair is placed on a separate line
in the text box.
126
INSET Statement Options
The INSET statement supports the following options.
The options follow a slash ('/') in the INSET statement.
126
OPTION Used to…
BORDER |
NOBORDER
specify whether to display a border around the INSET text
box.
TEXTATTRS= specify the appearance of text in the INSET box.
POSITION= specify the position of the INSET box in the graph area.
TITLE= specify a center-aligned title for the INSET box.
TITLEATTRS= specify the appearance of the title for the INSET box.
LABELALIGN= specify how the labels are aligned when you specify label-
value pairs.
VALUEALIGN= specify how the values are aligned when you specify label-
value pairs.
9/10/2013
43
127
REFLINE Statement Model Syntax
The REFLINE statement can enhance your graph with
a horizontal or vertical reference line. You can place a
reference line for every value of a specific variable or
at points along the axes. Model syntax for the REFLINE
statement is:
127
REFLINE variable / <options>;
REFLINE value1 value2 / <options>;
REFLINE
Specification Action…
Variable draws a reference line for each value of the
specified variable.
Value list draws one or more reference lines at the
values that you specify.
128
REFLINE Statement Options
The REFLINE statement supports the following options.
The options follow a slash ('/') in the REFLINE statement.
128
Option Used to…
AXIS= specify the axis that contains the reference line values.
For example, if you specify AXIS= X, vertical reference
lines are drawn at points on the X axis.
LABEL= create labels for each reference line. If you do not
specify a label for a line, the reference value for that
line is used as the label.
LABELLOC= specify whether the labels are placed inside or outside
of the plot area.
LABELPOS= specify the position of the labels.
LINEATTRS= specify the appearance of the reference line.
NOCLIP extends the plot axes to contain the reference lines.
129
Other REFLINE Statement Options
Other REFLINE statement options are
129
OPTION Used to…
LEGENDLABEL= specify a label that identifies the markers from
the plot in the legend. By default, the label
“Reference Line” is used.
NAME= specify a name for the plot.
TRANSPARENCY specify specifies the degree of transparency for
the reference lines. Specify a value from 0.0
(completely opaque) to 1.0 (completely
transparent).
9/10/2013
44
130
Specifying Line Attributes
To specify colors, patterns or line thickness, use the
following suboptions for the LINEATTRS option:
130
SUB-OPTION Example
COLOR= specify a color. (The default color is specified by the
ContrastColor attribute of the GraphReference style
element in the current style.)lineattrs=(color=blue);
lineattrs=(color=cx0000ff);
PATTERN= specify a line pattern. (The default line pattern is specified
by the LineStyle attribute of the GraphReference style
element in the current style.)lineattrs=(pattern=2);
THICKNESS= specify the thickness of the line. The default unit is pixels.
Supported units of measure are CM, IN, MM, PCT, PT, or
PX. (The default line thickness is specified by the
LineThickness attribute of the GraphReference style
element in the current style.)lineattrs=(thickness=3);
131
Line Patterns
Examples of specifying the line patterns are shown below:
131
refline &minsurg &avgsurg &maxsurg/lineattrs=(color=blue pattern=8);
refline &minsurg &avgsurg &maxsurg/lineattrs=(color=blue pattern=MediumDashShortDash);
Use name or number
as PATTERN= value.
132
Using DATALABEL with a Variable
Most PROC SGPLOT (and PROC SGSCATTER)
statements support the use of an identifying variable with
the DATALABEL option. If you specify a variable, then the
values of that variable are used for the data labels. If you
do not specify a variable, then the values of the calculated
response are used for the data labels.
132
Option Form Usage in SGPLOT Statements
DATALABEL or
DATALABEL=variable
DOT, HBOX, VBOX, HLINE, VLINE, LOESS,
NEEDLE, PBSPLINE, SCATTER, SERIES,
STEP, VECTOR
Also supported with SGSCATTER procedure
COMPARE, MATRIX, and PLOT statements.
DATALABEL only HBAR, VBAR
9/10/2013
45
133
KEYLEGEND Statement Model Syntax
The KEYLEGEND statement can enhance your graph
with legend information. You can place a legend for every
PLOT statement or collection of PLOT statements. Model
syntax for the KEYLEGEND statement is:
133
KEYLEGEND / <options>;
KEYLEGEND "name1" "name2" / <options>;
Selected Options Action…
TITLE Specify a legend title.
ACROSS | DOWN Specify legend lines across and/or down.
POSITION Specify legend position:
BOTTOM, BOTTOMLEFT, BOTTOMRIGHT, LEFT,
RIGHT, TOP, TOPLEFT, or TOPRIGHT.
LOCATION Specify legend location: INSIDE or OUTSIDE.
BORDER Specify whether legend should have a border.
134
Customizing Graphs with Statement Options
Many of the SGPLOT procedure options shown in this
section are supported by multiple statements.
134
Option
S
C
A
T
T
E
R
S
E
R
I
E
S
S
T
E
P
N
E
E
D
L
E
B
A
N
D
R
E
G
L
O
E
S
S
P
B
S
P
L
I
N
E
E
L
L
I
P
S
E
H
B
O
X
-
V
B
O
X
H
I
S
T
O
G
R
A
M
D
E
N
S
I
T
Y
H
B
A
R
-
V
B
A
R
H
L
I
N
E
-
V
L
I
N
E
D
O
T
V
E
C
T
O
R
DATALABEL x x x x x x x x x x x x
LEGENDLABEL x x x x x x x x x x x x x x x x
LINEATTRS x x x x x x x x x x x
135
Customizing Marker and Fill Attributes
Marker and fill attributes can be changed with the
following options in more than one SGPLOT procedure
statement:
135
Option
S
C
A
T
T
E
R
S
E
R
I
E
S
S
T
E
P
N
E
E
D
L
E
B
A
N
D
R
E
G
L
O
E
S
S
P
B
S
P
L
I
N
E
E
L
L
I
P
S
E
H
B
O
X
-
V
B
O
X
H
I
S
T
O
G
R
A
M
D
E
N
S
I
T
Y
H
B
A
R
-
V
B
A
R
H
L
I
N
E
-
V
L
I
N
E
D
O
T
V
E
C
T
O
R
MARKERATTRS x x x x x x x x x
MARKERS x x x x
NOMARKERS x x x
FILLATTRS x x x x
TRANSPARENCY x x x x x x x x x x x x x
9/10/2013
46
136
Specifying Marker Attributes
To specify colors, symbol or symbol size, use the
following suboptions for the MARKERATTRS option:
136
SUB-OPTION Example
COLOR= specify a color. (The default color for ungrouped data is
specified by the ContrastColor attribute of the
GraphDataDefault style element in the current style.)markerattrs=(color=blue);
markerattrs=(color=cx0000ff);
SYMBOL= specify a symbol. (The default symbol for ungrouped data is
specified by the MarkerSymbol attribute of the
GraphDataDefault style element in the current style.)markerattrs=(symbol=starfilled);
SIZE= specify the size of the marker symbol. The default unit is
pixels. Supported units of measure are CM, IN, MM, PCT, PT,
or PX. (The default size for ungrouped data is specified by the
MarkerSize attribute of the GraphDataDefault style element in
the current style.)markerattrs=(size=5px);
137
Changing Marker Symbols
The available list of marker symbols can be found in the
documentation:
137
138
Chapter 1: ODS Graphics for the Non-Statistician
1.1 What is ODS Graphics?
1.2 Producing Non-Statistical Graphs Using Base SAS
Procedures
1.3 SAS/GRAPH “SG” Procedures
1.4 Using PROC SGPLOT to Produce Non-Statistical Charts
and Plots
1.5 Controlling Graph Appearance with SGPLOT
1.6 Using PROC SGPANEL to Display Multiple
Cells Per Page1.7 Using PROC SGSCATTER to Produce Multiple Plots Per
Page
1.8 The ODS Graphics Designer
1.9 Editing Graphs Produced by SG Procedures
1.10 Upcoming Features and Conclusions
9/10/2013
47
139
Using PROC SGPANEL to Display Multiple Cells Per Page
140
PROC SGPANEL
PROC SGPANEL produces graphs similar to PROC
SGPLOT, but creates separate cells for each value of
a categorical variable or crossing of multiple categorical
variables. You can control the placement of the cells on
a graph.
141
General SGPANEL Syntax
PROC SGPANEL statement – invokes the procedure
and specifies input data set
Plot statement – specifies type of graph, variables,
and options.
PANELBY statement – specifies the classification
variables and the arrangement of cells on the page.
Axis statements – control appearance of axes
(optional)
KEYLEGEND statement – controls appearance of
legend (optional)
9/10/2013
48
142
PROC SGPANEL: Plot Statements
The plot statements supported by PROC SGPANEL
are the same as those supported by PROC SGPLOT.
They include HBAR, VBAR, HISTOGRAM, SCATTER,
SERIES, and others.
As with PROC SGPLOT, multiple plot statements
produce overlay graphs.
The same options used to control plot appearance
with PROC SGPLOT (such as LINEATTRS,
FILLATTRS, etc.) can be used with PROC SGPANEL.
143
The PANELBY Statement
The PANELBY statement lists one or more categorical
variables.
proc sgpanel data=orion.profit;where company in ('Orion Belgium'
'Orion Germany' 'Orion France' 'Orion Italy');
panelby company;series y=sales x=yymm;title 'Monthly Sales';run;
144
PANELBY Statement Options
Options on the PANELBY statement specify the
arrangement of cells
proc sgpanel data=orion.profit;where company in ('Orion Belgium‘
'Orion Germany''Orion France' 'Orion Italy');
panelby company/layout=columnlattice onepanel;series y=sales x=yymm;title 'Monthly Sales';run;
9/10/2013
49
145
Additional PANELBY Statement Options
proc sgpanel data=orion.profit;where company in ('Orion Belgium'
'Orion Germany''Orion France' 'Orion Italy');
panelby company/layout=columnlattice colheaderpos=bottomonepanel noborder novarname;
series y=sales x=yymm;title 'Monthly Sales';run;
146
Multiple PANELBY Variables
proc sgpanel data=orion.customer_orders;panelby order_type customer_gender;vbar customer_country;title 'Orders by Country, Order Type,
Gender';run;
147
Removing Borders Between Cells
proc sgpanel data=orion.customer_orders;panelby customer_gender / noborder
spacing=30 novarname;vbar customer_country;title 'Orders by Country, Gender';run;
9/10/2013
50
148
Demonstration
This demonstration illustrates concepts
discussed in this section.
149
Reference: SGPANEL Procedure
The following section contains reference material on the
SGPANEL material. For a comprehensive syntax guide,
refer to the documentation topic, entitled: “The SGPANEL
Procedure” in the SAS/GRAPH(R) 9.2: Statistical
Graphics Procedures Guide, Second Edition.
150
PROC SGPANEL Plot Statements
Basic plots
– SCATTER, SERIES, NEEDLE,
– STEP, BAND, VECTOR
Fits and confidence
– REG, LOESS, PBSPLINE
Distributions
– HISTOGRAM, DENSITY
– VBOX, HBOX
Categorization
– DOT, VBAR, HBAR
– VLINE, HLINE
150
9/10/2013
51
151
PANELBY Statement and Options
PANELBY variable(s) </options>;
151
Option Description
BORDER |
NOBORDER
Shows or suppresses borders around each cell.
Default is determined by the style you are using.
COLHEADERPOS= |
ROWHEADERPOS=
Specified the location of column and row
headings in the panel in Lattice-type layouts.
Default for columns is TOP. Default for rows is
RIGHT for lattice layout.
COLUMNS= |
ROWS=
Specifies the number of columns and rows to
use in each panel (page).
LAYOUT= Specifies panel, lattice, columnlattice, or
rowlattice layout for the panel.
MISSING Treats missing values as a classification level
and creates cells for it.
continued...
152
PANELBY Statement and Options
152
Option Description
NOVARNAME Suppresses the name of the variable from row
and column headers and only displays the
variable values.
ONEPANEL Forces the panel to display on a single page.
SPACING= Specifies the number of pixels between rows
and columns in the panel.
SPARSE Shows empty cells for combinations of
classification variables with no data.
START= Places the first cell in the panel in the top left or
bottom left corner. Default is TOPLEFT.
UNISCALE= Creates identical scaling of shared axes for
rows and/or columns. Default is both (ALL).
153
PROC SGPANEL Additional Statements
Add elements to plots:
– KEYLEGEND
– REFLINE
Manipulate axes:
– COLAXIS, ROWAXIS
153
9/10/2013
52
154
PROC SGPANEL Axis Concepts
With the SGPANEL procedure, you have the ability to
control options for the plot axes. The COLAXIS statement
specifies the properties for each x-axis in the panel and
the ROWAXIS statement specifies the properties for each
y-axis in the panel.
154
rowaxis
colaxis
155
PROC SGPANEL AXIS Options
Axis statements use these selected options.
155
Option Usage
ALTERNATE Adds reference ticks to each side of the panel and alternates
the tick values for each row or column between the two sides.
DISPLAY= Specify which axis features should be displayed.
LABEL= Specify an axis label.
GRID Specify placement of a grid line at each tick on the axis.
TYPE= Specify the type of axis: DISCRETE, LINEAR, LOG, or TIME.
DISCRETEORDER= Specify the discrete tick value order.
INTERVAL= Specify the tick interval for a TIME axis.
FITPOLICY= Specify the method to fit horizontal axis tick mark values.
VALUES= Specify values for the tick marks on the axis.
REFTICKS Adds tick marks to the side of the panel that is opposite from
the specified axis. To add tick marks to the top side of the
panel, specify the REFTICKS option in the COLAXIS
statement.
156
Chapter 1: ODS Graphics for the Non-Statistician
1.1 What is ODS Graphics?
1.2 Producing Non-Statistical Graphs Using Base SAS
Procedures
1.3 SAS/GRAPH “SG” Procedures
1.4 Using PROC SGPLOT to Produce Non-Statistical Charts
and Plots
1.5 Controlling Graph Appearance with SGPLOT
1.6 Using PROC SGPANEL to Display Multiple Cells Per
Page
1.7 Using PROC SGSCATTER to Produce Multiple
Plots Per Page1.8 The ODS Graphics Designer
1.9 Editing Graphs Produced by SG Procedures
1.10 Upcoming Features and Conclusions
9/10/2013
53
157
Using PROC SGSCATTER to Produce Multiple Plots Per Page
158
PROC SGSCATTER
PROC SGSCATTER produces multiple scatter or
line plots on a page, with the cells reflecting different
combinations of plot variables.
159
General SGSCATTER Syntax
PROC SGSCATTER Statement – invokes the
procedure and specifies input data set
PLOT, COMPARE, MATRIX statement – specifies
type of graph, variables, and options.
Axis statements are not supported
Legend is controlled by options in PLOT statement
Note that:
– Only certain types of plots can be produced
– Plot type is specified as an option on the PLOT,
COMPARE, or MATRIX statement.
9/10/2013
54
160
PLOT Statement
proc sgscatter data=orion.budget;where month le 6;plot (yr2003 yr2004 yr2005 yr2006)*month;title 'Monthly Budget by Year';title2 '(First 6 Months)';run;
161
Arranging Multiple Cells on a Page
proc sgscatter data=orion.budget;where month le 6;plot (yr2003 yr2004 yr2005)*month/join
columns=1 uniscale=y;title 'Monthly Budget by Year';title2 '(First 6 Months)';run;
162
COMPARE Statement – Shared Axes
proc sgscatter data=orion.budget;where month le 6;compare x=month y=(yr2003 yr2004 yr2005) /
join spacing=10;title 'Monthly Budget by Year';title2 '(First 6 Months)';run;
9/10/2013
55
163
MATRIX Statement – Scatterplot Matrices
proc sgscatter data=orion.heart;where ageatstart>60;matrix weight diastolic systolic/group=sex
diagonal=(histogram normal);title 'Comparison of Weight, Systolic, and Diastolic';run;
164
SGPANEL vs. SGSCATTER
What’s the difference?
SGPANEL creates a separate cell for each value of a
categorical variable or variables.
– Example: Sales by Month with a separate cell for
each country
SGSCATTER creates cells for different combinations
of variables
– Example: Sales by Month and Cost by Month (for
all countries), on the same page
165
Demonstration
This demonstration illustrates concepts
discussed in this section.
9/10/2013
56
166
Reference: SGSCATTER Procedure
The following section contains reference material on the
SGSCATTER material. For a comprehensive syntax
guide, refer to the documentation topic, entitled: “The
SGSCATTER Procedure” in the SAS/GRAPH(R) 9.2:
Statistical Graphics Procedures Guide, Second Edition.
167
The SGSCATTER Procedure
167
PROC SGSCATTER < option(s)>;
COMPARE statement;
MATRIX statement;
PLOT statement;
RUN;
168
Forms of the PLOT Statement
PLOT plot-request-1 <…plot-request-n> </options>;
168
Plot request Description
y-variable * x-variable Creates one plot of y-by-x.
(y-variables) * (x-variables) Creates a plot for each combination
of y-by-x.
(y-variables) * x-variable Creates a plot of each y-variable
by x.
y-variable * (x-variables) Creates a plot of y by each
x-variable.
9/10/2013
57
169
Overlay Options
ELLIPSE
JOIN (line plot)
LOESS
PBSPLINE
REG
169
Option Description
REG Adds a regression fit line to the scatter plot with
optional confidence limits and line attributes.
ELLIPSE Adds a confidence (TYPE=MEAN) or prediction
(TYPE=PREDICTED) ellipse to the scatter plot.
LOESS Adds a LOESS fit to the scatter plot, with optional
confidence limits and line attributes.
PBSPLINE Adds a penalized B-spline fit to the scatter plot
with optional confidence limits and line attributes.
JOIN Specifies that line segments join all data points
in increasing order along the x-axis.
170
PROC SGSCATTER Axis Concepts
Because SGSCATTER plots are arranged in panels, any
axis adjustments must be made with syntax specific for
each statement:
170
SGSCATTER
Statement
Information About AXIS control
PLOT By default, the axis ranges of each cell are independent from
the other cells. However, you can use the UNISCALE= option
to specify that all of the cells use the same axis ranges for the
X axis, the Y axis, or both axes. The PLOT statement also
allows the use of the GRID option and the REFTICKS option.
COMPARE You can perform simple X or Y axis sharing by specifying only
one X or Y variable in your COMPARE statement. In addition,
the REFTICKS option allows you to duplicate the tick marks
from the X and Y axes on the opposite sides of the graph.
MATRIX PROC SGSCATTER does not give you explicit axis control
with the MATRIX statement output.
171
More Selected Options
171
Option Description
COLUMNS=
ROWS=
Specifies the number of columns or rows
to display the cells.
DATALABEL Displays variable or data values for each
point.
GRID Creates grid lines for each axis.
GROUP= Groups observations by a classification
variable. Symbols and fit lines are identified
by color and style.
continued...
9/10/2013
58
172
More Selected Options
Option Description
LEGEND= Specifies the appearance of the legend.
NOLEGEND Suppresses the legend.
MARKERATTRS= Specifies appearance of markers in the
plot.
SPACING= Specifies the number of pixels between
cells in the graph.
TRANSPARENCY= Specifies the degree of transparency for
plot components.
172
173
MATRIX Statement
Creates a scatter plot matrix with optional ellipse overlays
and optional distribution plots in the diagonal cells.
For n variables, a matrix of n-by-n cells is created, with
(n2–n) scatter plots.
173
174
MATRIX Statement
MATRIX variable-1 <…variable-n> </options>;
174
Option Description
DIAGONAL= Specifies graphs (HISTOGRAM, NORMAL,
KERNEL) to add to the diagonal cells.
DATALABEL= Displays variable values for each point.
ELLIPSE Adds a confidence or prediction ellipse to
each cell.
GROUP= Groups observations by a classification
variable. Symbols and fit lines are identified
by color and style.
continued...
9/10/2013
59
175
MATRIX Statement
Option Description
LEGEND= Specifies the appearance of the legend.
NOLEGEND Suppresses the legend from the graph.
START= Specifies whether the diagonal starts in the
top left corner or the bottom left corner.
MARKERATTRS= Specifies appearance of markers in the plot.
TRANSPARENCY= Specifies the degree of transparency for plot
components.
175
MATRIX variable-1 <…variable-n> </options>;
176
COMPARE Statement
Creates a comparative panel of scatter plots with shared
axes.
Supports several overlay options that are applied to all
cells of the graph.
176
177
Form of the COMPARE Statement
COMPARE X = variable | (variable-1…variable-n)
Y = variable | (variable-1…variable-n)
</options>;
177
Overlay Option Description
REG Adds regression fit lined to the scatter plots with
optional confidence limits and line attributes.
ELLIPSE Adds confidence (TYPE=MEAN) or prediction
(TYPE=PREDICTED) ellipses to the scatter plots.
LOESS Adds LOESS fits to the scatter plots, with optional
confidence limits and line attributes.
PBSPLINE Adds penalized B-spline fits to the scatter plots with
optional confidence limits and line attributes.
JOIN Specifies that line segments join all data points in
increasing order along the x-axis within each plot.
9/10/2013
60
178
Selected Options
178
Option Description
REFTICKS Duplicates the tick marks from the X and Y
axes on the opposite sides of the graph.
DATALABEL Displays variable or data values for each
point.
GRID Creates grid lines for each axis.
GROUP= Groups observations by a classification
variable. Symbols and fit lines are identified
by color and style.
continued...
179
Selected Options
Option Description
LEGEND= Specifies the appearance of the legend.
NOLEGEND Suppresses the legend.
MARKERATTRS= Specifies appearance of markers in the
plot.
SPACING= Specifies the number of pixels between
cells in the graph.
TRANSPARENCY= Specifies the degree of transparency for
plot components.
179
180
Chapter 1: ODS Graphics for the Non-Statistician
1.1 What is ODS Graphics?
1.2 Producing Non-Statistical Graphs Using Base SAS
Procedures
1.3 SAS/GRAPH “SG” Procedures
1.4 Using PROC SGPLOT to Produce Non-Statistical Charts
and Plots
1.5 Controlling Graph Appearance with SGPLOT
1.6 Using PROC SGPANEL to Display Multiple Cells Per
Page
1.7 Using PROC SGSCATTER to Produce Multiple Plots Per
Page
1.8 The ODS Graphics Designer1.9 Editing Graphs Produced by SG Procedures
1.10 Upcoming Features and Conclusions
9/10/2013
61
181
The ODS Graphics Designer
182
ODS Graphics Designer
The ODS Graphics Designer provides an interactive
interface for creating the same types of graphs produced
by the SGPLOT, SGPANEL, and SGRENDER
procedures. It also allows creation of some types of
graphs that cannot be created with the above procedures.
To invoke the ODS Graphics Designer, submit the
following statement from your SAS Editor:
The ODS Graphics Designer comes up in a separate
application window.
%sgdesign;
183
Graphics Designer Main Window
9/10/2013
62
184
Creating a Graph Using the Designer
Select a graph type from the Graph Gallery. Graph is
displayed using default data sets and variables.
Fill in popup form to specify your data set, variables,
and options. Updated graph is displayed.
Add additional plots and elements by dragging them
from the Elements Pane onto the graph
or
Add additional panels to the graph and then drag plots
and elements into the panels.
185
Select a Graph from Graph Gallery
Graph is created with default data
Data menu is displayed
186
Choose Data Set and Variables
9/10/2013
63
187
Add Title and Right Click for Options
188
After Changes
189
Drag Legend On To Graph
9/10/2013
64
190
Add Another Panel to Graph
Select Row from Insert pull-down.
191
New Row Is Added
192
Drag a Plot Into Empty Row & Specify Details
9/10/2013
65
193
Overlay Another Plot in Panel
Drag Plot Layer into Panel and Fill in Details.
194
Plot After Adding Overlay
195
Right Click Plot and Add Enhancements
9/10/2013
66
196
Save Graph to Gallery
197
Specify Location and Name, then Save
We will come back to
the graph later…..
198
Creating Paneled Graphs
Select Panel tab from Gallery and choose graph type.
9/10/2013
67
199
Fill in Panel and Plot Variables
200
Paneled Graph
201
Some Other Graphs in the Gallery
9/10/2013
68
202
Click "My Graphs" Tab to See Saved Graphs
203
Can I Save Code Generated by the Designer?
Good news – code can be viewed and saved
Bad news – code is Graphics Template Language and
PROC SGRENDER, but…
Good news – the code is a good way to learn GTL!
204
Click View Code to See Generated Code
9/10/2013
69
205
Using Saved Layouts as Templates for Other Graphs Double-click graphs from Gallery window, and change
parameters as needed
Modify and run the generated GTL code with PROC
SGRENDER
Use PROC SGDESIGN
– You can change input data set and plot variables in
the PROC SGDESIGN code.
206
Using PROC SGDESIGN
This program replays the saved graph, specifying a
different grouping variable for the bar chart.
proc sgdesign sgd='globalforum2011.sgd';dynamic _order_type='supplier';run;
207
Demonstration
This demonstration illustrates concepts
discussed in this section.
9/10/2013
70
208
Chapter 1: ODS Graphics for the Non-Statistician
1.1 What is ODS Graphics?
1.2 Producing Non-Statistical Graphs Using Base SAS
Procedures
1.3 SAS/GRAPH “SG” Procedures
1.4 Using PROC SGPLOT to Produce Non-Statistical Charts
and Plots
1.5 Controlling Graph Appearance with SGPLOT
1.6 Using PROC SGPANEL to Display Multiple Cells Per
Page
1.7 Using PROC SGSCATTER to Produce Multiple Plots Per
Page
1.8 The ODS Graphics Designer
1.9 Editing Graphs Produced by SG Procedures
1.10 Upcoming Features and Conclusions
209
Editing Graphs Produced by SG Procedures
210
Purpose of the ODS Graphics Editor
Primary purpose – make simple, ad-hoc changes to
graphs created by SG and other procedures.
Add simple annotation to graphs (text, lines, symbols)
Data components (including tick labels) cannot be
modified.
9/10/2013
71
211
Enabling the Editing of ODS Graphics
You can use the ODS Graphics Editor to make edits to
graphs created with ODS graphics. The ODS Editor
cannot be used to edit graphcs created with "classic"
SAS/GRAPH procedures.
To enable use of the editor, submit the following
statement:
ods listing sge=on;
212
Creating an Editable Graph
When a SG procedure is run and SGE=ON is specified,
the results window shows two graphs – one is an image
file, and the second is the editable graph.
image file
editable
graph
213
Opening the Editable Graph
Double-clicking on the editable graph opens it in the
ODS Graphics Editor.
9/10/2013
72
214
Editing the Graph
Right clicking on a graph component brings us a dialog
you can use to modify the component.
215
Graph After Editing
216
Saving the Edited Graph
Click on File Save as….
9/10/2013
73
217
Saving the Edited Graph
You can save the edited graph as either:
A .png (Portable Network Graphics) file, which can be
printed or inserted into other documents
A .sge file, which can be opened and re-edited with
the ODS Graphics Editor
The ODS Graphics Editor does not generate SAS
code.
218
Demonstration
This demonstration illustrates concepts
discussed in this section.
219
Chapter 1: ODS Graphics for the Non-Statistician
1.1 What is ODS Graphics?
1.2 Producing Non-Statistical Graphs Using Base SAS
Procedures
1.3 SAS/GRAPH “SG” Procedures
1.4 Using PROC SGPLOT to Produce Non-Statistical Charts
and Plots
1.5 Controlling Graph Appearance with SGPLOT
1.6 Using PROC SGPANEL to Display Multiple Cells Per
Page
1.7 Using PROC SGSCATTER to Produce Multiple Plots Per
Page
1.8 The ODS Graphics Designer
1.9 Editing Graphs Produced by SG Procedures
1.10 Upcoming Features and Conclusions
9/10/2013
74
220
Upcoming Features and Conclusions
221
Upcoming ODS Graphics Features in SAS 9.3
Clustered groups for bar charts
Fill pattern "skins" for bar charts
Bubble plots
High-Low Plots
Pie Charts
Attribute maps
Annotation
Maps (experimental)
Default output to web page instead of image file
222
Clustered Groups and "Skins" for Bar Charts
9/10/2013
75
223
Bubble Plots
224
High-Low Plots
225
Pie Charts
9/10/2013
76
226
Annotate
227
Some Considerations
You cannot use SAS to combine ODS Graphics with
graphs produced by "classic" SAS/GRAPH procedures
ODS Graphics cannot produce some types of graphs that
"classic" SAS/GRAPH procedures can, such as:
Pie and donut charts
Radar charts
3D bar charts
Maps
Tile charts
ODS Graphics does not (yet) support Annotate
continued...
228
Some Considerations
ODS Graphics can produce the following types of graphs
that "classic" procedures cannot, including:
Density plots
Ellipse plots
Vector plots
Horizontal box plots
ODS Graphics can produce the following types of graphs
much more easily than "classic" SAS/GRAPH procedures:
Overlay plots
Panel graphs
Plot matrices
9/10/2013
77
229
Which Should I Choose?
If you are already creating graphs for a specific
application using "classic" SAS/GRAPH procedures,
continue to use them (they won’t go away).
If your graphs require complex annotation, use
"classic" SAS/GRAPH procedures.
Consider using SG procedures for new applications
that do not require complex annotation.
230