Anda di halaman 1dari 21

1

Plotting With SAS

• Proc Gplot generates plots that show the relationship between


two or three variables.
• Plot Types:
- Scatter Plot
- Line Plot
1. Regression
2. Spline
3. Smoothing Line
- 2 different Y axis – show the relationship
between X and two Y variables at the same
time
- Cumulative Area Plot & Box
Note:- SAS graphics requires extensive coding

Stat 598 Samiran Ghosh


2

• PROC GPLOT – creates a scatter plot by default.


• Plot statement creates plots relating the values of two
variables.
• You can use more than one plot statement in one PROC
GPLOT step.
• The Y axis variable is named left of the asterisk in the plot
statement and the X axis variable is to the right of the
asterisk.
Proc Gplot data = example;
plot Y*X;
Run;
Quit;

Stat 598 Samiran Ghosh


3

• Customizing Plot
- SYMBOL - statement controls appearance of plot.
- Options:
Height = Controls the size of the point of
plot symbol.
Value = Names the plot point character. These also
include any printable character such as “H”.
(Attached are a list of some of the special symbols)
Color = Defines the symbol color
CO = Defines the color for CI lines and area outlines
CV = Defines color for plot symbols
CI = Defines color for plot lines
Multiple symbol statements are used when more than one plot is
being displayed in a set of axes.

Stat 598 Samiran Ghosh


4

proc import
dbms=CSV
datafile="G:\stat598\count.csv"
out=combined1 replace;
getnames=yes;
run;

Symbol1 value = star height = 2 color = red;


Title "Plot of Series a vs, Series b";

Proc GPLOT data = combined1;


plot a*b;
Run;
Quit

Stat 598 Samiran Ghosh


5

Line plot

• To create a line plot use the symbol statement with the


INTERPOL = option or I= option to connect the points in
the plot.
• OPTIONs:
Join – Connects the points with a line.
Spline – Connects the points with a spline algorithm.
Smooth Line (SM)– Specifics that a smooth line is
fitted to the data using a spline routine.
NOTE: Plot points are connected in the order they appear in
the data set. Make sure that your horizontal variable is
sorted before plotting.

Stat 598 Samiran Ghosh


6

R - Specifies that a plot is a regression analysis.


Types of Regressions:
1. L – Request linear regression.
2. Q – Request a quadratic regression
3. C – Request a Cubic Regression
Notes: When least-square solutions for the parameters are not
unique, the SAS/Graph server defaults to a quadratic equation for
interpolation.
Confidence Limits – To display specify one of the
following:
1. CLM –Displays confidence limits for mean
predicted values.
2. CLI - Displays confidence limits for individual
predicted values
Specify confidence levels from 50% to 99%.
Example: INTERPOL = RLCLM95 (linear regression analysis with 95% limits)

Stat 598 Samiran Ghosh


7

proc import
dbms=EXCEL2000
datafile="G:\stat598\srs.xls"
out=ABC replace;
getnames=yes;
run;

Symbol1 value = triangle interpol = rlclm95 co = red color = black;

Title "Mean Predictive Values and 95% CI";


Proc gplot data = ABC;
plot sem*std;
run;
Quit;

Stat 598 Samiran Ghosh


8

More on Line Plot

proc import
dbms=CSV
datafile="G:\stat598\airpollution.csv"
out=pollution replace;
getnames=yes;
run;

data pollution;
set pollution;
sample=_N_;
run;

Symbol1 i=join v=diamond h=0.3 l=1 c=black;


Symbol2 i=join v=# h=0.3 l=2 c=red;

Proc gplot data = pollution;


Plot rainfall*sample mortality*sample/overlay;
Run;

Note:- The axis are default here (can you point what is not quite correct here). We will see more on axis control
latter. If you don’t specify symbols, defaults symbols will be used. If you want you can easily add a title and
footnote.

Stat 598 Samiran Ghosh


9

title1 color=green font=triplex 'Sample vs. Rainfall and Mortality plot';


footnote1 justify=right '60 yeras data';

Proc gplot data = pollution;


Plot rainfall*sample mortality*sample/overlay;
Run;

If you do not use overlay option you will get multiple plots.

Stat 598 Samiran Ghosh


10

Axis Control
If it does not bother you to write many lines of code, SAS gives you enough liberty to do what ever you want with
your plot.

AXIS Statement defines setting controls for axis color, the color and size
of tick marks, the order and value of the major tick marks and the text
attributes of the axis label and major tick marks.
AXIS Statements are global.
AXIS statements are independent of the input data.
Use PLOT Statement options such as VMINOR=, XTICKNUM=, and
NOAXES to control plot axes from within GPLOT.
• AXIS Statement –Options
1. Label= (Height = (Size of Print)
Color = (Color of Print)
Font = (Font of Print)
“Label”)
2. Order = (Define a series of major tick:
example: (0 to 5 by 10))
3. Minor = (this options turns off minor tick marks)

