Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to SAS Essentials Mastering SAS for Data Analytics

Similar presentations


Presentation on theme: "Introduction to SAS Essentials Mastering SAS for Data Analytics"— Presentation transcript:

1 Introduction to SAS Essentials Mastering SAS for Data Analytics
Alan Elliott and Wayne Woodward SAS ESSENTIALS -- Elliott & Woodward

2 Chapter 18: CREATING CUSTOM GRAPHS PART 2
SAS ESSENTIALS -- Elliott & Woodward

3 18.4 CREATING STACKED BAR CHARTS
Another often-used type of bar chart is a stacked bar chart. To create stacked bar charts do not use the GROUP option. Do Hands On Exercise p 410 (AGRAPH7.SAS) PATTERN1 V=R1 C=RED; * FOR BUS; PATTERN2 V=R1 C=BLUE; * FOR CAR; PATTERN3 V=R1 C=BLACK; * FOR WALK; TITLE C=RED 'Stacked Bars'; PROC GCHART DATA=MYSASLIB.SURVEY; VBAR GENDER / SUBGROUP=ARRIVE WIDTH=15; RUN;QUIT; Note – SUBGROUP but no GROUP option SAS ESSENTIALS -- Elliott & Woodward

4 Resulting Bar Chart By not including a GROUP option, the bars are stacked rather than each group being a separate bar. SAS ESSENTIALS -- Elliott & Woodward

5 Make a few changes… Results is a chart in grays…
PATTERN1 V=S C=GRAYAA; *FOR BUS(Dark Gray); PATTERN2 V=S C=GRAYCC; *FOR CAR (Medium Gray) ; PATTERN3 V=S C=GRAYEE; *FOR WALK (Light GRAY) ; Results is a chart in grays… SAS ESSENTIALS -- Elliott & Woodward

6 Put the GROUP Option Into the Code
PROC GCHART DATA=MYSASLIB.SURVEY; VBAR GENDER / GROUP=ARRIVE SUBGROUP=GENDER WIDTH=10; RUN;QUIT; Bars not stacked SAS ESSENTIALS -- Elliott & Woodward

7 Open and Run AGRAPH7A.SAS
Put together several of the options/ ideas to make a finished chart… PATTERN1 V=R1 C=RED; * FOR BUS; PATTERN2 V=R2 C=BLUE; * FOR CAR; PATTERN3 V=R3 C=BLACK; * FOR WALK; LEGEND1 ACROSS=1 LABEL=("Gender") POSITION=(top inside left) VALUE=(H=1 'Male' 'Female'); AXIS1 label=(A=90 Color=RED H=2 F="Swiss/Bold" "Count"); TITLE C=RED 'Example Bar Chart'; PROC GCHART DATA=MYSASLIB.SURVEY; VBAR GENDER / GROUP=ARRIVE SUBGROUP=GENDER WIDTH=10 LEGEND=LEGEND1 RAXIS=AXIS1 OUTSIDE=FREQ ; RUN; QUIT; SAS ESSENTIALS -- Elliott & Woodward

8 Resulting Chart… SAS ESSENTIALS -- Elliott & Woodward

9 Discussion of the code… (LEGEND)
ACROSS=1 LABEL=("Gender") POSITION=(top inside left) VALUE=(H=1 'Male' 'Female'); The LEGEND1 statement creates a key labeled "Gender" that appears at the top, inside left in the graph frame, with the key items "Male" and "Female" listed. The ACROSS option specifies that the key has items listed one across. SAS ESSENTIALS -- Elliott & Woodward

10 Discussion of the code (AXIS)
AXIS1 label=(A=90 Color=RED H=2 F="Swiss/Bold" "Count"); The AXIS1 statement defines an axis to be used as the vertical axis where the name "Count" will be displayed at a 90° angle using the Swiss font, in red, with a height of2 units (default is 1). In the PROC GCHART statements, note the LEGEND = LEGEND1 and RAXIS=AXIS1 statements. These apply the legend and axis to the chart. The statement OUTSIDE=FREQ. This tells the program to display the count (FREQ) outside for each bar. SAS ESSENTIALS -- Elliott & Woodward

