Week 12
Producing Graphics
Unit 5 SAS for Data Description
Week 12: Producing Graphics and Using SAS Analyst
Welcome! In this reading, the procedures discussed are CHART and PLOT. Also provided is a brief introduction to using the SAS ANALYST for producing graphics with the SASGRAPH module. The latter are higher quality graphics than the printer character charts and plots produced with PROCs CHART and PLOT.
Thus, PROCs CHART and PLOT do not provide high quality graphics, but these procedures can be used effectively in preliminary and summary reports, and to explore graphically, relationships between variables.
TIP - For high quality graphics, consider using SAS to export the information to another platform for producing graphs. SAS GRAPH can be used with SAS data sets, or data can be exported into spreadsheet packages, such as EXCEL, or other statistical packages such as STATA.
Note - Learning to use the simpler plotting procedures in SAS will also help you to learn to edit programs produced by the SAS SOLUTIONS graphics module.
Week 12
Page 1 of 13
Week 12
Producing Graphics
Goals of Week 12: Producing Graphics and Using SAS ANALYST 1. to be competent in using PROC CHART and PROC PLOT; 2. to understand that it is possible to export SAS data for producing high quality graphics using other software (such as EXCEL or STATA) ; 3. to be competent in the use of SAS ANALYST; and 4. to understand that it is possible to export a SAS graph directly into another document, such as an MS WORD document;
Week 12 Outline – Producing Graphics Using SAS ANALYST Section Topic
Page
1.
Simple Graphics Using PROC CHART …………….…………………………….
3
2.
Simple Graphics Using PROC PLOT …………………………………………….
6
3.
Producing Graphics Using SAS ANALYST ……………………………………….
Week 12
10
Page 2 of 13
Week 12
Producing Graphics
1. Simple Graphics using PROC CHART
PROC CHART is most appropriate for summarizing discrete data. • It can be used to produce bar charts, histograms, block, pie, and star charts. • Many options are available to control grouping, sub-grouping, cumulating values, controlling the axis scale and printed symbols. • By default, vertical charts are produced; however, horizontal charts can be requested. • TIP – Requesting horizontal charts will also get you frequencies and percents, neither of which accompany vertical charts. • See the procedures manual.
PROC CHART can also be used to summarize continuous data, in selected settings. • Provided the number of values is small, PROC CHART can be used to produce charts for continuous scale numeric data. • You can control the number of groupings, and the midpoints for the groupings; or these can be left to a default algorithm. • Note - PROC CHART, even when you request a histogram for grouped continuous data, a bar graph is produced.
Week 12
Page 3 of 13
Week 12
Producing Graphics
It is possible to specify how much space you wish the chart to occupy (full page, half page, etc.) by using the PAGESIZE and LINESIZE options. • FULL PAGE – This is the default. • HALF PAGE - To get a half page chart size, use PAGESIZE option in the OPTIONS statement and set it to size 30. • LARGER THAN FULL PAGE – To produce a chart that is two pages long, specify PAGESIZE 120.
Example This example is in two parts. The data used is from the cardiopulmonary bypass study, on the sex of the subject, and temperature of bypass. • Part 1 - Illustrates the use of grouping • Part 2 - Illustrates the use of sub-grouping with the vertical bar chart. • Note that in this program the PAGESIZE is set at 30 on the OPTIONS statement so as to produce a chart that fits in half a page.
* Example –part 1: reset options to print ½ page chart *; OPTIONS PAGESIZE=30 LINESIZE=110 NOCENTER; ** program to illustrate chart **; ** vertical bar, grouped **; PROC CHART DATA=CHART1; VBAR WCTYPE / GROUP=SEX; TITLE1 'BYPASS TYPE, GROUPING ON SEX'; RUN; ** Example part 2: vertical bar - subgrouped **; PROC CHART DATA=D.CHART1; VBAR WCTYPE / SUBGROUP=SEX; TITLE1 'BYPASS TYPE, SUBGROUPED BY SEX'; RUN;
Week 12
Page 4 of 13
Week 12
Producing Graphics
BYPASS TYPE, GROUPING ON SEX FREQUENCY OF WCTYPE GROUPED BY SEX FREQUENCY | ***** 15 + ***** | ***** | ***** | ***** | ***** ***** 10 + ***** ***** | ***** ***** | ***** ***** | ***** ***** | ***** ***** 5 + ***** ***** | ***** ***** ***** | ***** ***** ***** | ***** ***** ***** | ***** ***** ***** ***** -------------------------------------------------------------------WCTYPE COLD WARM COLD WARM |------ F ------| |------ M ------| SEX BYPASS TYPE, SUBGROUPED BY SEX FREQUENCY OF WCTYPE FREQUENCY 20 + MMMMM | MMMMM 16 + MMMMM | MMMMM MMMMM 12 + MMMMM MMMMM | MMMMM MMMMM 8 + MMMMM MMMMM | MMMMM MMMMM 4 + MMMMM FFFFF | FFFFF FFFFF -------------------------------COLD WARM TYPE OF BYPASS SYMBOL SEX SYMBOL SEX F F M M
Week 12
Page 5 of 13
Week 12
Producing Graphics
2. Simple Graphics using PROC PLOT
A QUIT statement is required at the end of a PROC PLOT.
PROC PLOT is considered an interactive program, so that you can add more plots without restating all of your options. Many other options are available for controlling the plots. Read the procedures manual.
PROC PLOT produces printer scatterplots of one variable against another. • Many options are available to control the plot size, scale, reference lines and symbols printed. • To define a scatterplot a PLOT statement is used. For example, to plot the variable Y against X PLOT Y * X;
• FIRST named variable defines the VERTICAL axis. • SECOND name variable defines the HORIZONTAL axis
To specify how the graphs fit on a page, use the HPERCENT and VPERCENT options. •
For example, the options of 50 mean that 50% of the linesize, and 50% of the pagesize are allocated for each graph requested.
Week 12
Page 6 of 13
Week 12
Producing Graphics
Example In this example, data on operative time from the cardiopulmonary bypass study is plotted. •
CPBTIME = duration of surgery time, and
•
XCLAMP = duration of the cross-clamp time, the actual amount of time the heart is stopped.
•
CPBTIME and XCLAMP should be closely associated, as appears on the graphs below.
•
The NOMISS option is used so that when data is missing for one variable, the observation is not used in determining the scale of the plot for the other variable. In this data, one extremely high value of XCLAMP that was much larger than the total bypass time was changed to missing – we didn’t want the value of the bypass time for this observation to influence the scale of the plot.
•
The HPERCENT and VPERCENT options have been set to 50. This means that 50% of the linesize, and 50% of the pagesize are allocated for each graph requested.
•
This example produces four graphs. 1st Graph – Illustrates the default options. The letter A is printed for one observation, B for two in one place, and so on. 2nd and 3rd Graphs - The next two graphs request that the value of a third variable be used in the plot. This is useful in determining if there is any obvious difference in operative time for warm or cold bypass, or for males and females. This does not appear to be the case here. 4th Graph - The final graph illustrates the use of choosing a plotting symbol, in this case an asterisk (*).
•
Week 12
Note the inclusion of the QUIT; statement in required at the end of PROC PLOT.
Page 7 of 13
Week 12
Producing Graphics
OPTIONS PAGESIZE=60 LINESIZE=110 NOCENTER; ** program to illustrate plot **; PROC PLOT DATA=CHART1 NOMISS HPERCENT=50 VPERCENT=50; PLOT CPBTIME * XCLAMP ; * default PLOT CPBTIME * XCLAMP = SEX; * value of sex plotted PLOT CPBTIME * XCLAMP = WCTYPE; * value of temp plotted PLOT CPBTIME * XCLAMP = '*'; * symbol ‘*’ plotted TITLE1 'BYPASS TIME BY CROSS-CLAMP TIME'; RUN; QUIT; * end to interactive procedure ;
Week 12
*; *; *; *;
Page 8 of 13
Week 12
Producing Graphics
BYPASS TIME BY CROSS-CLAMP TIME A=1, B=2, etc.
C P B T I M E I N M I N
Plot of CPBTIME*XCLAMP.
| 300 + | | | | A | AA 200 + A | A A | A A | A A A | A A A A A | A AA A A 100 + B B A | AA | A A | | | 0 + -+----------+----------+----------+----------+ 25 50 75 100 125
Plot of CPBTIME*XCLAMP.
C P B T I M E I N M I N
XCLAMP
C P B T I M E I N M I N
NOTE: 2 obs had missing values.
Symbol is value of WCTYPE.
| 300 + | | | | W | CW 200 + W | W W | C C | W W W | C W W W W | W CW C W 100 + C W C | WC | C W | | | 0 + -+----------+----------+----------+----------+ 25 50 75 100 125 XCLAMP
NOTE: 2 obs had missing values.
Week 12
| 300 + | | | | M | MM 200 + M | M M | M M | M F M | M M F M M | M FF M M 100 + M M M | MM | M M | | | 0 + -+----------+----------+----------+----------+ 25 50 75 100 125 XCLAMP
NOTE: 2 obs had missing values.
Plot of CPBTIME*XCLAMP.
Symbol is value of SEX.
2 obs hidden.
Plot of CPBTIME*XCLAMP.
C P B T I M E I N M I N
2 obs hidden.
Symbol used is '*'.
| 300 + | | | | * | ** 200 + * | * * | * * | * * * | * * * * * | * ** * * 100 + * * * | ** | * * | | | 0 + -+----------+----------+----------+----------+ 25 50 75 100 125 XCLAMP NOTE: 2 obs had missing values.
2 obs hidden.
Page 9 of 13
Week 12
3.
Producing Graphics
Producing Graphics Using SAS ANALYST
SAS ANALYST can be used to produce high quality graphics. •
This is accomplished through the use (in SAS ANALYST) of a SAS graphics module called SASGRAPH.
•
Note - It is also possible to use SASGRAPH without using SAS ANALYST. Here, however, the introduction is to SAS ANALYST.
•
SAS ANALYST has the advantage of point and click capabilities. This is a great advantage over older versions of SASGRAPH which required the user to be competent in coding selected instructions that are now replaced by point and click.
•
SAS ANALYST also permits on-screen editing.
•
TIP - SAVE the programming statements! When you use SAS ANALYST, SAS produces for you the programming statements that created the graph. This is nice because you can save the program. The saved program can then be edited, revised, re-run, whatever!
•
SAS ANALYST also allows you to directly edit a graph. Specifically, you can add new text, objects and lines, etc., and you can do this directly with what is seen on the screen.
•
The graph you produce may be exported as an image file and can be edited in another graphics package. (For example, export the graph as a .bmp file, and then edit it in Paint).
Week 12
Page 10 of 13
Week 12
Producing Graphics
Introduction to Using the SAS Analyst 1. After opening SAS, from the Menu choose
Solutions > Analysis > Analyst The Analyst Window will appear:
2. Open a SAS data set to work with:
File > Open
This will allow you to browse for .sas7bdat files. These can be previously saved SAS data files, or a file in your WORK directory, for the current SAS session. 3. Once you have selected a data file to work with, select Graphs from the menu, and proceed with
the selected graph type.
Week 12
Page 11 of 13
Week 12
Producing Graphics
TIPS - Some features to note in working with the ANALYST: •
The settings you select will be held, should you choose to change some features of your graph, and rerun, or re-create the graph.
•
To edit and rerun the SAS code with additional features not available in the analyst graph menu: o
Click to open the CODE in the Analyst Results Window
o
Select Edit > Copy to Program Editor
o
You may choose to copy and paste the code to an Enhanced Editor Window (which uses the color coding features)
•
•
Week 12
Features to consider adding/editing: o
Open format file, and attach formats
o
Delete missing or out of range data
o
Change colors, font sizes
o
Edit/add titles, footnotes, labels
When a graph is in the active window access the interactive edit features from the toolbar: o
Change colors, font sizes
o
Edit/add titles, footnotes, labels
o
Mark other features on graph
Page 12 of 13
Week 12
•
Producing Graphics
To save a graph: o
When the graph window is active, select File > Export as Image
o
Choose .gif or .jpeg as the file type if you want to use the file on a web page
o
Choose .bmp if you wish to edit the graph in PAINT
o
Other file types are available -- check compatibility with other software you wish to use, such as Adobe photoshop
A saved image file (any of the types mentioned above) can be incorporated directly into a Word document or a web page.
Week 12
Page 13 of 13