TEMPLATE Procedure: Creating a Style Template

Example 3: Using the CLASS Statement

Features:
DEFINE STYLE statement: :
User-defined attributes
BACKGROUNDCOLOR= style attribute
BORDERWIDTH= style attribute
CELLPADDING= style attribute
BORDERSPACING= style attribute
COLOR= style attribute
FONT= style attribute
FONTSTYLE= style attribute
FRAME= style attribute
POSTHTML= style attribute
RULES= style attribute
VISITEDLINKCOLOR= style attribute

CLASS statement

PARENT= statement

Other features:
Other ODS features::
ODS HTML statement: STYLE= option
ODS PATH statement
Data set: Energy
Format: DIVFMT. and USETYPE.

Program 1: Details

When you are working with styles, you are more likely to modify a SAS style than to write a completely new style. This example makes changes to the default style for the HTML destination. The new style affects both the contents file and the body file in the HTML output. In the contents file, the modified style makes changes to the following:
  • the text of the header and the text that identifies the procedure that produced the output
  • the colors for some parts of the text
  • the font size of some parts of the text
  • the spacing in the list of entries in the table of contents
In the body file, the modified style makes changes to the following:
  • two of the colors in the color list. Style1 of these colors is the foreground color for the table of contents, the byline, and column headings. The other is the foreground of many parts of the body file, including SAS titles and footnotes.
  • the font size for titles and footnotes.
  • the font style for headers.
  • the presentation of the data in the table by changing attributes such as border spacing, rules, and border width.
When you modify a style element in a new style that has a like-named style element in the parent style, then you must use the CLASS statement or the STYLE statement with the FROM option specified. This example uses the CLASS statement to produce a shorter, easier to read program.

Program 1: Using the Default Style with PROC PRINT

ods path sasuser.templat(update) sashelp.tmplmst(read);
ods html body="sasdefaultstyle-body.htm"
         contents="sasdefaultstyle-content.htm"
         frame="sasdefaultstyle-frame.htm";
title "Energy Expenditures for Each Region";
title2 "(millions of dollars)";
proc print data=energy noobs;
   var state type expenditures;
   format division divfmt. type usetype. expenditures comma12.;
   by division;
   where division=2 or division=3;
run;
ods html close;
ods html;

Program Description

Specify the search path in order to locate the table template. This statement specifies which locations to search for templates that were created by PROC TEMPLATE, as well as the order in which to search for them. The statement is included to ensure that the example works correctly. However, if you have not changed the path, then you do not need to include this statement because it specifies the default path.
ods path sasuser.templat(update) sashelp.tmplmst(read);
Create the HTML output and specify the name of the HTML file. The ODS HTML statement opens the HTML destination and creates HTML output. The output from PROC PRINT is sent to the body file. FRAME= and CONTENTS= create a frame that includes a table of contents that links to the contents of the body file. The body file also appears in the frame. No style is specified, so the default style, HTMLBlue, is used to format the output.
ods html body="sasdefaultstyle-body.htm"
         contents="sasdefaultstyle-content.htm"
         frame="sasdefaultstyle-frame.htm";
Specify the titles and footnote for the report. The TITLE and FOOTNOTE statements provide two titles and a footnote for the output.
title "Energy Expenditures for Each Region";
title2 "(millions of dollars)";
Print the report. PROC PRINT creates a report that includes three variables. ODS writes the report to the BODY file.
proc print data=energy noobs;
   var state type expenditures;
   format division divfmt. type usetype. expenditures comma12.;
   by division;
   where division=2 or division=3;
run;
Stop the creation of the HTML output. The ODS HTML statement closes the HTML destination and all the files that are associated with it. The ODS HTML statement opens the HTML destination to return ODS to its default setup.
ods html close;
ods html;

Output: HTML Output from PROC PRINT Using the Default Style

HTML Output from PROC PRINT Using the Default Style
HTML Output from PROC PRINT Using the Default Style

