Josh Horstman is an independent statistical programming consultant and trainer based in Indianapolis with 20 years experience using SAS in the life sciences industry. He specializes in analyzing clinical trial data, and his clients have included major pharmaceutical corporations, biotech companies, and research organizations. Josh is a SAS Certified Advanced Programmer who loves coding as well as talking about coding at SAS Global Forum and other SAS User Group meetings. 02/09/202 0 About the Presenter Getting Started with the SGPLOT Procedure Getting Started with the SGPLOT Procedure: A Hands-On Workshop 1

02/09/202 0 WUSS 2018 - Hands-On Workshop Josh Horstman Getting Started with the SGPLOT Procedure Getting Started with the SGPLOT Procedure Getting Started with the SGPLOT Procedure INTRODUCTION TO SGPLOT 3 02/09/202 0

Prior to ODS, SAS limited to text-based SAS listing output ODS output makes use of colors, fonts, graphics, and more! ODS provides ability to produce output in various formats: and more! ODS is part of the Base SAS product since version 7 (No separate license required) 02/09/202 0 Getting Started with the SGPLOT Procedure Overview: The Output Delivery System (ODS) 4 An extension to ODS used to create analytical graphs Introduced in SAS 9.2 as part of SAS/GRAPH (experimental in v9.1)

Moved into the Base SAS product in version 9.3 Based on the Graph Template Language (GTL) 02/09/202 0 Getting Started with the SGPLOT Procedure Overview: ODS Statistical Graphics 5 Graph Template Language (GTL) comprehensive language for creating statistical graphics ODS Graphics procedures provide a procedural interface to most common features of GTL ODS GRAPHICS statement controls various graphic-related settings and options 02/09/202

0 Getting Started with the SGPLOT Procedure ODS Statistical Graphics Components ODS Graphics Editor interactive tool for modifying graphs ODS Graphics Designer graphical interface for designing graphs 6 SGPLOT single-cell plots SGPANEL multiple-panel plots SGSCATTER advanced scatter plots SGRENDER render graphs written in GTL 02/09/202 0

Getting Started with the SGPLOT Procedure ODS Statistical Graphics Procedures SGDESIGN used with ODS Graphics Designer 7 SG Procedures SAS/GRAPH SGPLOT, SGPANEL, SGSCATTER, etc. GPLOT, GCHART, GSLIDE, GBARLINE, GCONTOUR, etc. Based on templates

Based on device drivers Creates image files Creates catalog entries Use ODS GRAPHICS statement to control environment Use GOPTIONS statement to control environment Visual properties are set within the procedure Many properties set with global statements such as AXIS, LEGEND, SYMBOL, etc.

02/09/202 0 Getting Started with the SGPLOT Procedure Statistical Graphics vs. Legacy SAS/GRAPH 8 To create ODS graphs, a valid ODS destination must be open. Build an ODS sandwich around your graph code. For example, to output a graph to the PDF destination: ods pdf file="c:\example.pdf"; ; 02/09/202 0 Getting Started with the SGPLOT Procedure About ODS Destinations

ods pdf close; Similar syntax for ODS HTML, ODS RTF, etc. 9 Datasets in SASHELP library included with SAS you already have them! SASHELP.CLASS (Demographics on 19 students) 02/09/202 0 Getting Started with the SGPLOT Procedure Example Datasets SASHELP.CARS (Data about 428 car models) 10 SASHELP.HEART (5,209 patients from a heart study)

SASHELP.STOCKS (Stock prices of IBM, Intel, & Microsoft) 02/09/202 0 Getting Started with the SGPLOT Procedure More Example Datasets 11 02/09/202 0 proc sgplot data=; There are dozens of plot request statements

run; available SCATTER, Other optional statements control specific graph features XAXIS, YAXIS, REFLINE, INSET, KEYLEGEND, etc. SERIES, VBOX, VBAR, HIGHLOW, BUBBLE, etc. Getting Started with the SGPLOT Procedure Basic SGPLOT Syntax 12 SIMPLE PLOTS Getting Started with the SGPLOT Procedure

EXERCISES 1-11 13 02/09/202 0 02/09/202 0 Creates a scatter plot. proc sgplot data=; scatter x=variable y=variable < / options>; run; X and Y are required arguments that specify the variables to plot.