11 18.5 CREATING MEAN BARS USING GCHART
An often-used analysis graphic is one that displays means as bars with error bars around the mean indicating a level of variability. Do Hands On Exercise p 412 (AGRAPH8.SAS) SAS ESSENTIALS -- Elliott & Woodward

12 MEANS will be displayed with 99% CI Errorbars on TOP
Code for Means Plot PATTERN1 V=L3 COLOR=LIGHTBLUE; AXIS1 LABEL=(A=90 H=1.5 F="SWISS" "OBSERVATION" C=BLUE); AXIS2 LABEL=(H=1.5 F="SWISS" "GROUP"); TITLE1 'Mean displayed with 95% error bars'; PROC GCHART DATA=MYSASLIB.SOMEDATA; VBAR GP / WIDTH=10 TYPE=MEAN SUMVAR=TIME1 INSIDE=MEAN CLM=99 ERRORBAR=TOP RAXIS=AXIS1 MAXIS=AXIS2 COUTLINE=SAME WOUTLINE=2; RUN; QUIT; Note TYPE=MEAN MEANS will be displayed with 99% CI Errorbars on TOP SAS ESSENTIALS -- Elliott & Woodward

13 Means Plot Results TYPE=MEAN SUMVAR=TIME1
Results in MEAN of TIME 1 displayed. INSIDE=MEAN CLM=99 ERRORBAR=TOP Results in the error bar on top and the mean in the bar SAS ESSENTIALS -- Elliott & Woodward

14 Summary of the options used
PATTERN1 makes the bars light blue with an L3 pattern. AXIS2 specifies the RAXIS (Response axis), AXIS2 specifies the MAXIS (Midpoint Axis). RAXIS is displayed rotated by 90o and both are displayed with a height of 1.5. SUMVAR=TIME1 tells SAS to use TIME1 to calculate the mean INSIDE=MEAN results in values displayed in the bars. CLM= 99 specifies a 99% confidence interval on the mean. TYPE=MEAN specifies the MEAN statistic used in the ERRORBAR. ERRORBAR=TOP causes the CI error bars to be displayed on top. COUTLINE and WOUTLINE specify a black outline with width 1. SAS ESSENTIALS -- Elliott & Woodward

15 Make Changes to AXIS Statement
AXIS1 LABEL=(A=90 H=l.S F="SWISS" "OBSERVATION" C=BLUE) MAJOR=(COLOR=BLUE HEIGHT=3 WIDTH=2) MINOR=(NUMBER=4 COLOR=RED HEIGHT=l); Observe changes in how the vertical axis is displayed. (Major and Minor axes) SAS ESSENTIALS -- Elliott & Woodward

16 Try These Changes Change the ERRORBAR specification to BOTH, rerun and observe the changes. Take off the ERRORBAR, TYPE, and CLM Statements and change INSIDE=MEAN to OUTSIDE=FREQ ( ERRORBAR, TYPE, and CLM do not work with FREQ) Rerun and observe the changes. SAS ESSENTIALS -- Elliott & Woodward

17 18.6 CREATING BOXPLOTS This section provides information on methods for creating and enhancing box plots. A simplified syntax for PROC BOXPLOT is: PROC BOXPLOT <DATA=datasetname>; <Options>; PLOT <depvar*>groupvar(s)</options>; <Statements>; The PLOT statement is required. For example, a typical simple use (group must first be sorted): PROC BOXPLOT DATA=MYDATA; PLOT HEIGHT*GROUP; RUN; SAS ESSENTIALS -- Elliott & Woodward

18 Example BoxPlot Code 18.10 Common Statements for PROC BOXPLOT
PROC BOXPLOT DATA=CARDATA; PLOT HWYMPG*CYLINDERS /CBOXFILL=LIGHTGREEN; RUN; 18.10 Common Statements for PROC BOXPLOT Statement Explanation DATA=dataname Specifies which data set to use. PLOT <depvar*>groupvar(s); Specifies the variables to use in the plot such as PLOT AGE*GROUP; INSET Specifies an inset key similar to those previously described for histograms. ID variables; Specifies variables used to identify observations BY, FORMAT, LABEL, WHERE Statements common to most procedures. SAS ESSENTIALS -- Elliott & Woodward

