现在的位置: 首页 > 综合 > 正文

跟着学作图——SAS/GRAPH® for the Timid

2018年10月23日 ⁄ 综合 ⁄ 共 7368字 ⁄ 字号 评论关闭

本文摘自:《SAS/GRAPH® for the Timid》——Earl Westerlund, University of Rochester, Rochester, NY

ABSTRACT
SAS/GRAPH is one of the foundation products of SAS®, and an invaluable tool for anybody who needs to see the
information in the data. Its procedures work the same way as any other procedure. But the number of
procedures, statements and options available are so numerous – and, in many cases, cryptic – that many people
become intimidated.
The purpose of this paper is to provide a gentle introduction to SAS/GRAPH, with numerous examples to show
how the fundamentals work. It is based on the following premises:
• If you can create a crosstab in PROC FREQ, you can create a plot in PROC GPLOT.
• If you can set a title or footnote, you can change the appearance of a graph line.
• If you can change the linesize of a listing, you can change the color of the title text.
This paper is intended for those who have not used SAS/GRAPH, or those who have tried and given up in
frustration.

 

....................

We are going to stick to the basics:
• The GPLOT and GCHART procedures
• A few enhancements to the TITLE and FOOTNOTE statements
• A few new statements

• A few new options
We are not going to discuss all the various options and permutations of these statements, but only those used in
these simple examples. Not only will sticking to these few components make your life simpler, they will often
result in better graphs.

 

THE GPLOT PROCEDURE
For the GPLOT procedure examples, we will use the yearly high and low values of the Dow Jones Industrial Average
from 1955 through 1995:

year  hdate           high       ldate          low
1955 30DEC1955 488.40 17JAN1955 388.20
1956 06APR1956 521.05 23JAN1956 462.35
1957 12JUL1957 520.77 22OCT1957 419.79
1958 31DEC1958 583.65 25FEB1958 436.89
.
.
STARTING SIMPLY
The simplest form of the procedure is to specify the data set, and the Y vs. X pairs to plot. The Y value comes
first in the pair:
proc gplot data=SASdataset;
plot high*year;
run; quit;

 

(The reason for including both the RUN and the QUIT statements is that GPLOT is an interactive procedure.
Changes can be made to the graph without restarting the procedure.)

ADDING A TITLE
You will probably want to add a title. We do this the same way any other SAS output gets titles: with a TITLE
statement.
title 'Dow Jones Yearly Highs';
proc gplot data=SASdataset;
plot y*x;
run; quit;

 

ADDING A BIT OF STYLE
The plots we have generated so far use simple fonts. SAS provides a number of fonts to make your output a bit
more readable. Colors are also available, as we show in our enhancement to the TITLE statement.
title c=blue f=swissb 'Dow Jones Yearly Highs';
proc gplot data=stocks;

plot high*year;
run; quit;

The “c=” designates the color; “f=” designates the font to use. All text following the specifications is rendered in
that style.

 

CONNECTING THE DOTS
Sometimes, it’s preferable to connecting the points in a graph with a line. The style of the line is modified in
SAS/GRAPH with the use of the SYMBOL statement.
title c=blue f=swissb 'Dow Jones Yearly Highs';
symbol i=join;
proc gplot data=stocks;
plot high*year;
run; quit;

The “i=join” refers to the “interpolation” option, telling SAS how join the points. The default, as we have seen, is
“i=none”. We want to simply connect the dots.

 

RESTORING THE DOTS AND ADDING COLOR TO THE LINE
We’ve connected the dots, but now the points themselves are missing. If we want to restore them, we can use
other options of the SYMBOL statement. We can also make the line a different color:
title c=blue f=swissb 'Dow Jones Yearly Highs';
symbol i=join v=star c=red;
proc gplot data=stocks;
plot high*year;
run; quit;

The “v=star” refers to the “value” option, telling SAS how display a star symbol at each points. The “c=red” tells
SAS to make the line red, similarly to how we made the title blue.

 

LABELING THE AXES
Unless changed, the descriptions of the X and Y axes are simply the names of the variables. Often, a fuller
description is better. AXIS statements are used for this:
title c=blue f=swissb 'Dow Jones Yearly Highs';
symbol i=join v=star c=red;
axis1 label=('High Value');
axis2 label=('Year');
proc gplot data=stocks;
plot high*year / vaxis=axis1 haxis=axis2;
run; quit;