Getting Started with the SGPLOT Procedure The SCATTER Statement Include a slash before specifying one or more options. 14 Goal: Create a scatter plot of WEIGHT vs HEIGHT Input: SASHELP.CLASS Syntax: SCATTER statement X= argument Y= argument 02/09/202 0 Getting Started with the SGPLOT Procedure

Exercise #1: Basic Scatter Plot 15 proc sgplot data=sashelp.class; scatter x=height y=weight; run; 02/09/202 0 Getting Started with the SGPLOT Procedure Exercise #1: Basic Scatter Plot 16 proc sgplot data=; scatter x=variable y=variable

/ group=variable ; run; Specifies a variable used to group the data. Plot elements for each group value are automatically distinguished by different visual attributes. GROUP= option available on almost every plot type. 02/09/202 0 Getting Started with the SGPLOT Procedure The GROUP= Option 17 Goal: Create a scatter plot of WEIGHT vs HEIGHT, grouped by SEX

Input: SASHELP.CLASS Syntax: SCATTER statement X= argument Y= argument GROUP= option 02/09/202 0 Getting Started with the SGPLOT Procedure Exercise #2: Grouped Scatter Plot 18

proc sgplot data=sashelp.class; scatter x=height y=weight / group=sex; run; Legend automatically generated Alternative: Use BY statement to get separate graphs for each value. 02/09/202 0 Specifies a grouping variable

Getting Started with the SGPLOT Procedure Exercise #2: Grouped Scatter Plot 19 proc sgplot data=sashelp.class; scatter x=height y=weight / group=sex datalabel=name; run; Specifies a variable used to label each data point. 02/09/202 0 Getting Started with the SGPLOT Procedure

Exercise #2: Grouped Scatter Plot - BONUS 20 02/09/202 0 Creates a bubble plot. proc sgplot data=; bubble x=variable y=variable size=variable < / options>; run; X and Y are required arguments that specify the variables to plot. SIZE is a required argument that specifies a variable that controls the

size of the bubbles. Getting Started with the SGPLOT Procedure The BUBBLE Statement 21 Goal: Create a bubble plot of WEIGHT vs. HEIGHT, grouped by SEX with bubbles sized by AGE. Input: SASHELP.CLASS Syntax: BUBBLE statement

X= argument Y= argument SIZE= argument GROUP= option 02/09/202 0 Getting Started with the SGPLOT Procedure Exercise #3: Grouped Bubble Plot 22 proc sgplot data=sashelp.class; bubble x=height y=weight size=age / group=sex; run; 02/09/202 0

Getting Started with the SGPLOT Procedure Exercise #3: Grouped Bubble Plot 23 Creates a line plot. proc sgplot data=; series x=variable y=variable < / options>; run; X and Y are required arguments that specify the variables to plot. By default, only lines are shown, not the points themselves. To add markers to points, use MARKERS option. 02/09/202 0 Getting Started with the SGPLOT Procedure

The SERIES Statement 24 Goal: Create a series plot of closing price (CLOSE) by date (DATE) grouped by company (STOCK). Add a title to your plot. Input: SASHELP.STOCKS Syntax: SERIES statement X= argument Y= argument GROUP= option TITLE statement

02/09/202 0 Getting Started with the SGPLOT Procedure Exercise #4: Grouped Series Plot 25 proc sgplot data=sashelp.stocks; title "Stock Prices 1986-2005"; series x=date y=close / group=stock; run; 02/09/202 0 Getting Started with the SGPLOT Procedure Exercise #4: Grouped Series Plot

26 Creates floating vertical or horizontal lines representing high and low values. proc sgplot data=; highlow x=variable | y=variable high=variable low=variable < / options>; run; Use either X OR Y to specify values to plot along X or Y axis. 02/09/202 0 Getting Started with the SGPLOT Procedure The HIGHLOW Statement

Use both HIGH AND LOW to specify upper and lower values for the floating lines. Add CLOSE= option to specify variable for a closing tick mark. 27 Goal: Create a high-low plot of monthly stock prices with closing ticks for the stock IBM during the year 2005. Input: SASHELP.STOCKS Syntax:

HIGHLOW statement X= argument HIGH= argument LOW= argument CLOSE= option WHERE statement 02/09/202 0 Getting Started with the SGPLOT Procedure Exercise #5: High-Low Plot 28 proc sgplot data=sashelp.stocks; where stock='IBM' and date >= "01jan2005"d; highlow x=date high=high low=low / close=close; run;

