**SAS 9.2 Graphics Course**

**Kriss Harris**

**italjet125@yahoo.com**

**SGSCATTER**

**GTL**

**SGRENDER**

**SGPLOT**

**SGPANEL**

proc sgplot data = boxplots;

vbox results / category = Treatment;

run;

Scatterplot

Boxplot

ods html style = statistical;

proc sgplot data = data;

scatter x = B y = A;

run;

hbox

Bar Chart

proc sgplot data = boxplots;

vbar Treatment / response = results STAT = MEAN ;

run;

hbar

The other STAT options that are FREQ and SUM, SUM is the default

Quiz 1

STATEMENT X=var Y=var / options;

STATEMENT response-var / options;

STATEMENT category-var / options;

scatter

vbox

vbar

Means and Confidence Intervals

(for one factor)

proc sgplot data = boxplots;

vline Treatment / response = results STAT = MEAN LIMITSTAT = CLM markers;

run;

(for more complicated models)

Means and Confidence Intervals

ods output lsmeans = lsmeans;

proc mixed data = boxplots;

class treatment day;

model results = treatment day;

lsmeans treatment / cl;

run;

proc sgplot data = lsmeans;

scatter x = treatment y = estimate / YERRORLOWER = lower YERRORUPPER = upper;

series x = treatment y = estimate;

run;

Fit the Model

Plot the graph

Scatterplot with different markers for each group

proc sgplot data = merged;

scatter x = bodyweight y = Results / group = treatment;

run;

Used to change the size,

symbol and color of the

marker.

Scatterplot with regression slopes

for each treatment group

proc sgplot data = merged;

reg x = bodyweight y = Results / group = treatment;

run;

Scatterplot with different markers for each group

proc sgplot data = merged;

scatter x = bodyweight y = Results / group = treatment;

reg x = bodyweight y = Results / nomarkers;

run;

prevents

more markers

from being

produced

Quiz 2

Using noautolegend

SGPLOT Concepts

Basic Plots

Distribution Plots

Categorisation Plots

Fit and Confidence

Step, Band, Needle Plots

Scatter, Series

Regression

Bar Charts, Line Charts

Box plots, Histograms, and Normal

and Kernel density estimates

Dot Plots

SGPLOT Overlay Compatibility

Fit and Confidence

Basic Pl ts

Distribution Plot

Categorisation Plot

PLOTS

OPTIONS

proc sgplot data = merged noautolegend;

reg x = bodyweight y = Results / group = treatment;

run;

Inset Statement

proc sgplot data = merged noautolegend;

reg x = bodyweight y = Results / group = treatment;

inset "Protocol: YYY0001112" "Date: 26.08.09" / position = bottomright BORDER;

run;

Other position options are

Bottom, Bottomleft, Left, Right, Top,

Topleft and Topright.

Using separate strings places the

New string on a separate line.

Axis labels

proc sgplot data = scatter;

yaxis label = "Treatment A Response";

xaxis label = "Treatment B Response";

scatter x = B y = A;

run;

Other options for xaxis and yaxis statements include:

values – specify values or intervals:

i.e. values=(0 to 20 by 2) values=(1 3 10 to 50 by 5) values=(2 3 4 5 6)

min – specify minimum value of axis – i.e. min=0

max – specify maximum value of axis – i.e. max=100

Changing Marker Properties

proc sgplot data = scatter;

scatter x = B y = A / MARKERATTRS = (color = red size = 12 symbol = circlefilled);

yaxis label = "Treatment A Response";

xaxis label = "Treatment B Response";

run;

Marker Symbols

Adding a Reference Line

proc sgplot data = scatter;

scatter x = B y = A / MARKERATTRS = (size = 12 symbol = circlefilled color = red);

refline 100 / axis = y LINEATTRS = (color = black) ;

yaxis label = "Treatment A Response";

xaxis label = "Treatment B Response";

run;

Lineattrs for pattern

Quiz 3

ADVANCED OPTIONS

Changing Axis Font Size

proc template;

define style MyStyleDefault;

parent=Styles.statistical;

style GraphLabelText from GraphLabelText / fontsize = 14px;

style GraphValueText from GraphValueText / fontsize = 12px;

end;

run;

PROC Template

ods html style = MyStyleDefault;

proc sgplot data = scatter;

yaxis label = "Treatment A Response";

xaxis label = "Treatment B Response";

scatter x = B y = A;

run;

Controlling Symbol and Line Options

for each group %modstyle macro

http://support.sas.com/documentation/cdl/en/statug/63347/HTML/default/viewer.htm#statug_odsgraph_sect056.htm

Reference

%modstyle(

parent=statistical,

name=plotmod,

type=CLM,

colors=black blue,

fillcolors=colors,

markers=diamondfilled starfilled

);

