# Example 2: Defining Dynamic Variables

 Features: DYNAMIC Statement Sample library member: SGREND1
This example uses dynamic variables to set values within the StatGraph template. By using dynamic variables to set the variable names, variable labels, and other parameters, the StatGraph template can be used with different data sets.
The first PROC SGRENDER statement generates a graph for the SASHELP.HEART data set.
The second PROC SGRENDER statement generates multiple graph for the CARS data set by using BY grouping.

## Program Template and Heart Data

```proc template;
define statgraph distribution;
dynamic VAR VARLABEL TITLE NORMAL _BYLINE_;
begingraph;
entrytitle TITLE;
entrytitle _BYLINE_;
layout lattice / columns=1 rows=2  rowgutter=2px
rowweights=(.9 .1) columndatarange=union;
columnaxes;
columnaxis / label=VARLABEL;
endcolumnaxes;
layout overlay / yaxisopts=(offsetmin=.035);
layout gridded / columns=2 border=true autoalign=(topleft topright);
entry halign=left "Nobs";
entry halign=left eval(strip(put(n(VAR),8.)));
entry halign=left "Mean";
entry halign=left eval(strip(put(mean(VAR),8.2)));
entry halign=left "StdDev";
entry halign=left eval(strip(put(stddev(VAR),8.3)));
endlayout;
histogram VAR / scale=percent;
if (exists(NORMAL))
densityplot VAR / normal( );
endif;
fringeplot VAR / datatransparency=.7;
endlayout;
boxplot y=VAR / orient=horizontal;
endlayout;
endgraph;
end;
run;```
```proc sgrender data=sashelp.heart template=distribution;
dynamic var="cholesterol" varlabel="Cholesterol (LDL)" normal="yes"
title="Framingham Heart Study";
run;
title;```

## Program Description

Create the Statgraph template.
```proc template;
define statgraph distribution;
dynamic VAR VARLABEL TITLE NORMAL _BYLINE_;
begingraph;
entrytitle TITLE;
entrytitle _BYLINE_;
layout lattice / columns=1 rows=2  rowgutter=2px
rowweights=(.9 .1) columndatarange=union;
columnaxes;
columnaxis / label=VARLABEL;
endcolumnaxes;
layout overlay / yaxisopts=(offsetmin=.035);
layout gridded / columns=2 border=true autoalign=(topleft topright);
entry halign=left "Nobs";
entry halign=left eval(strip(put(n(VAR),8.)));
entry halign=left "Mean";
entry halign=left eval(strip(put(mean(VAR),8.2)));
entry halign=left "StdDev";
entry halign=left eval(strip(put(stddev(VAR),8.3)));
endlayout;
histogram VAR / scale=percent;
if (exists(NORMAL))
densityplot VAR / normal( );
endif;
fringeplot VAR / datatransparency=.7;
endlayout;
boxplot y=VAR / orient=horizontal;
endlayout;
endgraph;
end;
run;```
Generate the first graphics output from the template using the SASHELP.HEART data set. The DYNAMIC statement defines dynamic variables in the template.
```proc sgrender data=sashelp.heart template=distribution;
dynamic var="cholesterol" varlabel="Cholesterol (LDL)" normal="yes"
title="Framingham Heart Study";
run;
title;```

## Program for Grouped Cars Data

```proc sort data=sashelp.cars out=cars;
by origin;
run;```
```proc sgrender data=cars template=distribution;
by origin;
dynamic var="weight" varlabel="Weight in LBS"
title="Distribution of Vehicle Weight";
run;
title;```

## Program Description

Sort the SASHELP.CARS data set. The data set must be sorted by the same variable that the following PROC SGRENDER block uses in its BY statement.
```proc sort data=sashelp.cars out=cars;
by origin;
run;```
Generate the second graphics output from the template using the WORK.CARS data set. The BY statement generates multiple graphs for each value of the BY variable. The DYNAMIC statement defines dynamic variables in the template.
```proc sgrender data=cars template=distribution;
by origin;
dynamic var="weight" varlabel="Weight in LBS"
title="Distribution of Vehicle Weight";
run;
title;```