Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to SAS Essentials Mastering SAS for Data Analytics Alan Elliott and Wayne Woodward SAS ESSENTIALS -- Elliott & Woodward1."— Presentation transcript:

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

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

3 LEARNING OBJECTIVES SAS ESSENTIALS -- Elliott & Woodward3 To be able to use SAS® to create scatterplots and line graphs using GPLOT To be able to use SAS to create charts using PROC GCHART To be able to use SAS to create box plots (box-and- whiskers plots) using PROC BOXPLOT To be able to create basic plots using SGPLOT

4 18.1 CREATING SCATTERPLOTS AND LINE GRAPHS USING GPLOT SAS ESSENTIALS -- Elliott & Woodward4  PROC GPLOT is the basic routine used to obtain several types of graphs in SAS.  This section illustrates how to create scatterplots and line graphs using PROC GPLOT.  The abbreviated syntax for this procedure is as follows: PROC GPLOT ; PLOT plot-request(s)</ option(s)>;

5 GOPTIONS Before, Quit After SAS ESSENTIALS -- Elliott & Woodward5  It is a good idea to include the following command to reset all SAS graphics options before you use PROC GPLOT or other SAS graph procedures: GOPTIONS RESET=ALL;  Moreover, it is a good practice to include a QUIT statement after a plot to tell the SAS program that the graph is complete. QUIT;

6 The PLOT Statement SAS ESSENTIALS -- Elliott & Woodward6  The PLOT statement is the heart of the PROC GPLOT. Using this statement, you request an X- Y plot from the PROC GPLOT procedure using code such as the following: PROC GPLOT DATA=sasdatafile; PLOT yvariable*xvariable/options; RUN; The PLOT statement specifies the Y-axis (yvariable) and the X-Axis (xvariable)

7 SAS ESSENTIALS -- Elliott & Woodward7 Table 18.1 Common Options for PROC GPLOT OptionExplanation DATA = dataname Specifies which data set to use. ANNOTATE= Allows you to change default graphical elements or add new elements to the graph. (This feature is not demonstrated in this chapter.) UNIFORM Specifies that the same axes be used in all of the graphs produced by the procedure.

8 SAS ESSENTIALS -- Elliott & Woodward8 Table 18.2 Common Statements for PROC GPLOT Statement Explanation PLOT (and PLOT2) Specifies information about vertical and horizontal axes in a scatter/line plot, along with statement options that customize the look of the plot. These statements are discussed below. (PLOT2 is not discussed in this chapter) BUBBLE (and BUBBLE2) Similar to the PLOT statement, but specifies information that creates a bubble plot. (An example BUBBLE is given in the SGGRAPH section in this chapter.) AXIS, FOOTNOTE, PATTERN, TITLE These are statements that can be used to enhance graphs. Examples in this chapter illustrate these options. BY, FORMAT, LABEL, WHERE These statements are common to most procedures, and may be used here. Note: At least one PLOT or BUBBLE statement is required.

9 YVARIABLE and XVARIABLE SAS ESSENTIALS -- Elliott & Woodward9  yvariable defines the (left) vertical axis  xvariable defines the horizontal axis.  These can be either numeric or character variables. If you specify PLOT yvariable*xvariable=n;  n indicates which symbol to use for the plot points. (See Appendix A)  Request multiple plots by including more than one variable within parentheses, such as in PLOT (yvarl yvar2)*xvariable;  This statement produce two plots, one that is yvarl*xvariable and the other that is yvar2*xvariable.  You could specify the same series of plots using PLOT yvarl*xvariable yvar2*xvariable;

10 More about the PLOT statement SAS ESSENTIALS -- Elliott & Woodward10  Another form of the PLOT statement allows you to use a third variable that is a grouping (classification) variable. For example, PLOT yvariable*xvariable=group;  where group is some grouping variable. This will cause the points on the plot to be styled differently for each value of the grouping variable.  Options that appear after the PLOT specification are used to enhance the plot For example, PLOT yvariable*xvariable/CAXIS=color;  would cause the axes in the plot to be display in the specified color.

11 Creating a Simple Scatterplot SAS ESSENTIALS -- Elliott & Woodward11  Do Hands On Example p 395 (AGRAPH1.SAS) GOPTIONS RESET=ALL; Title "Simple Scatterplot"; PROC GPLOT DATA=MYSASLIB.CARS; PLOT HWYMPG*ENGINESIZE; RUN; QUIT; Requests a plot with HWMPG as the Y-Axis and ENGINESIZE as the X-AXIS

12 Resulting Scatterplot SAS ESSENTIALS -- Elliott & Woodward12