19 Specifies outline color for boxes. For example CBOXES=RED.
18.11 Common options the BOXPLOT PLOT Statement (Follows a slash (/) in PLOT Statement) Option Explanation CBOXES=color Specifies outline color for boxes. For example CBOXES=RED. CBOXFILL=color Specifies the fill pattern for boxes. For example CBOXFILL=LIGHTBLUE. NOCHES Species that boxes will be notches. For an explanation for notches, see McGill, Tukey, and Larsen (1978) BOXSTYLE= Specifies the style of the boxplot. Options include SCHEMATIC, SKELETAL (default). SCHEMATIC displays outliers. SKELETAL does not display outliers. (See example that follows for more explanation.) SAS ESSENTIALS -- Elliott & Woodward

20 Do Hands On Example p 415 (AGRAPH9.SAS)
ODS GRAPHCS OFF may be needed if you are using SAS 9.4 or later. ODS GRAPHICS OFF; Title "Distribution of HwyMPG by Cylinders"; * Sort the data on the grouping variable; PROC SORT DATA=MYSASLIB.CARS OUT=CARDATA;BY CYLINDERS; PROC BOXPLOT DATA=CARDATA; PLOT HWYMPG*CYLINDERS /CBOXFILL=LIGHTGREEN; WHERE CYLINDERS GT 1; RUN; BOXPLOT specification in the PLOT statement SAS ESSENTIALS -- Elliott & Woodward

21 Resulting Plot for PROC BOXPLOT
SAS ESSENTIALS -- Elliott & Woodward

22 Make changes in PLOT Statement
PLOT HWYMPG*CYLINDERS/ CBOXFILL=VLIGB CBOXES=BLACK BOXSTYLE=SCHEMATIC NOTCHES; WHERE CYLINDERS GT 1; The SCHEMATIC option causes outliers to be displayed defined as values greater than the absolute value of 1.5*(interquartile range) NOTCHES measure the difference between two medians. If there is no overlap in the corresponding notches for two plots, it indicates a significant difference in the group medians at a .95 CL. SAS ESSENTIALS -- Elliott & Woodward

23 Add an ID MODEL; statement
The ID statement causes identifications to be displayed for outliers SAS ESSENTIALS -- Elliott & Woodward

24 Add an INSET Statement INSETGROUP MEAN (5.1 ) MIN MAX / HEADER = 'Mean and Extremes'; The INSET statement added MEAN, MIN and MAX in a table above the graph. SAS ESSENTIALS -- Elliott & Woodward

25 Finished Code for Boxplot Example
Title "Distribution of HwyMPG by Cylinders"; PROC SORT DATA=MYSASLIB.CARS OUT=CARDATA;BY CYLINDERS; PROC BOXPLOT DATA=CARDATA; PLOT HWYMPG*CYLINDERS/ CBOXFILL=VLIGB CBOXES=BLACK BOXSTYLE=SCHEMATIC NOTCHES; ID MODEL; INSETGROUP MEAN (5.1) MIN MAX / HEADER = 'Mean and Extremes'; WHERE CYLINDERS GT 1; RUN; SAS ESSENTIALS -- Elliott & Woodward

26 18.7 GOING DEEPER: CREATING AN INTERACTIVE BAR USING ODS
A useful feature of the ODS introduced in Chapter 8 is its ability to create interactive links within graphs in HTML mode. To do this you create a variable that creates a hypertext link, and then you apply that link to a portion of a graph. The following example uses a graph created using the SAS procedure PROC GCHART, where each bar is a link to a report. Do Hands On Example p 418 (AGRAPH10.SAS) SAS ESSENTIALS -- Elliott & Woodward