Cycles through colors, line, and markers simultaneously

type = LMbyC

type = CbyLM

Adding Jitter

data boxplots_jittered;

set boxplots;

if treatment = "Treatment A" then trtcode = 10;

else if treatment = "Treatment B" then trtcode = 20;

treatment_jittered = trtcode + ranuni(10) - 0.5;

label treatment_jittered = "Treatment";

run;

proc format;

value trtfmt 9.5 - 11.5 = "Treatment A"

19.5 - 20.5 = "Treatment B"

other = " ";

run;

Adding Jitter

Adding Jitter

proc sgplot data = boxplots_jittered;

scatter y = results x = treatment_jittered / group = treatment_jittered ;

format treatment_jittered trtfmt. ;

xaxis label = "Treatment" values = (10,20)

valueshint offsetmax = 0.1 offsetmin = 0.1;

run;

Minimum and Maximum axis values are determined

independently of the values you specify in the

VALUES= option.

Useful as some of the jittered values will be less

than 10 and more than 20.

Leaving some room between the

left hand and right hand side of the graph

Making a new numerical variable for treatment

Using the Format Statement to recode the

Treatments so that the character value is

displayed on the axis

Adding a value between 0 and 1 to each

of the treatment codes,

and then subtracting 0.5 so the tick mark

is in the middle

SGPANEL Concepts

The SGPANEL procedure has a required PANELBY statement that is used to define the classifier variables for the panel. This statement must be specified before any plot, axis, or legend statement or else an error occurs.

The SGPANEL procedure creates the same plots as the SGPLOT procedure, the only difference is that you can panel them in SGPANEL.

The SGPANEL and SGPLOT procedures contain the same statements except that there is a PANELBY statement in Proc SGPANEL and there are COLAXIS and ROWAXIS statements instead of XAXIS and YAXIS.

Boxplot panelled by day

proc sgpanel data = boxplots;

panelby day;

vbox results / category = Treatment;

run;

proc sgpanel data = boxplots;

panelby day / columns = 1 novarname;

vbox results / category = Treatment;

run;

Boxplot Panelled by day

for 6 days

proc sgpanel data = boxplots_all;

panelby day / novarname;

vbox results / category = Treatment;

run;

proc sgpanel data = boxplots_all;

panelby day / novarname uniscale = column;

vbox results / category = Treatment;

run;

proc sgpanel data = boxplots_all;

panelby day / novarname uniscale = column spacing = 10;

vbox results / category = Treatment;

run;

Both rows have the

same scales, and the

second row has values all

above 100.

Only makes the rows have a

scale that fits each cell within that

row.

Specifies the number of pixels

between the rows and columns

in the panel.

Quiz 4

proc sgpanel data = boxplots_all;

panelby day / options??? ;

vbox results / category = Treatment;

run;

Questions

?

Proc SGPLOT and SGPANEL

similarities

Questions

?

Proc SGSCATTER

Useful for having a panel of plots that have at least one independent axis

PLOT Statement - 2 independent axis i.e. x and y

COMPARE Statement - 1 independent axis i.e. just x

Useful for producing a matrix of plots

MATRIX Statement

Indepedent x and y axis

PLOT Statement

proc sgscatter data=sashelp.cars;

plot mpg_highway*weight msrp*horsepower;

run;

Different variables

Different x variables

COMPARE statement

proc sgscatter data=sashelp.cars;

compare y=mpg_highway x=(weight enginesize horsepower) / group = type;

run;

Different x variables

Matrix Plot

MATRIX statement

proc sgscatter data=sashelp.iris

(where=(species eq 'Virginica'));

matrix petallength petalwidth sepallength

/ ellipse=(type=mean)

diagonal=(histogram kernel);

run;

3 Variables

Therefore produces 3 * 3 matrix plot

Quiz 5

MATRIX?

PLOT?

COMPARE?

Questions

?

Graph Template Language

The SAS/GRAPH Graph Template Language (GTL) is an extension to the Output Delivery System (ODS) that enables you to create sophisticated analytical graphics that are not available from traditional SAS/GRAPH procedure statements.

The GTL templates are defined with PROC TEMPLATE.

The GTL templates are rendered using the SGRENDER procedure, which specifies a data source that contains appropriate data values and the template to use for rendering the graph.

GTL Reference link: http://support.sas.com/documentation/cdl/en/grstatgraph/63878/HTML/default/viewer.htm#/documentation/cdl/en/grstatgraph/63878/HTML/default/p0891gx3y0z8xqn1k9ijhv5xughi.htm

LAYOUTS

Going to go through 3 different layouts

Overlay

Lattice

Gridded

Obtaining GTL Code

(for Boxplot example)

proc sgplot data = boxplots tmplout = "C:\Template_for_boxplots.sas" ;