Stat 598 Samiran Ghosh


11

NOTE: SAS has many ways of programming to accomplish the same graph. I will show you very basic stuff to
get started.

axis1 order=(0 to 1200 by 100) offset=(2) color=blue label=(height=1.2


f=centb a=90 'Rainfall and Mortality');

Proc gplot data = pollution;


Plot rainfall*sample
mortality*sample/
vaxis=axis1 legend
overlay;
Run;

You can also add horizontal axis.

Stat 598 Samiran Ghosh


12

To get a confidence interval

Symbol1 i=rlclm95 v=star height=3 l=1 c=cyan;

title1 color=green font=triplex 'Sample vs. Mortality plot';

Proc gplot data = pollution;


Plot mortality*sample /vaxis=axis1;
Run;

Stat 598 Samiran Ghosh


13

Adding a Second Axis

Plot2 Statement generates a second set of points and a second vertical


axis on the right-hand side of the graph.
The X variable must be the same for both plot statements.
If there is no symbol2 statement to define the display setting for plot2 -
SAS will use the default setting (Scatter).
Use LEGEND and Overlay in both of the plot statements to generate a
legend identifying the plots.
Symbol1 i=j v=diamond h=0.3 l=1 c=black;
Symbol2 i=j v=# h=0.3 l=2 c=red;

Proc gplot data = pollution;


Plot mortality*sample /legend overlay;
plot2 rainfall*sample /legend overlay;
Run;

Note it is possible even to draw axis with

Stat 598 Samiran Ghosh


14

Unequal tick mark

It will be too complicated too show the code for the above here, but nevertheless the
point is that it is possible to get above plot by SAS.

Note:- Send me an email to get the code.

Stat 598 Samiran Ghosh


15

How to output SAS graphics in to PS file

Often it is require that you will save your figure as an external file. For example LaTEx
requires you to save it as PS (postscript file). Here is the trick (but only one figure at a
time)

GOPTIONS RESET=ALL;

filename outgraf1 "G:\stat598\graph1.ps";

GOPTIONS gsfname=outgraf1 gsfmode=replace dev=psepsf rotate=portrait


ftext=centb htext=0 htitle=1 hsize=5in vsize=4.5in;

axis1 label=(height=1.2 'Sample Number')


offset=(1.8);

axis2 label=(height=1.2 f=centb a=90 'SEM value from SRSWR')


order=(0 to 3 by 0.5)
offset=(4);

symbol1 color=red i=r value=star height=0.6;


PROC GPLOT data=ABC;
TITLE 'SEM plot from Sample of Size 100 (Popultaion size 1000)';
PLOT sem*sample/vaxis=axis2 haxis=axis1 ;
RUN;
Stat 598 Samiran Ghosh
16

1. Note the axis and label control


2. Note the figure size control
3. In fact you can change every details of this graph

Stat 598 Samiran Ghosh


17

To get similar things in GIF image file

GOPTIONS RESET=ALL;

filename pic2 "G:\stat598\temp.GIF";

goptions gsfname= pic2 gsfmode=replace DEVICE=psepsf rotate=portrait


cback=white ftext=centb htext=1 htitle=1
horigin=0 in vorigin=0 in hsize=5 in vsize=4 in;

GOPTIONS DEVICE=GIF GSFNAME=pic2


GSFMODE=REPLACE;
GOPTIONS XPIXELS=900 YPIXELS=900;

Stat 598 Samiran Ghosh


18

Proc Gchart

To draw pie chart, bar chart and many more standard plotting this is
useful

title1 color=green font=triplex 'Bar Chart


for Rainfall';
proc gchart ddata = pollution;
vbar rainfall ;
run ;

title1 color=green font=triplex 'Pie Chart for Rainfall';


proc gchart data = pollution;
pie rainfall/type=percent ;
run ;

Stat 598 Samiran Ghosh


19

A Fancy 3d plot
title1 color=green font=triplex 'Fancy hat plot';

data hat;
do x=-5 to 5 by 0.25;
do y=-5 to 5 by 0.25;
z=sin(sqrt(x*x+y*y));
output;
end;
end;
run;

proc g3d data=hat;


plot y*x=z ;
run;

Stat 598 Samiran Ghosh


20

proc g3d data=hat;


scatter y*x=z ;
run;

Some even fancier stuff J

data rough ;
set hat ;
if ranuni(1)<.5 then
output ;
run ;
proc g3d data=rough ;
plot y*x=z ;
run;

Stat 598 Samiran Ghosh


21

You can view, modify or create device drivers. Can use the interactive procedure as follows:

Proc Gdevice ;
Run ;

To view a font use:

proc gfont name=swiss nobuild ;


run ;

List all graphics options:

Proc goptions ;
Run ;

I will stop here but tutorial on SAS graphics can go on and on.

Stat 598 Samiran Ghosh