27 Begin with a Simple Bar Chart
TITLE "Bar Chart"; PROC GCHART DATA=MYSASLIB.SOMEDATA; HBAR GP/ DISCRETE; RUN; QUIT; SAS ESSENTIALS -- Elliott & Woodward

28 Create a Report for each Group
Suppose for each "group," represented by one of the bars on the plot, you want to make available a set of background statistics. You can associate a link for each bar on the graph to a separate Web page. Thus, each bar becomes a Web page link, and when you click on a bar on the graph, you open an HTML page. The code shown in Figure creates an HTML file that contains the report (from PROC MEANS} you wish to associate with Group A. Note that the HTML report is saved in the file named GPA.HTM, and that it contains information about GP="A". (Remember that the format to specify the file in ODS HTML is BODY=.) SAS ESSENTIALS -- Elliott & Woodward

29 Create a Report for Group A
ODS HTML BODY="C:\SASDATA\GPA.HTM"; PROC MEANS DATA= "C:\SASDATA\SOMEDATA" MAXDEC=2 N MEAN STD; VAR AGE TIME1-TIME4; WHERE GP="A"; TITLE 'Summary for GROUP A'; RUN; Create similar report for Groups B and C. The HTML type report is stored in the file named GPA.HTM Creates a report for observations in group a (GP=“A”) SAS ESSENTIALS -- Elliott & Woodward

30 The Report for Group A Do Hands On Example p 419 (AGRAPH11.SAS)
SAS ESSENTIALS -- Elliott & Woodward

31 Code to Create LINK in a Bar Chart
HTMLLINK Created here DATA BARCHART;SET MYSASLIB.SOMEDATA; LENGTH HTMLLINK $40; IF GP="A" THEN HTMLLINK='HREF="GPA.HTM"'; IF GP="B" THEN HTMLLINK='HREF="GPB.HTM"'; IF GP="C" THEN HTMLLINK='HREF="GPC.HTM"'; ODS HTML FILE="C:\SASDATA\GCHART.HTM" GPATH=“C:\SASDATA\"; * PART 3: CREATE THE GRAPH; PROC GCHART; HBAR GP/ HTML=HTMLLINK; TITLE 'Summary information for each GP.'; RUN; QUIT; HTMLLINK used here SAS ESSENTIALS -- Elliott & Woodward

32 Explanation of CODE LENGTH HTMLLINK $40;
The HTMLLINK variables defines the hyperlink for Group A LENGTH HTMLLINK $40; IF GP="A" THEN HTMLLINK='HREF="GPA.HTM"'; This code Sets up a length for the new variable HTMLLINK (long enough for an internet address) For Group A, assigns a value to HTMLLINK that is the HREF (an internet address or Hyperlink Reference) to the report that contains information about Group A The same is done for Groups B and C HREF means Hyperlink Reference (an “internet” address.) SAS ESSENTIALS -- Elliott & Woodward

33 Explanation of Code Defines an HTML (internet lnguage) file where the bar chart is saved, plus defines a path (GPATH) to associate with the links. ODS HTML FILE="C:\SASDATA\GCHART.HTM" GPATH=“C:\SASDATA\"; PROC GCHART; HBAR GP/ HTML=HTMLLINK; TITLE 'Summary information for each GP.'; RUN; QUIT; Creates the Bar Chart, and associated the bars to the HTML link named HTMLLINK. SAS ESSENTIALS -- Elliott & Woodward

34 Run AGRAPH11.SAS Result looks the same as before
But click on one of the bars, and a “report” for that bar is displayed. Change the last IF to IF GP="C" THEN HTMLLINK='HREF=" Rerun, and click on the C bar to observe results. SAS ESSENTIALS -- Elliott & Woodward

35 18.8 GOING DEEPER: SGPLOTS Starting with SAS 9.2, a new way of creating high-quality graphs was introduced. These are called Statistical Graphics Procedures. These include SGPLOT, SGSCATTER, and SGPANEL. We'll illustrate the use of SGPLOT in this section. An advantage of these "SG“ plots is that they can easily be created in standard graphical file types such as JPG (JPEG) or TIF. The basic syntax for SGPLOT is PROC SGPLOT <DATA=dataname> plotstatements/options; SAS ESSENTIALS -- Elliott & Woodward

