Published on :
Graphic SASHELP

Extreme MPG Graph Generation

This code is also available in: Deutsch Español Français
The code first defines a `STATGRAPH` named 'selections' using `PROC TEMPLATE`. This template uses two dynamic variables, `upLimit` and `lowLimit`, to filter vehicles. The graph is a `layout lattice` with two cells, each containing a `scatterPlot`. The first cell displays vehicles with highway MPG greater than or equal to `upLimit`, while the second displays those with MPG less than or equal to `lowLimit`. The template is then rendered via `PROC SGRENDER`, using the `sashelp.cars` data and specifying values for `upLimit` (44) and `lowLimit` (16).
Data Analysis

Type : SASHELP


The script uses the internal `sashelp.cars` dataset, available by default in all SAS installations, to retrieve vehicle information (model, MSRP, highway MPG).

1 Code Block
PROC TEMPLATE
Explanation :
This block defines a reusable graph template named 'selections' using the `PROC TEMPLATE` procedure. It incorporates dynamic variables `upLimit` and `lowLimit` which will be used to filter the displayed data. The graph is structured as a two-row lattice arrangement, each presenting a scatter plot (`scatterPlot`). The first row visualizes vehicles whose highway consumption is greater than or equal to `upLimit`, while the second shows those whose consumption is less than or equal to `lowLimit`. Data point labels display the vehicle model.
Copied!
1PROC TEMPLATE;
2 define statgraph selections;
3 dynamic upLimit "low end of high mpg"
4 lowLimit "upper end of low mpg";
5 beginGraph / designHeight=600;
6 EntryTitle "Vehicles with Extreme Highway MPG (2004)";
7 layout lattice / rows=2 columnDataRange=union rowDataRange=union
8 rowGutter=10px;
9 columnAxes;
10 columnAxis / label="MSRP($)";
11 endColumnAxes;
12 
13 rowAxes;
14 rowAxis / label=eval(colLabel(mpg_highway));
15 rowAxis / label=eval(colLabel(mpg_highway));
16 endRowAxes;
17 cell;
18 cellHeader;
19 Entry "MPG " {unicode '2265'x} " " upLimit;
20 endCellHeader;
21 scatterPlot x=eval(ifn(mpg_highway >= upLimit, msrp, .))
22 y=eval(ifn(mpg_highway >= upLimit, mpg_highway, .)) /
23 group=make dataLabel=model dataLabelAttrs=(size=10);
24 endCell;
25 cell;
26 cellHeader;
27 Entry "MPG " {unicode '2264'x} " " lowLimit;
28 endCellHeader;
29 scatterPlot x=eval(ifn(mpg_highway <= lowLimit, msrp, .))
30 y=eval(ifn(mpg_highway <= lowLimit, mpg_highway, .)) /
31 group=make dataLabel=model dataLabelAttrs=(size=10);
32 endCell;
33 endLayout;
34 endGraph;
35 END;
36RUN;
2 Code Block
PROC SGRENDER
Explanation :
This block configures the ODS Graphics system for graph output, resetting parameters and defining the height. Then, `PROC SGRENDER` is used to render the 'selections' graph template previously defined. It uses the `sashelp.cars` dataset as the source and assigns specific values (44 for `upLimit` and 16 for `lowLimit`) to the template's dynamic variables. This generates an ODS graph that highlights vehicles with exceptionally high or low highway fuel consumption.
Copied!
1ods graphics / reset height=600px labelMax=600;
2PROC SGRENDER template=selections DATA=sashelp.cars;
3 dynamic upLimit=44 lowLimit=16 ;
4RUN;
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.