Using the TMPLOUT= Option in SG Procedures

There are dozens of ways to create plots in SAS. Some are simple and intuitive, some are complex and hard to grasp. However, like most things in life, the hard stuff usually gives you more control in the long run.

This is also the case when we are dealing with statistical graphing in SAS. I like the SG procedures and especially the SGPLOT Procedure very much. They make simple graphs easy and complex graphs possible. However, I also like the Graph Template Language (GTL) and PROC SGRENDER because it gives me more control over the visual aspects of my graphs.

Unfortunately, the GTL is not as intuitive as the SGPLOT syntax. Even though PROC SGPLOT actually implicitly writes a template in the Graph Template Language and use it to produce a graph. However, there is a neat little option, that lets you ‘look under the hood’ of the SGPLOT Procedure and see what template the procedure generate. This is the TMPLOUT= Option.

SAS Code Example

Let us look at a simple example of how the TMPLOUT= Option works. First, I specify a path to a file directory and the .sas filename I want the generated template code to go. Next, I use the SGPLOT Procedure to draw a simple scatter plot from the sashelp.class data set. In the procedure statement, I specify the TMPLOUT= option with the path and file name from above. This creates a simple scatter plot and saves a .sas file with the GTL code that PROC SGPLOT implicitly generates and uses for creating the plot.

%let path=C:\Users\Peter\Desktop;
%let file=ScatterTemplate;
proc sgplot data=sashelp.class tmplout="&path.\&";
title "Simple Scatter Plot";
scatter y=weight x=height;

Next, let us take a look at the generated GTL code. I use the %include statement to run the code in the saved file. Also, I use OPTIONS NOSOURCE2 to display the included code in the log. Now, you can see the generated template in the log or you can open the saved .sas file to view or edit the template code.

options source2;
%include "&path.\&";

Finally, you can use PROC SGRENDER and specify the TEMPLATE= option with our generated template to draw the exact same (or modified) graph as you did with PROG SGPLOT above.

proc sgrender data=sashelp.class template=sgplot;

Obviously, this is a simplified example. However, it shows a very simple but effective way to see what goes on under the surface of the SGPLOT Procedure. I encourage you to draw some simple graph with PROC SGPLOT, use the TMPLOUT= Option to save the generated template to a file, make some change to the template and run PROC SGRENDER with the generated template and look at the new graph. Does it look the way you expected?


If you know the syntax of the SGPLOT Procedure, and you want to take it a step further and gain even more control over your graphs in SAS, the TMPLOUT= Option is a nice way to do so. It translate a syntax you know into a syntax that is not yet familiar to you, but when you have seen a few examples, it is actually quite intuitive.

Also, check out the related post SAS ODS Style Option.

You can download the entire code from this blog post here.