Program 2: Modifying the Default Style with the CLASS Statement

 proc template;
   define style customdefault;
   parent=styles.htmlblue;
      class contents  /
            background=cxffffcc;

	  class contenttitle /
	        background=cxffffcc; 

	   class  data /
	         background=cxcccccc;
   style IndexProcName from Index /
         
         backgroundcolor = cxffffcc;
      class colors /                                              
         'link2' = cx0000FF                                                   
         'link1' = cx800080                                                   
         'docbg' = cx99ccff                                               
         'contentbg' = cxFAFBFE                                               
         'systitlebg' = cx99ccff                                               
         'titlebg' = cxFAFBFE                                                 
         'proctitlebg' = cxFAFBFE                                             
         'headerbg' = cxEDF2F9                                                
         'captionbg' = cxFAFBFE                                               
         'captionfg' = cx112277                                               
         'bylinebg' = cx99ccff                                              
         'notebg' = cxFAFBFE                                                  
         'tablebg' = cxFAFBFE                                                 
         'batchbg' = cxFAFBFE                                                 
         'systitlefg' = cx112277                                              
         'titlefg' = cx112277                                                 
         'proctitlefg' = cx112277                                             
         'bylinefg' = cx112277                                                
         'notefg' = cx112277;      
   class Header   /                                              
         bordercolor = cxEDF2F9                                               
         backgroundcolor = cxEDF2F9                                           
         color = cx112277;          
   class text /
      "prefix1" = "PROC "
      "suffix1" = ":"
      "Content Title" = "Contents"
      "Pages Title" = "Pages"
      ;
   end;
run;
ods html body="customdefaultstyle-body.htm"
         contents="customdefaultstyle-content.htm"
         frame="customdefaultstyle-frame.htm"
         style=customdefault;
title "Energy Expenditures for Each Region";
title2 "(millions of dollars)";
proc print data=energy noobs;
   var state type expenditures;
   format

 division divfmt. type usetype. expenditures comma12.;
   by division;
   where division=2 or division=3;
run;
ods html close;

ods html;

Program Description

Create the style CustomDefault. The PROC TEMPLATE statement starts the TEMPLATE procedure. The DEFINE STYLE statement creates a new style called CustomDefault.
 proc template;
   define style customdefault;
Specify the parent style from which the CustomDefault style inherits its attributes. The PARENT= attribute specifies Styles.HTMLBlue as the style from which the current style inherits. All the style elements, attributes, and statements that are specified in the parent's style template are used in the child style template unless the child style template overrides them.
   parent=styles.htmlblue;
Customize the contents style elements and the data cells. By changing the BACKGROUND= style attribute in the style elements Contents, ContentTitle, and IndexProcName, the background of the contents becomes yellow.
      class contents  /
            background=cxffffcc;

	  class contenttitle /
	        background=cxffffcc; 

	   class  data /
	         background=cxcccccc;
   style IndexProcName from Index /
         
         backgroundcolor = cxffffcc;
Change the attributes of the style element Colors This CLASS statement adds to the child style the style element Colors, which also exists in the parent style (HTMLBlue). The CLASS statement adds all of the style attributes that are in the original instance of the Colorsstyle element to the new instance of Colors, except for those that are overridden by the new instance of Colors. By using the CLASS statement, you do not need to specify the FROM option. If you did not use the CLASS statement or the FROM option, then the attributes from the original instance of Colors would not be added to the new instance of Colors. The Colors style element in CustomDefault would contain only the style statements that it specifically specifies. All style elements that use the user-defined attributes that Colors defines (fgB2, fgB1, and so on) use the style attributes that are specified in Custom.Default, not the ones that are specified in Styles.HTMLBlue. Therefore, if you change a color here, then you change every occurrence of the color in the HTML output. This CLASS statement changes the values of three of the user-defined style attributes: Docbg=, Systitlebg=, and Bylinebg=.
      class colors /                                              
         'link2' = cx0000FF                                                   
         'link1' = cx800080                                                   
         'docbg' = cx99ccff                                               
         'contentbg' = cxFAFBFE                                               
         'systitlebg' = cx99ccff                                               
         'titlebg' = cxFAFBFE                                                 
         'proctitlebg' = cxFAFBFE                                             
         'headerbg' = cxEDF2F9                                                
         'captionbg' = cxFAFBFE                                               
         'captionfg' = cx112277                                               
         'bylinebg' = cx99ccff                                              
         'notebg' = cxFAFBFE                                                  
         'tablebg' = cxFAFBFE                                                 
         'batchbg' = cxFAFBFE                                                 
         'systitlefg' = cx112277                                              
         'titlefg' = cx112277                                                 
         'proctitlefg' = cx112277                                             
         'bylinefg' = cx112277                                                
         'notefg' = cx112277;      
