SAS Lecture 6 – SAS/GRAPH Aidan McDermott, May 3, 2005
SAS/GRAPH • There are a small number of graphic types commonly used in public health presentations and publication. • These basic types are either used alone or mixed together to form a composite graphic. • Here we will look at how to build some of these basic types of graph. • Golden Rule: Everybody is a graph critic.
Two types of graph maker • If you are using SAS for statistics and data management then it seems natural to use it to produce your graphs as well. Sometimes a statistical procedure will produce the graph you are looking for anyway. • Need a one-off graph for a presentation versus production line graphs. • To produce “quick and dirty” graphs you can use Graph-n-go. • Very easy to use; not bad for putting multiple graphs on one page; data viewer is a graph type; only a small number of graph types available; not all options available; labor intensive so not suitable for production line graphs. • Use SAS/Graph procedures • Very flexible; complete control over graphic elements; less labor intensive in the long run; harder to learn;same control can be used for SAS/STAT graphics output.
Some common types of graph • Charts • Histograms • Stem and leaf plots • Boxplots • Plots • Contour plots / 3-dimensional plots • Maps • Gantt charts • Trellis plots • Trees / pedigrees / dendograms • (mathematical) graphs / networks • Flow charts / entity-relationship diagrams
Graph-n-go • Solutions reporting graph-n-go • The top two icons represent data models • The rest are data viewers.
Graph-n-go • Choose and configure a data model. • Choose a dataset. • Right mouse button click on the data model and choose properties. • Set which columns to use, where clauses etc.
Graph-n-go Choose a viewer and position it on the viewer area (e.g. a bar chart). • Drag and drop the data model onto the viewer to associate data with the viewer. • Right mouse button on the viewer and choose properties. • Configure (choose variables to plot etc).
Graph-n-go When finished graph can be exported to html etc. Choose file export write to file You’ll see more in the lab.
Graphic output within SAS • You have already seen some graphic output from within SAS. • proc means, proc univariate, proc genmod, proc lifetest etc. all produce graphs • Other procedures in SAS specifically produce graphs, even some procedures that are not part of SAS/Graph (proc boxplot is an example) Here our aim is to produce publication/presentation-- quality graphs.
Graph basics • SAS stores graphs in catalogs (an entity similar to a folder in windows). • Graphs are stored in a SAS proprietary format. • By default graphs are stored in a catalog called Gseg in the work library. • Graphs can be translated to postscript, gif, jpeg, and a number of other commonly used formats for printing or including in other documents (Word, html, etc.).
Graphic control • There are three ways to control the look of a sas/graph. • 1. Use options within the procedure • 2. Use global commands • 3. Use goptions
GOPTIONS • set the environment for a graphics program to run and send output • independent of the program • remain in effect for the entire SAS session unless changed or reset • control appearance of graphic elements by specifying default fonts, colors, text heights etc. Useful when you want the same options in multiple procs
PROC GOPTIONS • used to review current GOPTIONS • lists alphabetically all of the current GOPTIONS in the LOG window proc goptions; run; • Can also type goptions at the command line
GOPTIONS GOPTIONS options-list • ROTATE= portrait or landscape (will override the setting in the print dialog box) • RESET=ALL resets all options to defaults including all global statements • RESET=GOPTIONS resets only goptions statements
COLORS=device dependent default color list for device driver • GUNIT= unit of measurement for height in global statements, such as TITLE and FOOTNOTE cell - character cells pct - percent of graphics area in - inches
Data • From the SAS samples folder. • Three Californian pollutant monitoring stations (AZU, LIV, SFO) • One monthly measurement (taken on the 15th of the month) for CO, O3, SO4, temperature etc. for each station. • 36 observations in all • Month is a numeric variable taking the value 1 for January, 2 for February, etc.
Charts • Examples • Look forgraphic elementsin each chart • Look forcommon data types • Look forsimilaritiesamong the examples
Charts • All the examples used a small number of graphic elements • Main difference between plots is the polygon/area type • Most involved a categorical/discrete variable and a numeric variable. • A histogram uses a continuous variable to create categories. The counts of a categorical variable can be used to create the numeric variable.
Proc GCHART • produces charts based on the values of one or more chart variables. • produces vertical and horizontal bar charts, block charts, pie charts etc. • graphs based on statistics - counts, percentages, sums, or means • run-group processing • numeric and character variables
Proc GCHART example • proc format; value seas 1 = ‘Win’ 2 = ‘Spr’ • 3 = ‘Sum’ 4 = ‘Fal’; • data ca88air; • set vol1.ca88air(where=(station=“SFO”)); • if ( month in (12,1,2) ) then season = 1; • else if ( month in (3,4,5) ) then season = 2; • else if ( month in (6,7,8) ) then season = 3; • else if ( month in (9,10,11)) then season = 4; • format season seas.; • format month mth.; • run;
Proc GCHART example • title1 h=4 • ’Mean seasonal carbon monoxide for station SFO’; • footnote j=l h=4 f=simplex • 'Bar Chart - vertical’; • proc gchart data=ca88air; • vbar season / sumvar=co type=mean • discrete • ctext=black clm=95 ; • run; • quit;
Proc GCHART syntax PROC GCHART data=data set name; • One of the following: • VBARvariables / options; • HBARvariables / options; • STARvariables / options; • PIEvariables / options; • BLOCKvariables / options; run;
VBAR • separate bar chart for each chart variable • each bar represents the statistic selected for a value of the chart variable • response axis (vertical) provides a scale for statistic graphed • midpoint axis - horizontal axis
VBAR SYNTAX VBAR chart variables/ options; chart-variable(s) specifies one or more variables that define the categories of data to chart. options specifies appearance, statistics, axes and midpoint options
VBAR • midpoints are the values of the chart variable that identify categories of data. By default, midpoints are selected or calculated by the procedure. The way the procedure handles the midpoints depends on whether the values of the chart variable are character, discrete numeric, or continuous numeric. • character chart variables- separate bar is drawn for each value
VBAR • numeric chart variables - each bar represents a range of values • DISCRETE option generates a midpoint for each unique value of the chart variable. • generates midpoints that represent ranges of values. By default, determines the ranges, calculates the median value of each range, and displays the median value at each midpoint on the chart. A value that falls exactly halfway between two midpoints is placed in the higher range.
VBAR OPTIONS • For character or discrete numeric values, you can use the MIDPOINTS= option to rearrange the midpoints or to exclude midpoints from the chart. For character data MIDPOINTS= list values in quotes MIDPOINTS=‘Sydney’ ‘Atlanta’ ‘Paris’
VBAR OPTIONS • For continuous numeric variables, use the MIDPOINTS= option to change the number of midpoints, to control the range of values each midpoint represents, or to change the order of the midpoints. To control the range of values each midpoint represents, use the MIDPOINTS= option to specify the median value of each range. For example, to select the ranges 20-29, 30-39, and 40-49, specify MIDPOINTS=25 35 45
VBAR OPTIONS Other options; DISCRETE separate bar for each value of numeric variable TYPE=statistic specifies the chart statistic. FREQ frequency PCT percentage SUM sum (the default) MEAN mean CLM=confidence-level draws chart confidence intervals (error bars)
VBAR SYNTAX SUMVAR=variable specifies variable to used for sum or mean calculations for each midpoint. The resulting statistics are represented by the length of the bars along the response axis, and they are displayed at major tick marks. REQUIRED if specifying TYPE-MEAN or SUM. RAXIS= axisn response axis MAXIS=axisn midpoint axis
GLOBAL STATEMENTS • define titles, footnotes • used to control axes, symbols, patterns, and legends • can be defined anywhere • inside a proc or before a proc • in effect until canceled, replaced, or the end of SAS session • cancel by repeating statement with no options or using goptions RESET=ALL;
GLOBAL STATEMENTS • TITLE defines titles • AXIS defines appearance of axes • FOOTNOTE defines footnotes • PATTERN defines patterns used in graphs (histograms) • LEGEND defines legends • SYMBOL defines symbols (plotting) • NOTEadds text to graph
TITLE STATEMENT • creates, changes or cancels a title for all subsequent graphics output in a SAS session • allowed up to 10 titles • keyword TITLE can be followed by unlimited number of text strings and options • text strings enclosed in single or double quotes • most recently created TITLE number replaces the previous TITLE of the same number
Title syntax TITLE<1,2....10> <options | ‘text’> ...... <options-n>| ‘text-n’>; Options: FONT=font specifies the font for the subsequent text. HEIGHT= specifies the height of text H=n<units> characters in number of units JUSTIFY= specifies the alignment J=R|L|C By default, JUSTIFY=C=center R=right L=left.
PATTERN STATEMENT • defines the characteristics of patterns used in charts • type of fill pattern - solid, empty, lined • color An example of a global statement
PATTERN STATEMENT PATTERN <1....99> options; OPTIONS COLOR=pattern color VALUE=fill E empty Ssolid Lnleft slanting lines Rnright slanting lines Xncrosshatched lines where n is 1-5 1 indicating the lightest
Proc GCHART example • pattern1 color=blue value=fill; • pattern2 color=red value=fill; • proc gchart data=ca88air; • star month / sumvar=co type=mean • discrete • ctext=black noheading ; • run; • quit;
Exporting graphs • Make sure the graphics window has focus, by clicking on it. • File export as Image • select type of image – gif, … • open other software program – Powerpoint • insert picture
Saving graphs • Graphs can also be saved in a SAS catalog. • They are stored in a SAS proprietary format. • They can be viewed withproc greplay. goptions replace; libname mylib ‘c:\Temp\sasclass\myfiles’; proc gchart data=mydat gout=lib.mygraphs; … proc greplay allows multiple plots on one page.