13 Make Modifications' SAS ESSENTIALS -- Elliott & Woodward13  Modify the PLOT the statement to make text RED: PLOT HWYMPG*ENGINESIZE/CTEXT=RED;

14 Managing Your SAS Graph – PLOT Statement (part 1) SAS ESSENTIALS -- Elliott & Woodward14 Table 18.3 Common Options for PROC GPLOT’s PLOT Statement (These options appear after a /) OptionExplanation CAXIS=color Specifies a color to use for the axis line and tick marks. (i.e. CAXIS=BLUE; ) See Appendix A. CFRAME= color Specifies frame color. CTEXT= color Specifies color for text. See Appendix A. HAXIS= or VAXIS= Specifies information about the horizontal or vertical axis. For example HAXIS=AXISk – Indicates which AXISk definition to apply to the horizontal axis. (i.e. HAXIS=AXIS1 ) VAXIS=AXISk - Indicates which AXISk definition to apply to the vertical axis. (i.e. VAXIS=AXIS2 )

15 Managing Your SAS Graph – PLOT Statement (part 1) SAS ESSENTIALS -- Elliott & Woodward15 Table 18.3 Common Options for PROC GPLOT’s PLOT Statement (These options appear after a /) (Part 2) OptionExplanation HREF=value list or VREF= value list Draws a horizontal or vertical line at the indicated axis value or values. LEGEND=legend Specifies which legend definition to use for a plot. An example is given in the chapter. NOLEGEND Suppresses the standard plot legend SYMBOL Specifies which symbol & attributes to use for points. See Appendix A. NOFRAME Suppresses the (default) frame around the plot GRID Draws grid line at major tick marks.

16 Using the Graph Your Create SAS ESSENTIALS -- Elliott & Woodward16  Copy Graph: Select Edit  Copy from the menu bar to copy the graph to the Clipboard (Then paste it into Word, Power Point, etc).  Export Graph: Right-click on the graph, select Save Picture as…, and select Files of Type to export the graph to a standard graphics format file. (PNG and BIT)  Print Graph: Use the File  Print menu option to print a hard copy of the graph. (or select Right-click, Print Picture …)

17 Enhancing a GPLOT Using the SYMBOL statement SAS ESSENTIALS -- Elliott & Woodward17  Often, the purpose of displaying a scatterplot is to illustrate an association between two variables.  The abbreviated syntax for the SYMBOL statement is as follows: SYMBOL ;  An example statement is SYMBOLl V=STAR I=RL C=BLUE L=2;

18 SAS ESSENTIALS -- Elliott & Woodward18 Table 18.4 Common Options for PROC GPLOT’s SYMBOL Statement OptionExplanation COLOR= color (or C=) Specify color for the symbol. For example, C=BLUE. See Appendix A for more colors. INTERPOLATION=type (or I=) Specifies an interpolation curve in the plot. Common options are: RL – Linear Regression RQ - Quadratic regression RC - Cubic regression STDk -Standard error bars of length k STDEV. STDM -Uses standard error of the mean JOIN -Joins dots BOXT – Specifies a high-low interpolation LINE=n(or L=) Indicates the pattern of the line drawn in the graph. For example, LINE=1 produces is a solid line. A value 2 value produces small dots, 3 is larger dots, and so on. See Appendix A for other options. VALUE=n (or V=) Indicates the symbol used on the plot. For example, V=STAR See Appendix A for other options.

19 Do Hands On Exercise p 399 (AGRAPH2.SAS) SAS ESSENTIALS -- Elliott & Woodward19 GOPTIONS RESET=ALL; Title ‘Simple Scatterplot’; PROC GPLOT DATA=MYSASLIB.CARS PLOT HWYMPG*CITYMPG; SYMBOLl V=STAR I=RL C=BLUB L=2; RUN; QUIT; Specify variables for plot Customize the symbol for the dots, and define an interpolation (I=RL. Linear Regression)

20 What the SYMBOL Statement Specifies SAS ESSENTIALS -- Elliott & Woodward20 SYMBOLl V=STAR I=RL C=BLUE L=2; The displayed symbol is a star (V=STAR). The displayed symbol is blue (C=BLUE). A linear regression line is displayed (I=RL). The displayed line is dotted (L=2).

21 Resulting Plot SAS ESSENTIALS -- Elliott & Woodward21

22 Customizing Axes in a Plot SAS ESSENTIALS -- Elliott & Woodward22  To change the characteristics of the plot axes, use the AXIS statement: AXIS ;  where n defines options for a specific axis such as AXISl, AXIS2, and so on. Applying an axis definition to a plot requires the following two steps: Step 1: Define the axis using an AXIS statement. Step 2: Associate the defined axis to a specific axis in the plot. GPLOT and GCHART