Change the style attributes in the style element Header. This STYLE statement adds the italic font style to the attributes that Header inherits from the Header style element that is defined in the parent style. You could have also specified the STYLE statement with the FROM option specified. Because this change occurs after the initial merge of the two styles, the change will effect HeaderFixed and the other style elements that inherit from Header in the parent style.
In the default style, the background color for the byline differs from the background color for the document, so it appears as a gray stripe in HTML Output from PROC PRINT Using the Default Style. In this customized style, the stripe disappears because the background color for the byline and the document are the same.
   class Header   /                                              
         bordercolor = cxEDF2F9                                               
         backgroundcolor = cxEDF2F9                                           
         color = cx112277;          
Customize the text used in parts of the output. In the customized style, the text that identifies the output reads "1. PROC PRINT". The heading that appears at the top of the contents file has been changed from "Table of Contents" to "Contents", and the heading at the top of the table of pages has been changed from "Table of Pages" to "Pages". The banners have been changed to use mixed case. (Note that neither these banners nor the table of pages is visible in the HTML output from this example, but the attributes are included so that you can use the style in a variety of circumstances.)
This CLASS statement alters the text that is used in parts of the HTML output. In the contents file, the default style uses "The" as the value of prefix1 and "Procedure" as the value of suffix1. Thus, in HTML output that uses the default style, the output from PROC PRINT is identified by "1. The PRINT Procedure" (see HTML Output from PROC PRINT Using the Default Style).
   class text /
      "prefix1" = "PROC "
      "suffix1" = ":"
      "Content Title" = "Contents"
      "Pages Title" = "Pages"
      ;
Stop the creation of the customized style. The END statement ends the style. The RUN statement executes the PROC TEMPLATE step.
   end;
run;
Create the HTML output and specify the style to use for the output. The ODS HTML statement opens the HTML destination and creates HTML output. The output from PROC PRINT is sent to the body file. FRAME= and CONTENTS= create a frame that includes a table of contents that links to the contents of the body file. The body file also appears in the frame. The STYLE= option tells ODS to use CustomDefault as the style when it formats the output.
ods html body="customdefaultstyle-body.htm"
         contents="customdefaultstyle-content.htm"
         frame="customdefaultstyle-frame.htm"
         style=customdefault;
Specify the titles and footnote for the report. The TITLE and FOOTNOTE statements provide two titles and a footnote for the output.
title "Energy Expenditures for Each Region";
title2 "(millions of dollars)";
Print the customized report. PROC PRINT creates a report that includes three variables. ODS writes the report to the body file. This PROC PRINT step is the same one that was used with the default style earlier.
proc print data=energy noobs;
   var state type expenditures;
   format

 division divfmt. type usetype. expenditures comma12.;
   by division;
   where division=2 or division=3;
run;
Close the HTML destination. The ODS HTML statement closes the HTML destination and all the files that are associated with it. The ODS HTML statement opens the HTML destination to return ODS to its default setup.
ods html close;

ods html;
HTML Output from PROC PRINT with the Customized Style
HTML Output from PROC PRINT with the Customized Style