Use X= for vertical bars or Y= for horizontal, but not both! CLOSE variable determines locations of closing ticks 02/09/202 0 HIGH and LOW specify endpoints of each bar. Getting Started with the SGPLOT Procedure

Exercise #5: High-Low Plot 29 Creates a horizontal box plot. proc sgplot data=; hbox variable < / options>; run; Analysis variable must be numeric! Use CATEGORY= option to create a box for each distinct value of a category variable. (Can be combined with GROUPing.) VBOX statement is analogous for vertical box plots. 02/09/202 0 Getting Started with the SGPLOT Procedure

The HBOX Statement 30 Q1 Minimum Value Above Lower Fence Mean Q3 Median Lower Fence = Q1 1.5*IQR Upper Fence = Q3 + 1.5*IQR

Outlier 02/09/202 0 Distance between Q1 and Q3 is the Inter-Quartile Range (IQR) Getting Started with the SGPLOT Procedure Anatomy of a Box Plot Maximum Value Beneath Upper Fence Values outside fence are considered outliers.

31 Goal: Create a horizontal box plot of vehicle price (MSRP) by vehicle type (TYPE). Input: SASHELP.CARS Syntax: HBOX statement Numeric analysis variable CATEGORY= option 02/09/202 0 Getting Started with the SGPLOT Procedure Exercise #6: Horizontal Box Plot 32 proc sgplot data=sashelp.cars;

title "Price by Car Type"; hbox msrp / category=type; run; 02/09/202 0 Getting Started with the SGPLOT Procedure Exercise #6: Horizontal Box Plot 33 Creates a vertical bar chart. proc sgplot data=; vbar categorical-variable < / options>; run; RESPONSE= option specifies response variable to control length of bars. (Otherwise, bars represent frequency counts.)

STAT= option specifies statistic for length of bars (Default is SUM when RESPONSE variable is included, FREQ otherwise.) HBAR statement is analogous for horizontal bar charts. 02/09/202 0 Getting Started with the SGPLOT Procedure The VBAR Statement 34 Goal: Create a vertical bar chart of mean engine size (ENGINESIZE) by vehicle origin (ORIGIN). Input: SASHELP.CARS Syntax:

VBAR statement Categorical variable RESPONSE= option STAT= option 02/09/202 0 Getting Started with the SGPLOT Procedure Exercise #7: Vertical Bar Chart 35 proc sgplot data=sashelp.cars; title "Mean Engine Size by Origin"; vbar origin / response=enginesize stat=mean; run;

02/09/202 0 Getting Started with the SGPLOT Procedure Exercise #7: Vertical Bar Chart 36 proc sgplot data=sashelp.cars; title "Mean Engine Size by Origin"; vbar origin / response=enginesize stat=mean limits=both; run; LIMITS= option adds upper limits, lower limits, or both. 02/09/202

0 Getting Started with the SGPLOT Procedure Exercise #7: Vertical Bar Chart - BONUS LIMITSTAT= option specifies statistics (default is confidence limits). 37 proc sgplot data=; vbar categorical-variable / group=variable ; run; GROUP= option will create a bar for each distinct value of a grouping variable, within each category. Use GROUPDISPLAY= to specify how bars are grouped (CLUSTER or STACK)

02/09/202 0 Getting Started with the SGPLOT Procedure The GROUP= Option 38 Goal: Create a vertical bar chart of mean engine size (ENGINESIZE) by vehicle type (TYPE) and grouped into clusters by vehicle origin (ORIGIN). Input: SASHELP.CARS Syntax:

VBAR statement Categorical variable RESPONSE= option STAT= option GROUP= option GROUPDISPLAY= option 02/09/202 0 Getting Started with the SGPLOT Procedure Exercise #8: Grouped Vertical Bar Chart 39 proc sgplot data=sashelp.cars; title "Mean Engine Size by Type and Origin"; vbar type / response=enginesize stat=mean

group=origin groupdisplay=cluster; run; 02/09/202 0 Getting Started with the SGPLOT Procedure Exercise #8: Grouped Vertical Bar Chart 40 proc sgplot data=sashelp.cars; title "Mean Engine Size by Type and Origin"; vbar type / response=enginesize stat=mean group=origin groupdisplay=stack; run;

To stack the bars, use the GROUPDISPLAY= option with a value of STACK instead of CLUSTER. 02/09/202 0 Getting Started with the SGPLOT Procedure Exercise #8: Grouped Vertical Bar Chart - BONUS 41 Color-codes rectangles based on two-dimensional binning of data. proc sgplot data=; heatmap x=variable y=variable < / options>; run; X and Y are required arguments that specify the variables to plot.

