SUGI 30 Tutorials Paper 262-30 An Introduction to the Simplicity and Power of SAS/Graph Dan Bruns,Chattanooga,TN IN THE BEGINNING In today's hectic and ever increasing demand for more information faster,the graphics tools for SAS are a great way to get lots of information conveyed quickly.I have seen tremendous enhancements and improvements in the product since it was first introduced (and first leamned it)in the late 1970s.But the basic,underlying,data-driven principals (like for most SAS procedures)are still there and easy to leam.In this tutorial we will explore how a little understanding and knowledge of the basic concepts and options of SAS/Graph will get you on your way to producing all kinds of impressive and informative graphs that can be printed,imbedded in other docs,or made available to web sites. SOME BASICS There are several procedures and statements that come with the SAS/GRAPH product,but we will only cover a few of the most powerful procedures and all the global statements in order to give you a flavor of what can be done. Within each of the procedures and statements we cover there will be dozens of options,but we will cover just a few of the most powerful. SAS/GRAPH Procedures GANNO--displays graphs created by Annotate data sets. GAREABAR--enables you to produce an area bar chart showing the magnitudes of two variables for each category of data GBARLINE--produces bar line charts.Bar line charts are vertical bar charts with a plot overlay. GCHART--produces six types of charts:block charts,horizontal and vertical bar charts,pie and donut charts,and star charts.These charts graphically represent the value of a statistic calculated for one or more variables in an input SAS data set. GCONTOUR--produces plots that represent three-dimensional relationships.The colors,contours,or surface areas of a contour plot represent the values of a contour variable at each point in a plane that is formed by a dependent and an independent variable. GDEVICE--is a tool for examining and changing the parameters of the graphics device driver catalog entries used with SAS/GRAPH software. GFONT--displays new or existing fonts and creates user-generated fonts for use in SAS/GRAPH programs. GIMPORT--enables you to import into SAS/GRAPH software graphics output that is produced with other software applications,graphics output that is produced by SAS/GRAPH software,or graphics output that is produced on other machines. GMAP--produces two-dimensional(choropleth)or three-dimensional(block,prism.and surface)color maps that show variations of a variable value with respect to an area GOPTIONS--provides information about the values of graphics options and the global statement definitions that are currently in effect in your session. GPLOT--plots the values of two or more variables on a set of coordinate axes (X and Y).The coordinates of each point on the plot correspond to two variable values in an observation of the input data set GPRINT--converts a text file into graphics output that can be displayed or printed on a graphics output device. GPROJECT-processes traditional map data sets by converting spherical coordinates(longitude and latitude)into Cartesian coordinates for use by the GMAP procedure. GMAP--produces two-dimensional(choropleth)or three-dimensional(block,prism,and surface)color maps that show variations of a variable value with respect to an area. GRADAR--creates radar(or star)charts that show the relative frequency of data measures in quality control or market research problems.(The chart statistics are displayed along spokes that radiate from the center of the chart, hence the term "star"charts). GREDUCE--processes map data sets so that they can draw simpler maps with fewer boundary points. GREMOVE--combines unit areas defined in a map data set into larger unit areas by removing shared borders between the original unit areas. GREPLAY--displays and manages graphics output that is stored in SAS catalogs.The GREPLAY procedure also creates templates and color maps that you can use when you replay your graphics output. GSLIDE--is useful for creating text slides for presentations.You can overlay text slides on other graphics output with
1 Paper 262-30 An Introduction to the Simplicity and Power of SAS/Graph® Dan Bruns, Chattanooga, TN IN THE BEGINNING In today’s hectic and ever increasing demand for more information faster, the graphics tools for SAS are a great way to get lots of information conveyed quickly. I have seen tremendous enhancements and improvements in the product since it was first introduced (and first learned it) in the late 1970s. But the basic, underlying, data-driven principals (like for most SAS procedures) are still there and easy to learn. In this tutorial we will explore how a little understanding and knowledge of the basic concepts and options of SAS/Graph will get you on your way to producing all kinds of impressive and informative graphs that can be printed, imbedded in other docs, or made available to web sites. SOME BASICS There are several procedures and statements that come with the SAS/GRAPH product, but we will only cover a few of the most powerful procedures and all the global statements in order to give you a flavor of what can be done. Within each of the procedures and statements we cover there will be dozens of options, but we will cover just a few of the most powerful. SAS/GRAPH Procedures GANNO -- displays graphs created by Annotate data sets. GAREABAR -- enables you to produce an area bar chart showing the magnitudes of two variables for each category of data. GBARLINE -- produces bar line charts. Bar line charts are vertical bar charts with a plot overlay. GCHART -- produces six types of charts: block charts, horizontal and vertical bar charts, pie and donut charts, and star charts. These charts graphically represent the value of a statistic calculated for one or more variables in an input SAS data set. GCONTOUR -- produces plots that represent three-dimensional relationships. The colors, contours, or surface areas of a contour plot represent the values of a contour variable at each point in a plane that is formed by a dependent and an independent variable. GDEVICE -- is a tool for examining and changing the parameters of the graphics device driver catalog entries used with SAS/GRAPH software. GFONT -- displays new or existing fonts and creates user-generated fonts for use in SAS/GRAPH programs. GIMPORT -- enables you to import into SAS/GRAPH software graphics output that is produced with other software applications, graphics output that is produced by SAS/GRAPH software, or graphics output that is produced on other machines. GMAP -- produces two-dimensional (choropleth) or three-dimensional (block, prism, and surface) color maps that show variations of a variable value with respect to an area. GOPTIONS -- provides information about the values of graphics options and the global statement definitions that are currently in effect in your session. GPLOT -- plots the values of two or more variables on a set of coordinate axes (X and Y). The coordinates of each point on the plot correspond to two variable values in an observation of the input data set. GPRINT -- converts a text file into graphics output that can be displayed or printed on a graphics output device. GPROJECT -- processes traditional map data sets by converting spherical coordinates (longitude and latitude) into Cartesian coordinates for use by the GMAP procedure. GMAP -- produces two-dimensional (choropleth) or three-dimensional (block, prism, and surface) color maps that show variations of a variable value with respect to an area. GRADAR -- creates radar (or star) charts that show the relative frequency of data measures in quality control or market research problems. (The chart statistics are displayed along spokes that radiate from the center of the chart, hence the term “star” charts). GREDUCE -- processes map data sets so that they can draw simpler maps with fewer boundary points. GREMOVE -- combines unit areas defined in a map data set into larger unit areas by removing shared borders between the original unit areas. GREPLAY -- displays and manages graphics output that is stored in SAS catalogs. The GREPLAY procedure also creates templates and color maps that you can use when you replay your graphics output. GSLIDE -- is useful for creating text slides for presentations. You can overlay text slides on other graphics output with SUGI 30 Tutorials
SUGI 30 Tutorials the GREPLAY procedure. GTESTIT--is a diagnostic tool for testing the installation of SAS/GRAPH software and the configuration of your device. G3D-produces three-dimensional graphs that plot one vertical variable(z)for a position on a plane that is specified by two horizontal variables (x and y). MAPIMPORT--enables you to import ESRI shapefiles (spatial data formats)and process the SHP files into SAS/GRAPH traditional map data sets. SAS/GRAPH Global Statements AXIS--modifies the appearance,position,and range of values of axes in charts and plots. GOPTIONS -specifies graphics options that control the appearance of graphics elements by specifying characteristics such as default colors,fill patterns,fonts,or text height.Graphics options can also temporarily change device settings. LEGEND--modifies the appearance and position of legends generated by procedures that produce charts.plots. and maps. PATTERN-controls the color and fill of patterns that are assigned to areas in charts,maps,and plots. SYMBOL--specifies the shape and color of plot symbols as well the interpolation method for plot data.It also controls the appearance of lines in contour plots. TITLE and FOOTNOTE--add titles and footnotes to graphics output. GPLOT PROCEDURE One of the really unique features of most SAS procedures is that they are driven by the data.And that becomes even more apparent when you see the output of the SAS/GRAPH procedures.We are going to begin with the GPLOT procedure because everyone has plotted data points a graph at some point.And that is all SAS is going to do....plot a point (with an x-value and a y-value)on the graph for each observation. With the PLOT statement you request plots to be generated with sets of Y-var*X-var pairs.GPLOT and several of the graph procedures support run-group processing. proc gplot data=sashelp.shoes; plot Returns Sales run; $50.000 $40.000 $30.000 $20.000 4+ $10.000 $0 0 $200.000 $400.000 $600.000$800.000$1.000.000$1.200.000$1.400.000 Total Sa es 2
2 the GREPLAY procedure. GTESTIT -- is a diagnostic tool for testing the installation of SAS/GRAPH software and the configuration of your device. G3D -- produces three-dimensional graphs that plot one vertical variable (z) for a position on a plane that is specified by two horizontal variables (x and y). MAPIMPORT -- enables you to import ESRI shapefiles (spatial data formats) and process the SHP files into SAS/GRAPH traditional map data sets. SAS/GRAPH Global Statements AXIS -- modifies the appearance, position, and range of values of axes in charts and plots. GOPTIONS -- specifies graphics options that control the appearance of graphics elements by specifying characteristics such as default colors, fill patterns, fonts, or text height. Graphics options can also temporarily change device settings. LEGEND -- modifies the appearance and position of legends generated by procedures that produce charts, plots, and maps. PATTERN -- controls the color and fill of patterns that are assigned to areas in charts, maps, and plots. SYMBOL -- specifies the shape and color of plot symbols as well the interpolation method for plot data. It also controls the appearance of lines in contour plots. TITLE and FOOTNOTE -- add titles and footnotes to graphics output. GPLOT PROCEDURE One of the really unique features of most SAS procedures is that they are driven by the data. And that becomes even more apparent when you see the output of the SAS/GRAPH procedures. We are going to begin with the GPLOT procedure because everyone has plotted data points a graph at some point. And that is all SAS is going to do….plot a point (with an x-value and a y-value) on the graph for each observation. With the PLOT statement you request plots to be generated with sets of Y-var * X-var pairs. GPLOT and several of the graph procedures support run-group processing. proc gplot data=sashelp.shoes; plot Returns * Sales ; run; Tot al Ret urns $0 $10, 000 $20, 000 $30, 000 $40, 000 $50, 000 $60, 000 Tot al Sal es $0 $200, 000 $400, 000 $600, 000 $800, 000 $1, 000, 000 $1, 200, 000 $1, 400, 000 SUGI 30 Tutorials
SUGI 30 Tutorials where Region a器品 + in("United states", s17,000 "Eastern Europe"); s16.000 plot Returns Sales; s1500 s14,000 run; s13000 s12000 Notice the default plot symbol is a plus s11,00四 sign and black is the default color for s1a,00 most everything.But rest assured that 000 and virtually everything else about the sa 000 S7.o00 graph you are able to control.In the 000 second plot,can you tell which Region 50001 each point is from? S4 000 000 200 s1,000 0 s100000 20000 300000 9400000 $g00000 where Region in("United states", a照S s17.000 "Eastern Europe"); s16000 plot Returns Sales Region; $15000 run; s14.000 313000 12000 Using the Y*X=variable notation tells $11.000 SAS to cycle through the colors from the s10000 devices color list for each unique value of 0,000 Region.If there are more values than 58.000 colors in the list,another symbol is $7,000 $8,000 chosen and the colors cycle again.The s5000 windows display is known as the WIN 4,000 device and has 11 colors(black,red, $3000 green,blue,cyan,magenta,grey,pink, 2,000 1,000 orange,brown,and yellow). 0 TTTT TTT 100,00m s200,00D 30m000 $40m,000 s50m,000 Tota 8io Regi on ++Eactorn Brope+++uitod gates In nearly all the graphics procedures,the statement that requests a certain kind of graphic will also allow you to specify options to control and enhance the graph.To specify those options on the PLOT statement,simply follow the plot request(s)with a slash followed by the plot options you want applied to the requested plots.There are far too many to cover all of them in this tutorial,so we will mention a few of the more powerful ones. You will notice that most options in SAS/GRAPH that begin with the letter"C"will refer to a color specification,those that begin with the letter"H"will refer to a height specification,those that begin with the letter"F"will refer to a font specification,and those that begin with the letter"L"will refer to a line-type specification. When in doubt about any specification,such as a color or font,always refer the Reference Manual.Especially line- types since they are a coded value of 1 to 46(1 being solid;2-46 some form of dashes).Over time you will get more use to them and develop your favorites. where Region The CAXIS defines the color for all the axis and CTEXT in("Unites states","Eastern Europe"); defines the color for all the text on the plot.By plot Returns Sales=Region specifying the GRID option you get what are called caxis=blue reference lines at the major tick marks on the vertical ctext=red and horizontal axis and will be a dashed line and the grid same color as the axis. run; 3
3 where Region in("United States", "Eastern Europe"); plot Returns * Sales; run; Notice the default plot symbol is a plus sign and black is the default color for most everything. But rest assured that and virtually everything else about the graph you are able to control. In the second plot, can you tell which Region each point is from? where Region in("United States", "Eastern Europe"); plot Returns * Sales = Region; run; Using the Y*X=variable notation tells SAS to cycle through the colors from the devices color list for each unique value of Region. If there are more values than colors in the list, another symbol is chosen and the colors cycle again. The windows display is known as the WIN device and has 11 colors (black, red, green, blue, cyan, magenta, grey, pink, orange, brown, and yellow). Tot al Ret urns $0 $1, 000 $2, 000 $3, 000 $4, 000 $5, 000 $6, 000 $7, 000 $8, 000 $9, 000 $10, 000 $11, 000 $12, 000 $13, 000 $14, 000 $15, 000 $16, 000 $17, 000 $18, 000 Tot al Sal es $0 $100, 000 $200, 000 $300, 000 $400, 000 $500, 000 Regi on East ern Europe Uni t ed St ates Total Ret urns $0 $1,000 $2,000 $3,000 $4,000 $5,000 $6,000 $7,000 $8,000 $9,000 $10,000 $11,000 $12,000 $13,000 $14,000 $15,000 $16,000 $17,000 $18,000 Tot al Sal es $0 $100, 000 $200, 000 $300,000 $400,000 $500,000 In nearly all the graphics procedures, the statement that requests a certain kind of graphic will also allow you to specify options to control and enhance the graph. To specify those options on the PLOT statement, simply follow the plot request(s) with a slash followed by the plot options you want applied to the requested plots. There are far too many to cover all of them in this tutorial, so we will mention a few of the more powerful ones. You will notice that most options in SAS/GRAPH that begin with the letter “C” will refer to a color specification, those that begin with the letter “H” will refer to a height specification, those that begin with the letter “F” will refer to a font specification, and those that begin with the letter “L” will refer to a line-type specification. When in doubt about any specification, such as a color or font, always refer the Reference Manual. Especially linetypes since they are a coded value of 1 to 46 (1 being solid; 2-46 some form of dashes). Over time you will get more use to them and develop your favorites. where Region in("Unites States","Eastern Europe"); plot Returns * Sales=Region / caxis=blue ctext=red grid ; run; The CAXIS defines the color for all the axis and CTEXT defines the color for all the text on the plot. By specifying the GRID option you get what are called reference lines at the major tick marks on the vertical and horizontal axis and will be a dashed line and the same color as the axis. SUGI 30 Tutorials
SUGI 30 Tutorials Tat al Rturns $18.000 $17,000 $16000 $15000 $14.000 $13.000 $12,000 $11.000 $10000 $9,000 tf 58000 $7,000 6,000 + 5000 十 $4.000 + + $53000 $2,000 $1,000 0 T7 0 $100.000 $200.000 $300.000 $400.000 $500.000 Regi on +++Eastern Erope++uited gates If you need more control of the reference lines,use the AUTOHREF and AUTOVREF options to have SAS automatically generate reference lines.Then use the LHREF(line-type for horizontal ref line),LVREF(line-type for vertical ref line).CHREF (color for horizontal ref line),and CVREF (color for vertical ref line)to control the line-type and color of the reference lines. where Region in("United states", s17.000 "Eastern Europe"); s1800 plot Returns Sales=Region s15000 s14000 autohref lhref=2 s13000 chref=lime s12000 autovref lvref-5 s11.000 cvref=pink $10000 caxis=blue s8,000 ctext=red 7,000 run; 8,000 5000 ,000 3,000 2000 1,000 100.000 200000 400000
4 Regi on East ern Europe Uni t ed St at es Tot al Ret urns $0 $1, 000 $2, 000 $3, 000 $4, 000 $5, 000 $6, 000 $7, 000 $8, 000 $9, 000 $10, 000 $11, 000 $12, 000 $13, 000 $14, 000 $15, 000 $16, 000 $17, 000 $18, 000 Total Sal es $0 $100, 000 $200, 000 $300, 000 $400, 000 $500,000 If you need more control of the reference lines, use the AUTOHREF and AUTOVREF options to have SAS automatically generate reference lines. Then use the LHREF (line-type for horizontal ref line), LVREF (line-type for vertical ref line), CHREF (color for horizontal ref line), and CVREF (color for vertical ref line) to control the line-type and color of the reference lines. where Region in("United States", "Eastern Europe"); plot Returns * Sales=Region / autohref lhref=2 chref=lime autovref lvref=5 cvref=pink caxis=blue ctext=red ; run; Regi on East ern Europe Uni t ed St at es Tot al Ret urns $0 $1, 000 $2, 000 $3, 000 $4, 000 $5, 000 $6, 000 $7, 000 $8, 000 $9, 000 $10, 000 $11, 000 $12, 000 $13, 000 $14, 000 $15, 000 $16, 000 $17, 000 $18, 000 Tot al Sal es $0 $100, 000 $200, 000 $300, 000 $400, 000 $500, 000 SUGI 30 Tutorials
SUGI 30 Tutorials Another powerful option is VAXIS (and HAXIS)to specify the major tick marks of the vertical axis.If any point lies 4 beyond what you specify.it is NOT plotted-so be careful. 10000 where Region in("United States", "Eastern Europe"); plot Returns Sales=Region vaxis=0 to 15000 by 5000 5000 autohref lhref=2 chref=lime autovref lvref=5 ++ cvref-pink 我 caxis=blue ctext=red 50 雪100000 82am000 30m.000 5400000 00000 run; GLOBAL STATEMENTS Titles Using global statements gives you even MORE control of certain aspects of the graphs along with more options to further enhance the graph.Let's take a look at the TITLE statement. titlel c=darkblue h=2.5 f=swissb "SAS/Graph c=darkred h=3.0 f=swissbi "GPLOT Example"; Can you guess what the C=,H=,and F=are specifying? That's right!-Color,Height,and Font.The quoted text following the options will take on those specifications. Axis Instead of using the VAXIS option to define the vertical axis,let's code an AXIS statement (which is much more powerful)and point to it in the plot option. axisl label=(c=darkorange h=1.5 f=zapfbi j=r "Total Returns") offset=(0.2 in order=(0 to 15000 by 5000) value=(c=darkorange f=swissl); Since you can(and usually do)have more than one axis statement,you need to number them(from 1-99)just like a title statement. The LABEL=option defines the options for just the label of the axis.The J=option in the justification of the text- left(L),center(C),right(R). The ORDER=defines the range of values and major tick marks. The VALUE=define the attributes of the labeling of the axis and the tick marks. The OFFSET=defines how far from the lower left corner of the graph to start the first tick mark
5 Another powerful option is VAXIS (and HAXIS) to specify the major tick marks of the vertical axis. If any point lies beyond what you specify, it is NOT plotted—so be careful. where Region in("United States", "Eastern Europe"); plot Returns * Sales=Region / vaxis=0 to 15000 by 5000 autohref lhref=2 chref=lime autovref lvref=5 cvref=pink caxis=blue ctext=red ; run; Regi on East ern Europe Uni t ed Stat es Tot al Returns $0 $5, 000 $10, 000 $15, 000 Tot al Sal es $0 $100, 000 $200,000 $300,000 $400, 000 $500, 000 GLOBAL STATEMENTS Titles Using global statements gives you even MORE control of certain aspects of the graphs along with more options to further enhance the graph. Let’s take a look at the TITLE statement. title1 c=darkblue h=2.5 f=swissb "SAS/Graph " c=darkred h=3.0 f=swissbi "GPLOT Example"; Can you guess what the C=, H=, and F= are specifying? That’s right! – Color, Height, and Font. The quoted text following the options will take on those specifications. Axis Instead of using the VAXIS option to define the vertical axis, let’s code an AXIS statement (which is much more powerful) and point to it in the plot option. axis1 label=(c=darkorange h=1.5 f=zapfbi j=r "Total Returns") offset=(0.2 in ) order=(0 to 15000 by 5000) value=(c=darkorange f=swissl ); Since you can (and usually do) have more than one axis statement, you need to number them (from 1 – 99) just like a title statement. The LABEL= option defines the options for just the label of the axis. The J= option in the justification of the text – left(L), center(C), right(R). The ORDER= defines the range of values and major tick marks. The VALUE= define the attributes of the labeling of the axis and the tick marks. The OFFSET= defines how far from the lower left corner of the graph to start the first tick mark. SUGI 30 Tutorials