Published on :
Reporting SASHELP

Simple Statistics with Scatter Plot and Reference Lines

This code is also available in: Deutsch Español Français
This SAS© script defines a `STATGRAPH` template named `simpleStats` for data visualization. It generates a scatter plot displaying the relationship between the manufacturer's suggested retail price (msrp) and highway fuel consumption (mpg_highway), grouped by vehicle type. The graph is enhanced with reference lines: a vertical one indicating the median of `msrp`, and two horizontal ones for the mean of `mpg_highway` plus/minus one standard deviation. A legend is included for the type groups, and a dynamic footnote displays the min/max weight range of the vehicles. The template is then rendered using the `sashelp.cars` table.
Data Analysis

Type : SASHELP


The script uses the `cars` table from the internal `sashelp` library, a standard data source available in all SAS installations.

1 Code Block
PROC TEMPLATE
Explanation :
This block defines a reusable `STATGRAPH` template named `simpleStats`. It specifies the visual elements of the graph, including a title, an overlay layout to combine graphic elements, a scatter plot (`scatterPlot`) grouped by the `type` variable, and reference lines (`referenceLine`) dynamically calculated using statistical functions (`median`, `mean`, `std`) and the `eval` function. A discrete legend is added, as well as a footnote (`entryFootnote`) that displays summarized statistics (min and max of `weight`) from the data set being rendered. This template encapsulates the graph's presentation logic without processing the data at this stage, making it reusable.
Copied!
1PROC TEMPLATE;
2 define statgraph simpleStats;
3 beginGraph;
4 entryTitle "Simple Statistics using Expression";
5 layout overlay;
6 scatterPlot x=msrp y=mpg_highway / name="sp1"
7 group=type dataTransparency=0.2;
8 referenceLine x=eval(median(msrp)) / lineAttrs=(pattern=shortDash)
9 curveLabel="x(*ESC*){unicode '0303'x}" curveLabelAttrs=(size=12);
10 referenceLine y=eval(mean(mpg_highway) + std(mpg_highway)) /
11 curveLabel="x(*ESC*){unicode '0304'x} + (*ESC*){unicode sigma}"
12 curveLabelAttrs=(size=12) lineAttrs=(pattern=dashDashDot);
13 referenceLine y=eval(mean(mpg_highway) - std(mpg_highway)) /
14 curveLabel="x(*ESC*){unicode '0304'x} - (*ESC*){unicode sigma}"
15 curveLabelAttrs=(size=12) lineAttrs=(pattern=dashDashDot);
16 discreteLegend "sp1" / across=3 location=inside hAlign=right vAlign=top;
17 endLayout;
18 entryFootnote halign=left "Weight (in lbs) ranges from " eval(min(weight))
19 " to " eval(max(weight)) ;
20 endGraph;
21 END;
22RUN;
2 Code Block
PROC SGRENDER
Explanation :
This code block begins by resetting ODS graphics options (`ods graphics / reset;`) to ensure a clean graphics environment and avoid interference with previous settings. Then, the `PROC SGRENDER` procedure is used to generate the graph. It applies the `simpleStats` template previously defined (via `template=simpleStats`) to the data contained in the `sashelp.cars` table (via `data=sashelp.cars`). This produces the graph image with all the elements specified in the template, using the `msrp`, `mpg_highway`, `type`, and `weight` variables from the `cars` table for the final rendering.
Copied!
1ods graphics / reset;
2PROC SGRENDER
3DATA=sashelp.cars template=simpleStats;
4RUN;
5 
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.