Previous Page | Next Page

Using the Output Delivery System

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 reference the template. Graphical templates are discussed in more detail in Chapter 21, Statistical Graphics Using ODS. A style template applies to an entire SAS job, 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=Statistical;

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 reference 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;

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

   proc glm data=x;
      model y = x;
   run; quit;

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

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

The GLM Procedure
 
Dependent Variable: y

Source DF Sum of Squares Mean Square F Value Pr > F
Model 1 77.05005975 77.05005975 190.53 <.0001
Error 8 3.23526554 0.40440819    
Corrected Total 9 80.28532529      

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=x;
      model y = x;
      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.y.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;

Here are the results:

   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 there 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 there 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 FORMAT_WIDTH=8 chooses the format for each column based on the maximum value in that column and an overall width of 8. Note that you are editing and not replacing the definition, so column header and other information in the definition is not lost. The following step uses the new templates:

   proc glm data=x;
      model y = x;
   run; quit;

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

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

The GLM Procedure
 
Dependent Variable: y

Source DF Sum of Squares Mean Square F Value Pr > F
Model 1 77.05006 77.05006 190.53 <.0001
Error 8 3.23527 0.40441    
Corrected Total 9 80.28533      

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 by default stored in your SASUSER library. You can then modify the path that ODS uses to look up templates with the ODS PATH statement—for example, to 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 path. The following statements illustrate the ODS 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 file is open for storing new templates and retrieving templates for use. After that, the SASHELP.TMPLMST file is used, but it is open only for Read access. You cannot modify templates in SASHELP. The second and third statements add a template library to the front of this list in the current directory. The fourth statement shows the new path list, 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 showing 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 will persist beyond the duration of your SAS session.


Previous Page | Next Page | Top of Page