36 SGPLOT Syntax At least one plot type statement is required. For example, PROC SGPLOT; HISTOGRAM AGE; RUN; To add a density plot (default is a normal plot), you simply add another plot statement: DENSITY AGE; SAS ESSENTIALS -- Elliott & Woodward

37 Table 18.13 Common Plot Statements for PROC SGPLOT Plot Statement
Explanation HBAR vname or VBAR vname Specifies horizontal or vertical bar chart. For example: HBAR AGE; HISTOGRAM vname Specifies histogram. For example: HISTOGRAM AGE; SCATTERPLOT Specifies scatterplot. SCATTER X=AGE Y=SBP; DENISTY vname Displays density plot. For example DENSITY AGE </TYPE=Kernel or Normal) KEYLEGEND/options; LOCATION= location of legend (INSIDE or OUTSIDE) POSITION (topleft , topright, bottomleft, bottomright) XAXIS options; and YAXIS options; Options include LABEL=’Label name’, MAX=, MIN=, VALUES=(Min to Max by interval), and OFFSETMIN= OFFSETMAX= (from 0 to 1) REG X=xvar Y=yvar/options Specify regression line for scatterplot where options include ALPHA=, CLI and CLM and GROUP=. SAS ESSENTIALS -- Elliott & Woodward

38 Do Hands On Exercise p 422 (SGPLOT1.SAS)
PROC SGPLOT DATA=MYSASLIB.WOUND; HISTOGRAM SBP; TITLE "SGPLOT Histogram"; RUN; SAS ESSENTIALS -- Elliott & Woodward

39 Add this code (after HISTOGRAM line)
DENSITY SBP/TYPE=KERNEL; SAS ESSENTIALS -- Elliott & Woodward

40 Add this code DENSITY SBP/TYPE=NORMAL;
SAS ESSENTIALS -- Elliott & Woodward

41 Include multiple plots – they overlay one another.
Final Code PROC SGPLOT DATA=MYSASLIB.WOUND; HISTOGRAM SBP; DENSITY SBP/TYPE=KERNEL; DENSITY SBP/TYPE=NORMAL; TITLE "SGPLOT Histogram"; RUN; Include multiple plots – they overlay one another. SAS ESSENTIALS -- Elliott & Woodward

42 Creating an SGPLOT BAR CHART
Do Hands On Exercise p 423 (SGPLOT2.SAS) PROC SGPLOT DATA=MYSASLIB.WOUND; HBAR RACE_CAT; TITLE "SGPLOT Bar Chart"; RUN; SAS ESSENTIALS -- Elliott & Woodward

43 Make Changes Change to VBAR RACE_CAT/GROUP=GENDER;
SAS ESSENTIALS -- Elliott & Woodward

44 Add more code…(after the GROUP=GENDER; line)
KEYLEGEND / LOCATION=INSIDE POSITION=TOPLEFT; XAXIS LABEL="Race“; Notice Notice SAS ESSENTIALS -- Elliott & Woodward

45 Creating a scatterplot Using SGPLOT
PROC SGPLOT DATA =sasfilename; SCATTER X=varnamel Y=varname2; RUN; An option that is helpful for the SCATTER plot type is MARKERATTRS= (specifications) . This option allows you to specify the type of symbol displayed at each point, along with size and color. For example, MARKERATTRS=(symbol=trianglefilled size=lOpx color=red); Do Hands On Example p 425 (SGPLOT3.SAS) SAS ESSENTIALS -- Elliott & Woodward

46 Scatterplot Example PROC SGPLOT DATA =ANCOVA; SCATTER X=PRETEST Y=POSTTEST; RUN; SAS ESSENTIALS -- Elliott & Woodward

47 Change code (add /GROUP=METHOD)
SCATTER X=PRETEST Y=POSTTEST/GROUP=METHOD; Dots are two different colors according to group. SAS ESSENTIALS -- Elliott & Woodward

