Paper TT08 Let SAS Write Your SAS/GRAPH Programs for You Max Cherny, GlaxoSmithKline, Collegeville, PA ABSTRACT Creating graphics is one of the most challenging tasks for SAS users. SAS/Graph is a very powerful application which allows one to create many kinds of graphics. However with this power comes a certain degree of complexity which makes learning SAS/GRAPH a challenging task. Fortunately, SAS has three tools that easily and interactively create graphics: SAS/ASSIST, Graph-N-Go and SAS Enterprise Guide. These fairly obscure and underutilized tools are part of the SAS software, and generate accompanying SAS programs for you. No knowledge of SAS/GRAPH is required to create powerful and visually appealing graphs. Any may serve as the first step to quickly learn how to program SAS graphs or assist experienced SAS/GRAPH users in fine tuning their graphic skills. These tools vary in their functionalities, availabilities, design, scope and ease of use. The use of these SAS tools will be demonstrated and their strengths and weaknesses will be analyzed and compared. INTRODUCTION Creating graphics is one of the most challenging tasks for SAS users. SAS/Graph is a very powerful application which creates many kinds of graphics. However with this power comes a certain degree of complexity. The real or perceived complexity impedes some from trying to create graphics using SAS. Usually the first step for SAS users when faced with a task of creating a graph would be to consult the program s help facility. While the help facility offers many good examples of SAS/GRAPH programs, and most come with test data, not all types of graphs have examples and sometimes running those programs using a real data is difficult. Fortunately, SAS offers tools to interactively create graphics based on existing real data. Users may then view the code which was used to generate the graphics. There are three fairly obscure and undeservingly rarely used SAS tools that accomplish this: SAS/ASSIST, Graph-N-Go and SAS Enterprise Guide. All three tools offer an easy way to create a graph and save code which may later be re-used. Those SAS users who are inexperienced with creating graphics can experiment with designing graphs while using real data and then view the code used to generate them. Even SAS users who have experience with certain types of graphs may find these tools helpful while starting to design different types of graph. The code may then be modified and incorporated in the production program. Regardless of a user s experience, these tools may serve as good first starting step while designing graphs in SAS. Even people who do not have any knowledge of SAS can design good graphics using one of these three tools. SAS/ASSIST Very few people, both beginners and expert users use SAS/ASSIST. In my opinion, this is a mistake as this tool is very easy to use and can help analyze data and learn more about writing SAS code. You can start SAS/ASSIST in SAS by selecting Solutions->ASSIST. You will see a series of options. Click the one that says Graphics. You will be given a choice of 5 most common types of graphs. The design of a graph is an easy and straightforward process. You will need to choose your data, SAS variables which you want to graph and the options for the graph. Some of the variables are required you have to choose the variables if you want to continue designing a graph. Other variables are optional, for example a stacking variable. Figure 1 below shows common options for designing a bar chart summarizing number of patients at each visit in a clinical trial grouped by treatment and stacked by gender. 1
Figure 1 After you choose your variables and options you will need to submit your graph, just like you would submit any other SAS program. If SAS is unable to create your graph you will be notified and asked to change some of your settings. You will then see the resulting graph. Figure 2 2
The SAS code which created this graph can be viewed by selecting View->View Source. You can view the program and become familiar with SAS code used to generate this graph. If you copy the code you will be able to run it as a stand alone program. The program also contains comments explaining the code used in the graph. /*------------------------------------------------------------------* Summary: Creating a grouped and stacked bar chart using the table SAMPLE.VISITS and charting the columns trtgrp along the vertical axis. Generated: 08JUN2006 10:03:00 *------------------------------------------------------------------*/ /*------------------------------------------------------------------* The GOPTIONS statement allows you to have more control over the final appearance of your output such as fonts, colors, text height and so on. The output device and destination is also specified in the goptions statement. *------------------------------------------------------------------*/ goptions reset=(axis, legend, pattern, symbol, title, footnote) norotate hpos=0 vpos=0 htext= ftext= ctext= target= gaccess= gsfmode= ; goptions device=win ctext=blue graphrc interpol=join; /*------------------------------------------------------------------* PATTERN statements allow you to define colors and patterns in the chart, map or plot that you are creating. SAS/GRAPH uses any pattern statements that you specify. If more are needed, default PATTERN statements are used. *------------------------------------------------------------------*/ pattern1 value=solid; /*------------------------------------------------------------------* AXIS statements allow you to supply information on how your vertical and horizontal axes will appear on the graph. *------------------------------------------------------------------*/ axis1 color=blue width=2.0; axis2 color=blue width=2.0; axis3 color=blue width=2.0; /*------------------------------------------------------------------* This section produces the actual bar chart and contains the options that directly relate to the data and the axis area. *------------------------------------------------------------------*/ proc gchart data=sample.visits; VBAR3D trtgrp /maxis=axis1 raxis=axis2 frame subgroup=gender group=visit type=freq DISCRETE ; run; quit; 3
Changing the existing graph is easy. You simply go back to the design screen and apply new options or choose other variables. The SAS system provides a good help facility for SAS/ASSIST and its graphics sections. Please refer to the online documentation for further help with SAS/ASSIST. GRAPH-N-GO Graph-N-Go is another easy tool which will help you learn SAS/GRAPH. It is a very obscure tool however it should not be it is quite powerful and easy to use and is available to you as part of SAS/GRAPH. It is specifically designed for creation of SAS graphs. You can start the tool by selecting Solutions-> Reporting->Graph-N-Go or by submitting gng or graphngo commands. At the beginning, Graph-N-Go offers an optional quick tutorial. At first, like in SAS/ASSIST you will need to choose your data. This is done by clicking the top left icon representing a dataset. It will start the wizard which will guide you through your libraries and help you choose your dataset. Below the data icon there are icons representing 6 types of your most commonly requested graphs: bar chart, pie chart, plot, overlay chart, histogram and overlay plot. You can click one of them and drag them into the right window. There you can design your graph. Designing the graph is quite similar to SAS/ASSIST, however it is visually more appealing. As you choose your columns the graph is being displayed so you can preview the results before running the code. The generated code is well documented and comes with many comments which explain the role of various SAS/GRAPH functions. Figure 3 While with SAS/ASSIST you can only see the resulting graph after submitting the program, Graph-N-Go allows you to see the results when designing the graph. Graph-N-Go also offers a way to create titles or footnotes. It has many different kind of commonly used SAS graph options. After you design your graph and click OK, the graph appears in the right bottom window of Graph-N-Go. You 4
can modify the graph by clicking the right button of your mouse. There you can maximize, shrink it or move it. You can change the graph by choosing different variables or applying other options. Figure 4 To see the code used to generate the graph, you should click the right mouse button-and select Export Source file. You will then be asked to save the file on your local machine. The file will contain a SAS program which you can rerun again just like you would run any other SAS program. /* Graph-N-Go SAS/Graph Code Generation for ** Viewer: Bar Chart 1 08JUN2006 10:38:57 ** Description: Bar Chart 1 ** Modified: 08JUN2006:10:43:30 ** SAS products required: Base, SAS/Graph (Version 9 or later) ** Code generated on: 08JUN2006 10:49:18 ** There may be differences in appearance of the graph ** generated by the code below and the Graph-N-Go viewer. ** To make code modifications consult the documentation ** for these statements: ODS, GCHART, GPLOT, ** GOPTIONS, AXIS, LEGEND, SYMBOL, TITLE, FOOTNOTE. ** ** To route output to a graphics device other than your monitor, ** modify the source code below to change the device driver by ** 1) removing the asterisk preceding GOPTIONS DEVICE=JAVA; ** 2) changing JAVA to some other valid device. ** ** To create an HTML file, modify the source code below ** to enable ODS output by ** 1) removing the asterisks from the ODS statements and ** the asterisk preceding GOPTIONS DEVICE=JAVA; ** 2) setting the value of DEVICE= to GIF, JAVAIMG, ACTXIMG, ** JAVA, or ACTIVEX (JAVA and ACTIVEX create interactive graphs) ** 3) verifying or changing the ODS FILE= option so it names the ** HTML file for output. ** 4) verifying or changing the ODS GPATH= option so it names the 5
** path for the GIF or JPG file displayed by the HTML page. This ** option is not necessary if the DEVICE is ACTIVEX or JAVA. */ /* Begin ODS output */ * ods listing close; * ods html file="output-html-file-specification" (title="graph-n-go Output") gpath="output-image-path-specification" gtitle gfootnote style=minimal; /* Set the SAS/Graph options */ goptions reset=all ctext=cx000000 ftext="ms Sans Serif" htext=8 pt colors=(cx0000ff CXFF0000 CX008080 CX00FF00 CXFF00FF CXFFFF00 CX00FFFF CX800000 CX008000 CX800080 CX000080 CX808000 CXFFFFFF CX808080 CXC0C0C0 CX000000); /* Set the Titles/Footnotes */ title1 justify=center color=cx000000 font="ms Sans Serif" height=8 pt "Treatment grouped by Visit"; /* Set the SAS/Graph device driver */ * goptions device=java xpixels=766 ypixels=471; /* AXIS1 describes axis for Category variable TRTGRP */ /* AXIS2 describes axis for Response statistic FREQ */ axis1 minor=none noplane label=("treatment") ; axis2 minor=none noplane label=("(frequency)") order=(0 to 16 by 2) ; /* LEGEND1 specifies the legend for Subgroup GENDER */ legend1 frame shape=bar(1,1)cells cborder=cx000000 label=(justify=center position=(top center) "Gender") position=(bottom center); proc gchart data=sample.visits; vbar3d TRTGRP / type=freq shape=block maxis=axis1 noframe woutline=1 coutline=cx000000 caxis=cx000000raxis=axis2 legend=legend1 group=visit G100 subgroup=gender; run; quit; /* Reset all graphics options */ goptions reset=all; /* End ODS output */ * ods html close; * ods listing; Graph-N-Go comes with a good help facility. Many of its functions are explained and well documented by SAS. Please refer to the online documentation for further help with Graph-N-Go. SAS ENTERPRISE GUIDE While SAS/ASSIST and Graph-N-Go exist in earlier versions of SAS, SAS Enterprise Guide is a new tool and is available only in SAS version 9.1. It is not a part of interactive SAS, however it is totally integrated with it. It is much more powerful then either SAS/ASSIST or Graph-N-Go and resembles graphics features of Excel or Splus. 6
Figure 5 After starting SAS Enterprise Guide, usually by Start->All programs->sas-> SAS Enterprise Guide (this may vary depending on your machine s configuration) you will see a task list on your right. This list contains a graph subsection where you can choose to create different kind of graphs: area plot, bar chart, box plot, bubble plot, contour plot and others. By clicking one of the graph types you will be asked to select your data. A wizard will help you find your data. Figure 6 7
Just like in SAS/ASSIST and Graph-N-Go, the SAS Enterprise Guide not only guides through a creation of your graph, it also saves the code used to generate graphics. You can preview the code while you are still designing the graph by clicking Preview Code option in the wizard. After the graph is created you can go to File->Export All Code to see the SAS program. This program can be then re-run and modified. Please refer to the online documentation for further help with SAS Enterprise Guide. /* ---------------------------------------- Code exported from SAS Enterprise Guide DATE: Thursday, June 08, 2006 TIME: 11:19:09 AM PROJECT: Project PROJECT PATH: ---------------------------------------- */ ODS PROCTITLE; OPTIONS DEV=ACTIVEX; FILENAME EGHTMLX TEMP; ODS HTML(ID=EGHTMLX) FILE=EGHTMLX STYLESHEET=(URL="file:///C:/Program%20Files/SAS/Shared%20Files/BIClientStyles/ AMODefault.css") ATTRIBUTES=("CODEBASE"="http://www2.sas.com/codebase/graph/v91/sasgraph.exe") NOGFOOTNOTE GPATH=&sasworklocation NOGTITLE STYLE=AMODefault ENCODING='utf-8'; ODS LISTING; /* START OF NODE: Bar Chart */ LIBNAME ECLIB000 "C:\Documents and Settings\chernm01\My Documents\nonproject\SAS ASSIST, Graph-N-Go and EG presentation\datases"; %LET _CLIENTTASKLABEL=%NRBQUOTE(Bar Chart); %LET _EGTASKLABEL=%NRBQUOTE(Bar Chart); LIBNAME ECLIB000 "C:\Documents and Settings\chernm01\My Documents\nonproject\SAS ASSIST, Graph-N-Go and EG presentation\datasets"; %macro _SASTASK_DROPDS(dsname); %IF %SYSFUNC(EXIST(&dsname)) %THEN %DO; DROP TABLE &dsname; %END; %IF %SYSFUNC(EXIST(&dsname,VIEW)) %THEN %DO; DROP VIEW &dsname; %END; %mend _SASTASK_DROPDS; %LET _EGCHARTWIDTH=0; %LET _EGCHARTHEIGHT=0; /* ------------------------------------------------------------------- Code generated by SAS Task Generated on: Thursday, June 08, 2006 at 11:08:11 AM By task: Bar Chart Input Data: ECLIB000.VISITS Server: Local ------------------------------------------------------------------- */ PROC SQL; %_SASTASK_DROPDS(WORK.SORT7076); QUIT; /* ------------------------------------------------------------------- Data set ECLIB000.VISITS does not need to be sorted. ------------------------------------------------------------------- */ PROC SQL; CREATE VIEW WORK.SORT7076 8
AS SELECT trtgrp FROM ECLIB000.VISITS; QUIT; GOPTIONS xpixels=&_egchartwidth ypixels=&_egchartheight; Axis1 STYLE=1 WIDTH=1 MINOR=NONE LABEL=(FONT='Microsoft Sans Serif' HEIGHT=12pt JUSTIFY=Right) ; Axis2 STYLE=1 WIDTH=1 LABEL=(FONT='Microsoft Sans Serif' HEIGHT=12pt JUSTIFY=Center) ; TITLE; TITLE1 "Bar Chart"; FOOTNOTE; FOOTNOTE1 "Generated by the SAS System (&_SASSERVERNAME, &SYSSCPL) on %SYSFUNC(DATE(), EURDFDE9.) at %SYSFUNC(TIME(), TIMEAMPM8.)"; PROC GCHART DATA=WORK.SORT7076; VBAR3D trtgrp / SHAPE=BLOCK FRAME NOLEGEND COUTLINE=BLACK RAXIS=AXIS1 MAXIS=AXIS2 LREF=1 CREF=BLACK AUTOREF ; /* ------------------------------------------------------------------- End of task code. ------------------------------------------------------------------- */ RUN; QUIT; PROC SQL; %_SASTASK_DROPDS(WORK.SORT7076); QUIT; TITLE; FOOTNOTE; GOPTIONS RESET=ALL;RUN; %LET _CLIENTTASKLABEL=; %LET _EGTASKLABEL=; ODS _ALL_ CLOSE; RUN; COMPARISON OF THESE TOOLS SAS/ASSIST is the most basic tool among the three. SAS/ASSIST does not allow creation of many complex types of graphics but has the ability to quickly produce simple, basic, commonly requested graphs and can serve as a good starting point for graph creation. It has enough options for you to become familiar with the most frequently used SAS/GRAPH graphical options by analyzing the code created by SAS/ASSIST. The main disadvantages of SAS/ASSIST is the lack of preview during the design mode -you must click the submit button in order to view the graph or SAS code. SAS/ASSIST s graphical user interface is not very appealing and sometimes hard to use. However, this tool is available in earlier versions of SAS and can be used by users who do not have SAS 9 installed on their machines. Graph-N-Go is a more robust tool than SAS/ASSIST. It allows for creation of more frequently requested graphs and saving code. Furthermore, it has a more appealing interface and the ability to view the graph while in design mode. 9
It has enough features to create relatively complex graphs. It seems to be specifically designed for people trying to learn SAS/GRAPH. In fact, programs generated by Graph-N-Go s offer better documentation then SAS/ASSIST or SAS Enterprise Guide. However it maybe a bit complex for a novice SAS user who may find the simplicity of SAS/ASSIST more appealing during the initial experimentation with SAS graphics. SAS Enterprise Guide is clearly the most powerful tool for the creation of graphics. It has an excellent wizard which allows you to design any kind of graphs interactively and then easily save the resulting code. It is very intuitive, however the resulting code is not very well documented. SAS Enterprise Guide may be appealing to novice SAS/GRAPH users as well as experts. Many people find it easier to design a graph first and then review the code. SAS Enterprise Guide s main disadvantage is the fact that it s only available with version SAS 9.1 and that version of SAS may not be available to all of SAS users yet. Some people with little graphical experience may find the guide a bit complex. In which case, it may be easier to start experimenting with SAS/ASSIST or Graph-N-Go first and then moving on to SAS Enterprise Guide. SAS Enterprise Guide is a relatively new application and bugs may be encountered while using it. CONCLUSION Any of the three tools allows SAS users to experiment with creating complex graphics and then viewing the code automatically generated by SAS. This approach is an excellent way to quickly create graphics and learn SAS/GRAPH REFERENCES SAS Institute Inc. (2005), SAS ONLineDoc, Version 9.1.3, Cary, NC, USA: SAS Institute Inc. ACKNOWLEDGMENTS Author would like to thank Terek Peterson and Rober Gordon for introducing him to SAS Enterprise Guide and Patricia Coyle and Greg Cicconetti, Ph.D. for their help with this paper. RECOMMENDED READING Peterson, T and Gordon, M. (2006), Creating ActiveX Graphs for Presentations using SAS Enterprise Guide And Drill-Down Graphs Too, Proceedings of the PharmaSug 2006 CONTACT INFORMATION Your comments and questions are valued and encouraged. Contact the author at: Max Cherny GlaxoSmithKline Email: chernym@yahoo.com SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. indicates USA registration. Other brand and product names are trademarks of their respective companies. 10