How To Plot A Line Chart With A Standard Error
Hany Aboutaleb, Pfizer Inc., NYC NY.
ABSTRACT
No single statistical tool is as powerful as a graph. Graphs can
display a large amount of information, reveal patterns and
relationships, support hypothesis, and picture new ideas. The
SAS® system and SAS/GRAPH® software provide basic tools
for statistical analysis and graphical display. This article will
describe and illustrate how to plot a line chart with a standard
error and how to produce custom legends using the annotate
data set.
OVERVIEW THE PROGRAM
PROGRAM NAME: GRAPH.SAS
The plot of a line chart with standard error is a great aid in PROGRAMMER: Hany Aboutaleb
interpreting the pattern of group differences, especially when LANGUAGE: PC SAS VERSION 6.11
the factors interact.
FIGURE 1: Running Speed and Sugar
Concentration - Means with Standard Error
METHOD
OPTIONS FORMCHAR='|____|+|---+=|-/\*';
Plots that show mean values of data can display lines for the data that PROC FORMAT;
show the standard error for significant differences among the means. To VALUE COLOR
draw the standard error lines I will use PROC UNIVARIATE to generate other='BLACK';
the mean values and standard errors. I will construct the data so that for VALUE SYMPOL
each value of the independent (X) variable there are three values for the 1,9='=' 2,10='CIRCLE'
dependent (Y) . These three observations are the high(mean+std), close 3,11='SQUARE 4,12='DOT'
(mean), and low(mean-std). In the Symbol statement I will set the option 5='STAR' 6='DIAMOND';
INTERPOL=HILOJT to designate that a solid vertical line connects the VALUE LIN
high and low values. Adding 'j' suppresses the tick mark at the mean 1='1' 2='3' 3='5' 4='14';
value, and 't' adds a top and bottom to the std line. I will then create the VALUE GROUP
annotate data set, using the input data set for the plot as the basis for plot 1='Female Rats ' 2='Male Rats ';
and to add the legend and the symbol to the graph. run;
Create the input data set, Sucrose. Sucrose is the title of a set
KEY STEPS
of experiments in which rats group1(female rats)and
Create graphs using both methods: group2(male rats) were given sugar solution as a reward for
running a runway. The independent variable (X) was the
• Generate the graph using SAS/GRAPH without the sucrose concentration(8,16,32 or 64%) and the response
annotate data set . variable(Y) was the rat's running speed to complete the
runway. The goal is to determine how running speed depends
on gender and sucrose concentration.
DATA SUCROSE;
INPUT GROUP SUGAR @;
DO SUBJECT =1 TO 8;
INPUT SPEED @;
OUTPUT;
END;
CARDS;
1 8 1.4 1.2 2.2 1.4 2.3 3.0 2.0 2.7
1 16 3.2 4.8 5.0 2.5 6.1 4.8 4.6 4.2
1 32 3.2 4.1 6.5 4.0 4.5 4.4 4.5 4.1
1 64 3.8 6.6 6.5 5.9 5.9 3.0 5.9 5.6
2 8 3.4 5.5 6.4 5.7 5.5 5.2 4.3 5.2
2 16 6.3 6.5 6.3 7.0 6.9 8.0 6.2 6.5
2 32 7.3 7.0 6.3 5.5 7.3 8.6 6.7 6.7
2 64 8.9 8.0 8.8 8.9 8.0 8.0 8.1 8.9 ;
• Generate the graph with the annotate data set. Observe PROC UNIVARITE generates the means and the standard
that the use of annotate data set has significantly enhance
error.
the graph . proc sort;
1
by group SUGAR; TEXT=(PUT(COUNT,SYMPOL.));
proc univariate noprint; OUTPUT;
by group SUGAR; X=X+1.5;
var SPEED; FUNCTION='MOVE';
output out=desc n=n mean=mean std=std; OUTPUT;
RUN; FUNCTION='SYMBOL';
TEXT=(PUT(COUNT,SYMPOL.));
Structure the data so that for each value of the independent X, OUTPUT;
X=X-2.25;
(sucrose concentration) there are three values for the FUNCTION='MOVE';
dependent Y, (the speed in runway). OUTPUT;
data des;set desc; FUNCTION='DRAW';
by group SUGAR; LINE=INPUT(PUT(COUNT,LIN.),2.);
inh=mean-STD; OUTPUT;
output; X=X+3;
inh=mean; FUNCTION='DRAW';
output; LINE=INPUT(PUT(COUNT,LIN.),2.);
inh=mean+STD; OUTPUT;
output; X=X+3.5;
run; FUNCTION='MOVE';
OUTPUT;
Set the graphics environment. FUNCTION='LABEL';
goptions reset=global gunit=pcT dev=win SIZE=1.8;
FTEXT=DUPLEX FTITLE=DUPLEX SIMFONT=DUPLEX STYLE='DUPLEX';
nodisplay; COLOR='BLACK';
PROC SORT DATA=des OUT=HANJJ;BY group; TEXT=PUT (group,group.);
OUTPUT;
END;
Create the Annotate data set STDERR. X=80;
DATA STDERR; FUNCTION='MOVE';
OUTPUT;
RUN;
Declare the variables.
LENGTH COLOR FUNCTION $8 TEXT $18; DATA SYMLEG;
RETAIN XSYS YSYS '2' WHEN 'A' SET STDERR LEGEND;
HSYS'3' FUNCTION 'SYMBOL';
SET HANJJ;
BY group; Select the color and interpolation method of the plot line.
SYMBOL1 I=HILOCTJ WIDTH=2.5 VALUE=none L=1
Draw the Symbol for each group with a different color. HEIGHT=5.2 MODE=EXCLUDE C=BLACK;
IF FIRST.group THEN COUNT+1;
SYMBOL2 I=HILOCTJ WIDTH=2.5 VALUE=none L=3
SIZE=5.2;
X=SUGAR;Y=MEAN; HEIGHT=5.2 MODE=EXCLUDE C=BLACK;
TEXT=PUT(COUNT,SYMPOL.);
COLOR=PUT(COUNT,COLOR.); Modify the appearance of the axes.
OUTPUT; AXIS1 VALUE=(H=2) OFFSET=(2,2) LABEL=(H=2.5 A=90
RUN; R=0 "Mean Speed in Runway (ft/sec)")
MAJOR=(H=1 W=3) ORDER=(0 TO 10 BY 2) WIDTH=1.5
MINOR=NONE ORIGIN=(12,15);
Create the Annotate data set LEGEND.
DATA LEGEND; AXIS2 VALUE=(H=2) OFFSET=(3,3) ABEL=(H=2.5
"Sucrose Concentration (%)") MAJOR=(H=1 W=3)
Declare the variables. ORDER=(8 TO 64 BY 8) WIDTH=1.5 MINOR=NONE
LENGTH TEXT $ 16 STYLE FUNCTION $ 8; length=65 ORIGIN=(12,15);
RETAIN FUNCTION 'LABEL' POSITION '3'
WHEN 'A' XSYS '5' YSYS '6' SIZE 2.2 Produce the plot. The PLOT statement includes the annotation
STYLE 'DUPLEX' X 80 Y 26;
SET HANJJ;
defined in the stderr and legend data sets.
BY group; PROC GPLOT DATA=des GOUT=WORK.GSEG;
PLOT INH * SUGAR=group/
HAXIS = AXIS2
Draw the Symbol and write the text in a different color for VAXIS = AXIS1
each group to create a legend. NOLEGEND
IF _N_=1 THEN DO; anno=symleg
TEXT='Legend '; NAME = "HAN29";
OUTPUT; FORMAT group group.;
END; RUN;
STYLE=' ';POSITION='6'; QUIT;
IF FIRST.group THEN DO;
COUNT+1; Set the graphics environment.
COLOR=(PUT(COUNT,COLOR.)); goptions reset=global gunit=pcT dev=win
Y=Y-2.3; FTEXT=DUPLEX FTITLE=DUPLEX SIMFONT=DUPLEX
X=X+.75; TARGET=WINPRTM DISPLAY;
FUNCTION='MOVE';
OUTPUT;
SIZE=2.2;
FUNCTION='SYMBOL'; Add the title and footnotes.
2
TITLE1 " ";
TITLE2 " "; CONCLUSION:
TITLE3 " ";
TITLE6 H=2.5 J=C "FIGURE 1";
TITLE8 H=2.5 J=C "Running Speed AND Sugar A great deal of flexibility can be gained by using the annotate
Concentration - Means with Standard Error"; data set to enhance the graph. It also improves readability and
TITLE9 H=2.5 J=C " "; create more professionally display.
FOOTNOTE1 h=1.8 J=L "_______________________";
FOOTNOTE3 h=1.8 J=L "GRAPH.SAS - July 7 1997";
FOOTNOTE4 " "; TRADEMARKS:
FOOTNOTE5 " ";
SAS® is a registered trademark of the SAS Institute, Inc., in the USA and
Begin the DAT step to produce the graphics. The keyword, in other countries.
_NULL_, prevents a data set from being created, but does not Windows® is a registered trademark of the Microsoft Corporation.
prevent the graph from being generated.
DATA _null_; ACKNOWLEDGMENT:
Select the output catalog. The author would like to thank Prof. Judy Coomes of William
RC=GSET('CATALOG','WORK','GSEG'); Paterson College for her comments in his preparation of this
paper.
Initialize SAS/GRAPH SOFTWARE to accept DSGI
statements. REFERENCES:
RC=GINIT();
RC=GRAPH('CLEAR'); [1] SAS Institute Inc. (1991), SAS® System for Statistical Graphics,
First Edition, Cary, NC: SAS Institute Inc.
Define the viewports [2] SAS Institute Inc. (1990), SAS /GRAPH® Software; Reference, Ver.
RC=GSET('VIEWPORT',1,0,0,1,1); 6, Vols. 1, 2, Cary, NC: SAS Institute Inc.
[3] SAS Institute Inc., SAS® Language Reference: Version 6, First
Define the windows. Edition, Cary, NC: SAS Institute Inc.
RC=GSET('WINDOW',1,0,0,100,100);
CONTACT:
Activate the first transformations.
RC=GSET('TRANSNO',1); Hany Aboutaleb
Pfizer Inc.
Insert the existing graph. 235 East 42nd street
RC=GRAPH('INSERT',"HAN29"); New York, NY 10017-5755
Mail Stop:150-37-30
Display graph and end DSGI. Office: (212) 733-4718
RC=GRAPH('UPDATE'); Fax: (212) 309-4346
RC=GTERM(); Internet:abouth@pfizer.com
RUN;
3