48 Change the shape and size of the “dot” to a filled triangle.
Change code to SCATTER X=PRETEST Y=POSTTEST/GROUP=METHOD MARKERATTRS=(symbol=trianglefilled size=10px); Change the shape and size of the “dot” to a filled triangle. SAS ESSENTIALS -- Elliott & Woodward

49 Add these lines (before the RUN;)
KEYLEGEND / LOCATION=INSIDE POSITION=TOPLEFT; XAXIS LABEL="Pre Test Results"; YAXIS LABEL="Post Test Results"; Legend added SAS ESSENTIALS -- Elliott & Woodward

50 Add this line (before RUN statement)
REG X=PRETEST Y=POSTTEST/ GROUP=METHOD; Adds regression lines by group SAS ESSENTIALS -- Elliott & Woodward

51 Final Code for Scatterplot with Regression Lines
PROC SGPLOT DATA =ANCOVA; SCATTER X=PRETEST Y=POSTTEST/GROUP=METHOD MARKERATTRS=(symbol=trianglefilled size=10px); KEYLEGEND / LOCATION=INSIDE POSITION=TOPLEFT; XAXIS LABEL="Pre Test Results"; YAXIS LABEL="Post Test Results"; REG X=PRETEST Y=POSTTEST/ GROUP=METHOD; RUN; SAS ESSENTIALS -- Elliott & Woodward

52 Using SGPLOT to create a Bubble Plot
Do Hands on Exercise p 427 (SGPLOT4.SAS) PROC SGPLOT DATA=MYSASLIB.CORONARY; TITLE 'BUBBLE PLOT'; TITLE2 'Bubble is BMI'; BUBBLE X=AGE Y=SBP SIZE=BMI / TRANSPARENCY=0.5; YAXIS GRID; RUN; Determines size of bubble Determines transparency of bubble. SAS ESSENTIALS -- Elliott & Woodward

53 Results of Bubble Plot SAS ESSENTIALS -- Elliott & Woodward

54 Continue Exercise… Change the TRANSPARENCY to 0.9 and run the code. Change it to 0.1 and run the code. Change the transparency back to 0.5. Add the option GROUP=INSURANCE after the TRANSPARENCY statement (and before the semicolon). Rerun and note the different colors by group. Change the option in the BUBBLE statement to /TRANSPARANCY=O.5 FILLATTRS=(COLORS=GREEN) to change the color of the bubbles to green. SAS ESSENTIALS -- Elliott & Woodward

55 18.9 OTHER WAYS TO CUSTOMIZE PLOTS
There are at least two other techniques you can use to customize plots: (1) Annotation Data Sets and (2) Graph Template Language (GTL): Annotation Data Set – a data set that contains graphic commands that you then apply to an existing plot (or create an entirely custom plot GTL - is a SAS sub-language used to specify the details of how an ODS graph is created Both of these techniques are beyond the scope of this book. (See SAS Documentation for more information.) SAS ESSENTIALS -- Elliott & Woodward

56 18.10 SUMMARY This chapter described a small portion of the many graphical options available in SAS. However, the discussion of graphs in this chapter (and elsewhere in the book) covered many of the common graphs used in data analysis. Continue to Chapter 19: CREATING CUSTOM REPORTS SAS ESSENTIALS -- Elliott & Woodward

57 These slides are based on the book:
Introduction to SAS Essentials Mastering SAS for Data Analytics, 2nd Edition By Alan C, Elliott and Wayne A. Woodward Paperback: 512 pages Publisher: Wiley; 2 edition (August 3, 2015) Language: English ISBN-10:  X ISBN-13:  These slides are provided for you to use to teach SAS using this book. Feel free to modify them for your own needs. Please send comments about errors in the slides (or suggestions for improvements) to Thanks. SAS ESSENTIALS -- Elliott & Woodward


Download ppt "Introduction to SAS Essentials Mastering SAS for Data Analytics"

Similar presentations


Ads by Google