23 Using an AXIS Statement SAS ESSENTIALS -- Elliott & Woodward23  Axis options define the "look" of the axis: its scale, appearance, tick marks, and text. This is step 1 in using an axis: AXIS1 C=RED ORDER=(O TO 80 BY 5) LABEL=('City Miles Per Gallon' FONT=SWISS) OFFSET=(S) WIDTH=2 VALUE=(H=2);  Apply this axis to a plot (step 2 in using an axis): PROC GPLOT DATA=CARS; PLOT HWYMPG*CITYMPG/HAXIS=AXIS1; Apply AXIS1 to the plot like this.

24 SAS ESSENTIALS -- Elliott & Woodward24 Table 18.5 AXIS names for GPLOT and GCHART Axis Name for GPLOTRefers to which axis? HAXIS HAXIS=AXIS applies to the horizontal axis, also called the X axis in a standard X-Y plot. VAXISVAXIS=AXIS applies vertical axis, also called the Y axis in a standard X-Y plot.. Axis Name for GCHART Refers to MAXIS= MAXIS=AXIS applies to the midpoint axis, which is the axis associated with a group (categorical) specification. GAXIS= GAXIS=AXIS applies to the group axis, when you use a GROUP= statement (see example below). RAXIS= RAXIS=AXIS applies to the response axis, which is the axis associated with the count, percent, or frequency.

25 SAS ESSENTIALS -- Elliott & Woodward25 Table 18.6 Common Options for PROC GPLOT’s AXIS Statement (Part 1) OptionExplanation COLOR=color (or C=) Indicates the color used for the axis in the plot. For example, C=BLUE causes the axis to be blue. See Appendix A. LABEL=(specifications)Specifies characteristics of an axis label. The text arguments define specific parts of the label’s appearance. An example of a label statement is LABEL=(H=4 J=C C=BLUE "My Axis"); where H= 4 specifies Height of the text J=C means Justify Center C=BLUE specifies color of label text ORDER=(list) Specifies the order of the values on the axis. For example, ORDER=(1 to 100 by 5) specifies that the axis will contain the numbers 1 to 100 with axis label increments of 5. VALUE=(options) Specifies the height of text (among other items)—for example, VALUE=(H=4).

26 SAS ESSENTIALS -- Elliott & Woodward26 Table 18.6 Common Options for PROC GPLOT’s AXIS Statement (Part 2) OptionExplanation WIDTH=thickness Specifies width of the axis (affects the width of the entire frame unless a NOFRAME option is used as defined below). OFFSET=(x,y) Moves the axis away from the edge by a certain amount. For example OFFSET=(3,4) moves the x axis 3 percent horizontally, and the y axis 4 percent vertically. MAJOR=(options)Controls major tick marks. For example MAJOR=(COLOR=BLUE HEIGHT=3 WIDTH=2) MINOR=(options)Controls minor axis tick mark. MINOR=NONE causes no minor ticks to be displayed. For example MINOR=(NUMBER=4 COLOR=RED HEIGHT=1) JUSTIFICATION=type (J=)Justification, L=Left, C=Center, R=Right A=nSpecifies text angle. For example, use A=90 to place a horizontal axis at a 90 degree angle.

27 Note About Graphics in SAS SAS ESSENTIALS -- Elliott & Woodward27  For SAS version 9.3 and later, you may need to turn off automatic ODS graphics (which is the default in those versions) for some of the upcoming graph examples to work (particularly the colors). To turn off ODS graphics: ODS GRAPHICS OFF;  Turn graphics back on with ODS GRAPHICS ON;  Another way to return ODS graphics back to their normal default state in SAS versions 9.3 and later is ODS PREFERENCES;

28 Do Hands On Exercise p 402 SAS ESSENTIALS -- Elliott & Woodward28 AXIS1 C=RED ORDER=(0 TO 80 BY 5) LABEL=('City Miles Per Gallon' FONT=SWISS) OFFSET=(5) WIDTH=2 VALUE=(H=2); AXIS2 C=GREEN ORDER=(0 TO 80 BY 5) LABEL=(A=90 'Highway Miles Per Gallon' FONT=SWISS) OFFSET=(5) WIDTH=4 VALUE=(H=1); TITLE "Enhanced Scatterplot"; PROC GPLOT DATA="C:\SASDATA\CARS"; PLOT HWYMPG*CITYMPG/HAXIS=AXIS1 VAXIS=AXIS2 NOFRAME; SYMBOL1 V=DOT I=RL C=PURPLE L=2; RUN; QUIT;