Options are available to control the size and/or number of bins in each dimension as well as the colors used. 02/09/202 0 Getting Started with the SGPLOT Procedure The HEATMAP Statement 42 Goal: Create a heat map of CHOLESTEROL vs. WEIGHT. Input: SASHELP.HEART Syntax: HEATMAP statement X= option Y= option

02/09/202 0 Getting Started with the SGPLOT Procedure Exercise #9: Heat Map 43 proc sgplot data=sashelp.heart; heatmap x=weight y=cholesterol; run; Data are grouped into bins in two dimensions. 02/09/202 0 Bins are colored

according to frequency count. Getting Started with the SGPLOT Procedure Exercise #9: Heat Map 44 proc sgplot data=sashelp.heart; heatmap x=weight y=cholesterol / nxbins=50 nybins=50; run; 02/09/202 0 Getting Started with the SGPLOT Procedure

Exercise #9: Heat Map - BONUS Specify 50 bins in the X dimension and 50 bins in the Y dimension (2500 total rectangles). 45 Creates a vertical line chart (line is horizontal). proc sgplot data=; vline categorical-variable < / options>; run; VLINE plots statistics, SERIES plots raw data points RESPONSE= and STAT= options are similar to VBAR HLINE statement is analogous for horizontal line charts. 02/09/202 0 Getting Started with the SGPLOT Procedure

The VLINE Statement 46 Goal: Create a vertical line chart of mean HEIGHT by AGE grouped by SEX and include plot markers. Input: SASHELP.CLASS Syntax: VLINE statement Categorical variables RESPONSE= option

STAT= option GROUP= option MARKERS options 02/09/202 0 Getting Started with the SGPLOT Procedure Exercise #10: Grouped Vertical Line Chart 47 proc sgplot data=sashelp.class; title "Height by Age and Sex"; vline age / response=height stat=mean markers group=sex; run; 02/09/202

0 Getting Started with the SGPLOT Procedure Exercise #10: Grouped Vertical Line Chart 48 proc sgplot data=sashelp.class; title "Height by Age and Sex"; vline age / response=height stat=mean markers group=sex limits=both; run; Adds confidence limits 02/09/202 0 Getting Started with the SGPLOT Procedure

Exercise #10: Grouped Vertical Line Chart - BONUS 49 Fits a regression line or curve. proc sgplot data=; reg x=variable y=variable < / options>; run; X and Y are required arguments that specify the variables to plot. Includes both plot markers and line by default. Remove markers with NOMARKERS option. 02/09/202 0 Getting Started with the SGPLOT Procedure The REG Statement

50 Goal: Create a regression plot of WEIGHT vs. HEIGHT. Input: SASHELP.CLASS Syntax: REG statement X= option Y= option 02/09/202 0 Getting Started with the SGPLOT Procedure Exercise #11: Regression Plot 51 proc sgplot data=sashelp.class;

title "Height vs. Weight"; reg x=height y=weight; run; 02/09/202 0 Getting Started with the SGPLOT Procedure Exercise #11: Regression Plot 52 proc sgplot data=sashelp.class; title "Height vs. Weight"; reg x=height y=weight / clm cli; run; CLM adds confidence limits. CLI adds prediction limits.

02/09/202 0 Getting Started with the SGPLOT Procedure Exercise #11: Regression Plot - BONUS 53 COMBINATION PLOTS Getting Started with the SGPLOT Procedure EXERCISES 12-14 54 02/09/202 0

proc sgplot data=; Combination plots are created when more than one plot statement is used. ... run; All plots are overlaid atop one another in the same graph space and using the same axis system. 02/09/202 0 Getting Started with the SGPLOT Procedure

About Combination Plots 55 HISTOGRAM creates a histogram. DENSITY creates a density curve. proc sgplot data=; histogram response-variable < / options>; density response-variable < / options>; run; 02/09/202 0 Getting Started with the SGPLOT Procedure The HISTOGRAM and DENSITY Statements 56

Goal: Create a combination plot including both a histogram and density plot of WEIGHT. Suppress the automatic legend. Input: SASHELP.HEART Syntax: HISTOGRAM statement Response variable DENSITY statement 02/09/202 0 Getting Started with the SGPLOT Procedure Exercise #12: Histogram and Density Plot Response variable NOAUTOLEGEND option (on PROC SGPLOT)

