Controlling Output Appearance with Templates

A template is a description of how output should appear when it is formatted. Templates describe several characteristics of the output, including headers, column ordering, style information, justification, and formats. Each table in the output has a template, and all SAS templates are stored in the Sashelp library. You can find the template associated with a particular output table or column by using the ODS TRACE statement or the SAS Results window. You can create or modify a template with the TEMPLATE procedure. For example, you can specify different column headings or different orders of columns in a table.

There are a number of different types of templates including column and table templates, graphical templates, and style templates. A column or table template applies to the specific columns or tables that refer to the template. Graphical templates are discussed in more detail in Chapter 21, Statistical Graphics Using ODS. A style template applies to an entire SAS program, including all tables and graphs, and can be specified with the STYLE= option in a valid ODS destination, such as HTML, RTF, or PDF. You can specify a style as follows:

ods html style=HTMLBlue;

A style template controls stylistic elements such as colors, fonts, and presentation attributes. You can change the style to give your output different looks and color schemes. You can also refer to style information in table templates for individual headers and data cells. You can modify all types of templates with PROC TEMPLATE. For information about creating your own styles, see the SAS Output Delivery System: User's Guide.

You can display the contents of a template by running PROC TEMPLATE with a SOURCE statement and a template name, as in the following example:

proc template;
   source Stat.REG.ANOVA;
   source Stat.GLM.OverallANOVA;
run;

In many cases, a template definition is based at least in part on another template. When you see the PARENT=template option in a template definition, you need to look at the specified template to learn more about the rest of the template definition. To illustrate, consider the following PROC GLM step:

proc glm data=sashelp.class;
   model height=weight;
run; quit;

The ANOVA table from this step is displayed in Figure 20.6.

Figure 20.6 PROC GLM ANOVA Table with the Default Template
Getting Started with ODS

The GLM Procedure
 
Dependent Variable: Height

Source DF Sum of Squares Mean Square F Value Pr > F
Model 1 364.5762619 364.5762619 57.08 <.0001
Error 17 108.5879486 6.3875264    
Corrected Total 18 473.1642105      


The sums of squares and mean squares are presented with eight decimal places. You can change the templates to change the formats of those columns to use fewer decimal places. First, you can use the ODS TRACE statement when you run PROC GLM to determine the name of the template:

ods trace output;
proc glm data=sashelp.class;
   model height=weight;
run; quit;
ods trace off;

The trace output results include the following:

   Output Added:
   -------------
   Name:       OverallANOVA
   Label:      Overall ANOVA
   Template:   stat.GLM.OverallANOVA
   Path:       GLM.ANOVA.Height.OverallANOVA
   -------------

From this, you can see that the template for the overall ANOVA table is stat.GLM.OverallANOVA. You can submit the following statements to see the overall ANOVA table template:

proc template;
   source stat.glm.overallanova;
run;

The results are as follows:

   define table Stat.GLM.Overallanova;
      notes "Over-all ANOVA";
      top_space = 1;
      parent = Stat.GLM.ANOVA;
      double_space;
   end;

The results show that this template inherits its definition from a parent template named Stat.GLM.ANOVA. Submit the following statements to see the parent template:

proc template;
   source stat.glm.anova;
run;

Some of the results are as follows:

   define SS;
      parent = Stat.GLM.SS;
   end;

   define MS;
      parent = Stat.GLM.MS;
   end;

These columns inherit their definitions from the parent columns named Stat.GLM.SS and Stat.GLM.MS. This is all of the information that you need to redefine these columns, but you can run PROC TEMPLATE again as follows to see more information about how these templates are defined:

proc template;
   source Stat.GLM.SS;
   source Stat.GLM.MS;
run;

The results are as follows:

   define column Stat.GLM.Ss;
      notes "Parent for GLM ANOVA Sums of Squares columns";
      parent = Common.ANOVA.SS;
   end;
   define column Stat.GLM.Ms;
      notes "Parent for GLM ANOVA Mean Squares columns";
      parent = Common.ANOVA.MS;
   end;

These columns inherit their definitions from the columns named Common.ANOVA.SS and Common.ANOVA.MS. You can run PROC TEMPLATE as follows to see their definitions:

proc template;
   source Common.ANOVA.SS;
   source Common.ANOVA.MS;
run;