29 Resulting Graph SAS ESSENTIALS -- Elliott & Woodward29

30 Make Some changes in the Code: SAS ESSENTIALS -- Elliott & Woodward30 AXIS1 ORDER=(0 TO 80 BY 5) LABEL=('City Miles Per Gallon' FONT=SWISS) OFFSET=(2) WIDTH=4 VALUE=(H=1); AXIS2 ORDER=(0 TO 80 BY 5) LABEL=(A=90 'Highway Miles Per Gallon' FONT=SWISS) OFFSET=(2) WIDTH=4 VALUE=(H=1); TITLE "Enhanced Scatterplot"; PROC GPLOT DATA=mysaslib.CARS; PLOT HWYMPG*CITYMPG/HAXIS=AXIS1 VAXIS=AXIS2 ; SYMBOL1 V=DOT I=RL C=GRAY L=5; RUN; QUIT;

31 Resulting Graph SAS ESSENTIALS -- Elliott & Woodward31

32 Displaying Error Bars in GPLOT SAS ESSENTIALS -- Elliott & Woodward32  To display data by groups with error bars on the graph to indicate the variability of data within a group, use the I= option in the SYMBOL statement. For example, SYMBOL INTERPOLATION=specification (or I= )  To employ this option to display error bars, use one of the following techniques: I=STD1JT produces error bars that are 1 standard deviation (STD1) and that are joined (J) at the means with tops (T) on each bar. STD also supports values of 2 and 3. I=BOXT includes box plots with tops (T) on each whisker.

33 Do Hands On Example p 403 (AGRAPH3.SAS) SAS ESSENTIALS -- Elliott & Woodward33 AXIS1 ORDER=(4 TO 12 BY 1) LABEL=('Number of Cylinders' FONT=SWISS HEIGHT=4)OFFSET=(5); AXIS2 ORDER=(0 TO 50 BY 5) LABEL=(A=90 'Highway Miles Per Gallon' FONT=SWISS HEIGHT=4) OFFSET=(5); PROC GPLOT DATA=mysaslib.cars; PLOT HWYMPG*CYLINDERS /HAXIS=AXIS1 VAXIS=AXIS2; SYMBOL1 I=STD1JT H=1 LINE=1 VALUE=NONE; RUN; QUIT; Note Axis numbers defined

34 Resulting Graph SAS ESSENTIALS -- Elliott & Woodward34 These bars are the result of the SYMBOL1 I=STD1JT… Statement.

35 Change Statement to SYMBOL1 I=BOXT etc… SAS ESSENTIALS -- Elliott & Woodward35 The I=BOXT option displays box plots.

36 18.2 CREATING BAR CHARTS AND PIE CHARTS SAS ESSENTIALS -- Elliott & Woodward36  Produce bar and pie charts with the SAS GCHART procedure. The abbreviated syntax is as follows: PROC GCHART ; HBAR | HBAR3D | VBAR | VBAR3D chartvar(s) ; PIE | PIE3D | DONUT | STAR chartvar(s) ;  Chart-type specifications such as HBAR and VBAR refer to horizontal and vertical bar charts, pie charts, and three-dimensional (3D) versions of the charts. An example is PROC GCHART DATA=MYDATA; HBAR STATUS/DISCRETE COUTLINE=BLUE WOUTLINE=3;

37 SAS ESSENTIALS -- Elliott & Woodward37 Table 18.7 Common Options for PROC GCHART OptionsExplanation DATA=dataname Specifies which data set to use. HBAR, HBAR3D, VBAR, VBAR3D, PIE, PIE3D, DONUT, or STAR Specify which type of plot to create. AXIS, FOOTNOTE, PATTERN, TITLE These are statements that can be used to enhance graphs. Examples in this chapter illustrate these options. BY, FORMAT, LABEL, WHERE These statements are common to most procedures, and may be used here.

38 SAS ESSENTIALS -- Elliott & Woodward38 Table 18.8 Common GCHART Options for Bar Charts (Options appear after /.) (Part 1) OptionExplanation CAXIS=color CFRAME=color CTEXT=color Specifies a color to use for the axis, frame or text. For example CAXIS=BLUE. COUTLINE=color WOUTLINE=n COUTLINE specifies color for bar outline (or SAME) and WOUTLINE specifies outline width. Default width is 1. For example COUTLINE=BLUE WOUTLINE=3 DISCRETE Treat numeric values as discrete (categorical) GROUP variable Split bars into groups using specified classification variable SUBGROUP variable Specify qualities of bars according to subgroups within the group defined by the GROUP variable (i.e. display a different color for each subgroup.) WIDTH=n SPACE=n WIDTH specifies BAR WIDTH and SPACE= specifies the space between bars.