57 proc sgplot data=sashelp.heart noautolegend; title "Weight Distribution of Patients"; histogram weight; density weight; run; Two plot statements are used. Either could have been used separately. Density is drawn on top of histogram because it appears later in SGPLOT. 02/09/202 0 Suppress

automatic legend Getting Started with the SGPLOT Procedure Exercise #12: Histogram and Density Plot 58 Goal: Create a layered vertical bar chart showing both city (MPG_CITY) and highway mileage (MPG_HIGHWAY) by type. Input: SASHELP.CARS Syntax:

VBAR statement (twice!) Categorical variable RESPONSE= option STAT= option BARWIDTH=0.5 option (only on second VBAR) 02/09/202 0 Statistical Graphics (SG) Procedures Exercise #13: Layered Vertical Bar Chart 59 proc sgplot data=sashelp.cars; title 'Mileage by Type'; vbar type / response=mpg_city stat=mean; vbar type / response=mpg_highway stat=mean

barwidth=0.5; run; Decrease bar width to 50% MPG_HIGHWAY is layered over MPG_CITY because we plotted MPG_HIGHWAY second. 02/09/202 0 Statistical Graphics (SG) Procedures Exercise #13: Layered Vertical Bar Chart 60 proc sgplot data=sashelp.class;

vbar age / response=height stat=mean; scatter x=height y=age; run; ERROR: Attempting to overlay incompatible plot or chart types. 02/09/202 0 Statistical Graphics (SG) Procedures The VBARBASIC and HBARBASIC Statements Some combinations of plot statements are not allowed To combine VBAR or HBAR with basic plot types (such as SCATTER, SERIES, etc.), use VBARBASIC or HBARBASIC. 61

By default, all plots requests use a common set of axes Use the X2AXIS and/or Y2AXIS options on a plot statement to use the secondary axes. Secondary X Axis Primary Y Axis 02/09/202 0 Getting Started with the SGPLOT Procedure Multiple Axis Systems in Combination Plots Secondary Y Axis 62 Primary X Axis Goal: Combine a series plot of closing price and a vertical bar

chart of trading volume for monthly IBM stock data for 2005. Input: SASHELP.STOCKS Syntax: VBARBASIC statement Categorical variable RESPONSE= option Y2AXIS option 02/09/202 0 Statistical Graphics (SG) Procedures Exercise #14: Bar and Line Chart SERIES statement X= and Y= arguments MARKERS option WHERE statement

63 proc sgplot data=sashelp.stocks; title IBM Stock Price and Volume for 2005'; where stock='IBM' and year(date)=2005; vbarbasic date / response=volume y2axis; series x=date y=close / markers; run; 02/09/202 0 Statistical Graphics (SG) Procedures Exercise #14: Bar and Line Chart

64 Getting Started with the SGPLOT Procedure WRAP-UP 65 02/09/202 0 The SGPLOT procedure is extremely versatile. Presentation-ready graphics with minimal coding. We've only scratched the surface! Dozens more statements and hundreds more options provide: Complete customization of plot symbols, lines, labels, titles, axes, legends and much more 02/09/202 0

Getting Started with the SGPLOT Procedure Conclusion Advanced features like axis tables and attribute maps Custom annotation facility 66 SAS 9.4 ODS Graphics: Procedures Guide, Sixth Edition (SAS Press, 2016) View in browser at http://support.sas.com/documentation/cdl/en/grstatproc/ 69716/HTML/default/viewer.htm#titlepage.htm 02/09/202 0 Getting Started with the SGPLOT Procedure

Recommended Resources Download PDF free at http://support.sas.com/documentation/cdl/en/grstatproc/ 69716/PDF/default/grstatproc.pdf 67 Over 30,000 SAS User Group papers available free online at http://www.lexjansen.com. 40+ years of papers from SAS Global Forum (formerly SUGI), PharmaSUG, and regional conferences (WUSS, MWSUG, etc.) Fully searchable database Includes many papers authored by SAS staff including ODS Statistical Graphics experts such as Dan Heath, Warren Kuhfeld, Sanjay Matange, Cynthia Zender, and others. 02/09/202 0

Getting Started with the SGPLOT Procedure Recommended Resources 68 Thank you for attending! Feel free to contact me with questions or comments: Josh Horstman Nested Loop Consulting [email protected] 317-721-1009 02/09/202 0 Getting Started with the SGPLOT Procedure Contact Information

69