We made quite a few changes here.
• First, the AXIS statements define how the axis looks. The number following the word AXIS allows us to
make multiple axis definitions. There is no significance beyond identification.
• Next, the LABEL= option defines the label that will be displayed on the axis. Note that the parentheses
are required.
• Finally, we modified the PLOT statement to add options. The vaxis= (vertical, or Y axis) and haxis=
(horizontal, or X axis) options are how the two axes we defined get matched up.

 

CHANGING THE SCALE OF THE PLOT
This graph has a lot of white space between the left and right, and the increments are farther apart than we might
like. If we don’t want to use the default scaling that SAS has defined, we can specify our own scaling in the AXIS
statements.
title c=blue f=swissb 'Dow Jones Yearly Highs';
symbol i=join v=star c=red;
axis1 order=(0 to 6000 by 500) label=('High Value');
axis2 order=(1955 to 1995 by 5) label=('Year');
proc gplot data=stocks;
plot high*year / vaxis=axis1 haxis=axis2;
run; quit;

The ORDER= option specifies the minimum and maximum values, and the increment (much like the specification
of a DO loop).

 

ADDING COLOR AND FONT TO THE AXES
Just as we can change the color and font of the title, we can change the color and font of the axes: We do this
with the GOPTIONS statement. This statement performs a similar function to the OPTIONS statement, but
controls various graphics options.
title c=blue f=swissb 'Dow Jones Yearly Highs';
symbol i=join v=star c=red;
axis1 order=(0 to 6000 by 500) label=('High');
axis2 order=(1955 to 1995 by 5) label=('Year');
goption ftext=zapfb ctext=green;
proc gplot data=stocks;
plot high*year / vaxis=axis1 haxis=axis2;
run; quit;

The FTEXT= and CTEXT= options control the font and color of any text in the plot that is not otherwise specified

 

GRAPHING TWO LINES
Our data contains both high and low values for each year. We can plot both values on the same graph by using
another SYMBOL statement and another PLOT statement option.
title c=blue f=swissb 'Dow Jones Yearly Highs';
symbol1 i=join v=star c=red;
symbol2 i=join c=blue v=star;
axis1 order=(0 to 6000 by 500) label=('High');
axis2 order=(1955 to 1995 by 5) label=('Year');
goption ftext=zapfb ctext=green;
proc gplot data=stocks;
plot high*year=1 low*year=2 / overlay vaxis=axis1 haxis=axis2;
run; quit;

We have added a second SYMBOL statement, calling it SYMBOL2 (and adding a “1” to the first SYMBOL, to
distinguish them). Then we added a second plot specification to the PLOT statement, and used “=1” and “=2” to
tell SAS which symbol to apply to which X-Y pair. We also had to add the OVERLAY option, because the
procedure‘s default behavior is to generate separate plots.

 

THE GCHART PROCEDURE
For the GCHART procedure examples, we will use sales data for a fictitious company, from three departments in
three cities for four calendar quarters:

 Dept   Site       Qtr Sales
Parts    Sydney  1 4043.97
Parts    Atlanta  1 6225.26
Parts    Paris     1  3543.97
Repairs Sydney 1 5592.82
Repairs Atlanta 1 9210.21
Repairs Paris    1 8591.98
Tools    Sydney 1 1775.74
Tools Atlanta 1 2424.19
Tools Paris 1 5914.25
.
.

STARTING SIMPLY
We will cover GCHART in much less detail. We will start with a simple vertical bar chart of total sales at each
site.
proc gchart data=SASdataset;
vbar site / sumvar=sales;
run; quit;

 

ADDING SUBGROUPS
If we want to know which departments contributed to each site’s sales, we can use a subgroup.
proc gchart data=SASdataset;
vbar site / sumvar=sales subgroup=dept;
run; quit;

SHADES OF GRAY
If you need to make black and white copies, the above will not display well. You can use the PATTERN
statement to change colors.
pattern1 c=grayee;
pattern2 c=grayaa;
pattern3 c=gray33;
proc gchart data=SASdataset;
vbar site / sumvar=sales subgroup=dept;
run; quit;

Each pattern specification is assigned to a subgroup. The “graynn” colors are a way to specify shades of gray.
The last two characters are actually hexadecimal digits from ‘00’X to ‘FF’X (0 to 255 decimal) representing the
degree of darkness. A higher number indicates a lighter shade. “GRAYFF” is white, and “GRAY00” is black

...................

.....................

CONCLUSIONS
We have covered a small fraction of the features, options, and procedures available in SAS/GRAPH. But with
just this small fraction, we have managed to produce some informative graphs and charts. I hope I have helped
you become less timid about using SAS/GRAPH, and have become emboldened to explore it further.

 

..............

..............

 

抱歉!评论已关闭.