39 SAS ESSENTIALS -- Elliott & Woodward39 Table 18.8 Common GCHART Options for Bar Charts (Options appear after /.) (Part 2) OptionExplanation INSIDE=statistic OUTSIDE=statistic Determines where a statistic will be displayed in a chart. Statistics are MEAN, SUM, PERCENT (PCT) or FREQ. TYPE=statistic Defines the statistic to be computed (to display on the chart.) Options include SUM, MEAN, PERCENT (PCT) and FREQ(default.) SUMVAR=varname Defines the response variable. When you used this option TYPE= should be MEAN or SUM NOLEGEND Suppresses Chart Legend CLM=p Specify the level for a confidence interval to be displayed on the bars. For example CLM=95. ERRORBAR=type Specify TOP, BARS, or BOTH.

40 Do Hands On Example p 406 (AGRAPH4.SAS) SAS ESSENTIALS -- Elliott & Woodward40 GOPTIONS RESET=ALL; Title "Horizontal Bar Chart"; PROC GCHART DATA=MYSASLIB.SOMEDATA HBAR STATUS /DISCRETE; RUN; QUIT; The /DISCRETE option tells SAS that the numeric values for the STATUS variable should be treated as discrete (individual) values.

41 Resulting Bar Chart SAS ESSENTIALS -- Elliott & Woodward41

42 Try Other Options, Observe Results SAS ESSENTIALS -- Elliott & Woodward42 Make this change and rerun. VBAR STATUS/DISCRETE WIDTH=10;  Make this change and rerun. HBAR STATUS/DISCRETE; PATTERN1 VALUE=R1 COLOR=GREEN;  Make this change and rerun. (Remove DISCRETE) HBAR STATUS;

43 18.3 DEFINING GRAPH PATTERNS SAS ESSENTIALS -- Elliott & Woodward43  The PATTERN statement allows you to change the color and pattern of a bar in a bar chart. A simplified syntax for the statement is PATTERN ;  For example, PATTERN1 VALUE=Rl COLOR=GREEN;  The in the statement determines which category the pattern will be defined for, where the categories are the order in which categories are displayed for the chart. For example: PATTERN1 VALUE=SOLID C=LIGHTBLUE;

44 Do Hands On Example p 408 (AGRAPH5.SAS) SAS ESSENTIALS -- Elliott & Woodward44 AXIS1 LABEL=NONE VALUE=(H=l.2); AXIS2 LABEL=(A=90 "Count"); TITLE "Vertical Bar Chart"; PROC GCHART DATA=MYSASLIB.SOMEDATA; VBAR GENDER/GROUP=GP MAXIS=AXIS1 RAXIS=AXIS2; PATTERN1 VALUE=Rl COLOR=GREEN; RUN; QUIT; Note that AXIS= controls the frequency (vertical) axis for GCHART and MAXIS= controls the appearance of the midpoint (horizontal) axis.

45 Resulting Bar Chart SAS ESSENTIALS -- Elliott & Woodward45

46 Make These Changes, Rerun SAS ESSENTIALS -- Elliott & Woodward46 VBAR GENDER/GROUP=GP SUBGROUP=GENDER; PATTERN1 VALUE=Rl COLOR=RED; PATTERN2 VALUE=R1 COLOR=BLUE; Assign a different pattern to each value in the group to display the groups like this.

47 Change the statement in PATTERN1 to VALUE=X4 SAS ESSENTIALS -- Elliott & Woodward47 The X4 option results in a crosshatch pattern.

48 Change VBAR to VBAR3D SAS ESSENTIALS -- Elliott & Woodward48

49 Continue to Chapter 18, Part 2 SAS ESSENTIALS -- Elliott & Woodward49

50 SAS ESSENTIALS -- Elliott & Woodward50 These slides are based on the book: Introduction to SAS Essentials Mastering SAS for Data Analytics, 2 nd Edition By Alan C, Elliott and Wayne A. Woodward Paperback: 512 pages Publisher: Wiley; 2 edition (August 3, 2015) Language: English ISBN-10: 111904216X ISBN-13: 978-1119042167 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 acelliott@smu.edu. Thanks.


Download ppt "Introduction to SAS Essentials Mastering SAS for Data Analytics Alan Elliott and Wayne Woodward SAS ESSENTIALS -- Elliott & Woodward1."

Similar presentations


Ads by Google