vbox results / category = Treatment;

run;

Obtained GTL Code

For Boxplot

For Scatterplot

Looks daunting at

first but the only real

difference is within

the layout statement

proc template;

define statgraph sgplot;

dynamic _ticklist_;

begingraph;

layout overlay / xaxisopts=(type=Discrete discreteOpts=(tickValueList=_ticklist_));

BoxPlot X=Treatment Y=Results / SortOrder=Internal primary=true LegendLabel="Results" NAME="VBOX";

;

endlayout;

endgraph;

end;

run;

proc template;

define statgraph sgplot;

begingraph;

layout overlay;

ScatterPlot X=Treatment Y=Results / primary=true LegendLabel="Results" NAME="SCATTER";

;

endlayout;

endgraph;

end;

run;

Code is nested

similar to HTML

Why is GTL Overlay useful?

proc template;

define statgraph sgplot;

dynamic _ticklist_;

begingraph;

layout overlay / xaxisopts=(type=Discrete discreteOpts=(tickValueList=_ticklist_));

BoxPlot X=Treatment Y=Results / SortOrder=Internal primary=true LegendLabel="Results" NAME="VBOX";

ScatterPlot X=Treatment Y=Results / primary=true LegendLabel="Results" NAME="SCATTER";

;

endlayout;

endgraph;

end;

run;

proc sgrender data=boxplots template=sgplot;

run;

SGRENDER

plots the template

In SGPLOT you cannot

overlay a boxplot with any

other plot but you can in GTL!

Lattice and Overlay Layout

You don't have to memorise the code, can output it by using tmplout in SG Procedures

proc template;

define statgraph sgmypanel;

dynamic _ticklist_;

begingraph;

layout lattice;

layout overlay /

xaxisopts=(type=Discrete discreteOpts=(tickValueList=_ticklist_) LABEL = "Treatment") yaxisopts = (LABEL = "Results");

BoxPlot X=Treatment Y=Results / SortOrder=Internal primary=true LegendLabel="Results" NAME="VBOX";

endlayout;

layout overlay;

ScatterPlot X=Treatment Y=Results / primary=true LegendLabel="Results"

NAME="SCATTER";

endlayout;

endlayout;

endgraph;

end;

run;

The lattice layout is a multicell layout that combines features of gridded and overlay layouts

and offers reserved areas for additional formatting:

4 sidebars (top, bottom, left, and right) that span all rows and columns

individual row and column headings

individual cell headings

axis scaling on a per cell, per row, per column, all rows, or all columns basis

Lattice Layout

Lattice, Overlay and Gridded Layout

layout lattice / columns = 2;

layout overlay /

xaxisopts=(type=Discrete discreteOpts=(tickValueList=_ticklist_) LABEL = "Treatment"

yaxisopts = (LABEL = "Results");

BoxPlot X=Treatment Y=Results / SortOrder=Internal primary=true LegendLabel="Results"

NAME="VBOX";

endlayout;

layout overlay;

ScatterPlot X=Treatment Y=Results / primary=true LegendLabel="Results" NAME="SCATTER";

layout gridded /

columns=2 border=TRUE autoalign=(BottomLeft TopLeft TopRight);

entry "Mean of Dataset:"; entry "Mean";

entry "Std Dev of Dataset:"; entry "Std Dev";

endlayout;

endlayout;

endlayout;

Lattice

Overlay

(or gridded)

Using Dynamic Variables

Created a Dynamic Variable called VAR.

Using the statement: (after Proc Template)

Dynamic VAR;

layout gridded /

columns=2 border=TRUE autoalign=(BottomLeft TopLeft TopRight);

entry "Mean of Dataset:"; entry eval(strip(put(mean(VAR),8.2)));

entry "Std Dev of Dataset:"; entry eval(strip(put(stddev(VAR),8.2)));

endlayout;

Using EVAL to calculate the mean

and standard deviation

proc sgrender data=boxplots template=sgmypanel;

dynamic var = "Results";

run;

Rendering with

SGRENDER

Setting the dynamic variable to equal Results.

Quiz 6

The Gridded Layout

is the simplest organisation

of cells and can be used to

enter text!

Lattice layout = multicell layout

Overlayout is useful for

superimposing plots

What's the easiest way to obtain GTL Code?

Best for superimposing plots?

Best for complex multicell layouts?

Best for entering text?

Questions?

?

proc sgplot data = merged;

scatter x = bodyweight y = Results / group = treatment;

run;

proc sgplot data = merged;

reg x = bodyweight y = Results / group = treatment;

run;

proc sgplot data = merged;

scatter x = bodyweight y = Results / group = treatment;

reg x = bodyweight y = Results / nomarkers;

run;

What code?

Which words are not real plot statements?

refline

inset

set

inref