The results are as follows:

   define column Common.ANOVA.Ss;
      notes "Default ANOVA Sum of squares column";
      header = "Sum of Squares";
      translate _val_=._ into "";
   end;
   define column Common.ANOVA.Ms;
      notes "Default ANOVA Mean square column";
      header = "Mean Square";
      translate _val_=._ into "";
   end;

You can redefine Common.ANOVA.SS and Common.ANOVA.MS to change all SS and MS columns in ANOVA tables. This would be the most general redefinition. More specifically, you can redefine Stat.GLM.SS and Stat.GLM.MS to change SS and MS columns in ANOVA tables produced by PROC GLM. Finally, and most specifically, you can change the SS and MS columns in just the overall ANOVA table template.


In this example, the Stat.GLM.SS and Stat.GLM.MS columns are redefined as follows, so that results are displayed with fewer decimal places:

proc template;
   edit Stat.GLM.SS;
      choose_format=max format_width=8;
   end;
   edit Stat.GLM.MS;
      choose_format=max format_width=8;
   end;
run;

The CHOOSE_FORMAT=MAX option along with the FORMAT_WIDTH=8 option chooses the format for each column based on the maximum value in that column and an overall width of eight. You are editing and not replacing the definition, so the column header and other information in the definition is not lost. The following step uses the new templates:

proc glm data=sashelp.class;
   model height=weight;
run; quit;

The new ANOVA results, using the edited templates, are shown in Figure 20.7. You can see that the original results in Figure 20.6 have eight decimal places, whereas the new results in Figure 20.7 have only five decimal places and an overall format width of eight.

Figure 20.7 PROC GLM ANOVA Table after Template Customization
Getting Started with ODS

The GLM Procedure
 
Dependent Variable: Height

Source DF Sum of Squares Mean Square F Value Pr > F
Model 1 364.5763 364.5763 57.08 <.0001
Error 17 108.5879 6.3875    
Corrected Total 18 473.1642      

The preceding PROC TEMPLATE step produces the following notes:
   NOTE: Overwriting existing template/link: Stat.GLM.Ss
   NOTE: COLUMN 'Stat.GLM.Ss' has been saved to: SASUSER.TEMPLAT
   NOTE: Overwriting existing template/link: Stat.GLM.Ms
   NOTE: COLUMN 'Stat.GLM.Ms' has been saved to: SASUSER.TEMPLAT


When you run PROC TEMPLATE to modify or edit a template, the template is stored by default in your Sasuser library. You can then modify the template search path with the ODS PATH statement—for example, so you can access these new templates in a later SAS session. This enables you to create a new default set of templates to modify the display format for all of your SAS output. You can specify the SHOW option in the ODS PATH statement to determine the current template search path. The following statements illustrate the template search path:

ods path show;
libname mytpls '.';
ods path (prepend) mytpls.template(update);
ods path show;

proc template;
   edit Stat.GLM.SS;
      choose_format=max format_width=8;
   end;
   edit Stat.GLM.MS;
      choose_format=max format_width=8;
   end;
run;

The results of the first statement are as follows:

   Current ODS PATH list is:

   1. SASUSER.TEMPLAT(UPDATE)
   2. SASHELP.TMPLMST(READ)

This shows that the Sasuser.Templat template store is open for storing new templates and retrieving templates for use. After that, the Sashelp.Tmplmst template store is used, but it is open only for read access. You cannot modify templates in Sashelp. The LIBNAME and second ODS PATH statements add a template store to the front of this list in the current directory. The final ODS PATH SHOW statement shows the new template search path, which is as follows:

   Current ODS PATH list is:

   1. MYTPLS.TEMPLATE(UPDATE)
   2. SASUSER.TEMPLAT(UPDATE)
   3. SASHELP.TMPLMST(READ)

The PROC TEMPLATE step produces the following notes, which show that the templates are now stored in MYTPLS.TEMPLATE:

   NOTE: Overwriting existing template/link: Stat.GLM.Ss
   NOTE: COLUMN 'Stat.GLM.Ss' has been saved to: MYTPLS.TEMPLATE
   NOTE: Overwriting existing template/link: Stat.GLM.Ms
   NOTE: COLUMN 'Stat.GLM.Ms' has been saved to: MYTPLS.TEMPLATE


In all cases, the original template definitions in Sashelp.Tmplmst are not changed. You can delete your custom template and restore the default template as follows:

proc template;
   delete Stat.GLM.SS;
   delete Stat.GLM.MS;
run;

It is good practice to delete any template redefinitions that you do not want to be permanent, because otherwise they persist beyond the duration of your SAS session.