TEMPLATE Procedure: Creating a Style Template

Example 1: Creating a Stand-Alone Style

Features:

BACKGROUNDCOLOR= in the STYLE statement

BORDERWIDTH= in the STYLE statement

BORDERSPACING= in the STYLE statement

FONTFAMILY= in the STYLE statement

FONTSIZE= in the STYLE statement

FONTSTYLE= in the STYLE statement

FONTWEIGHT= in the STYLE statement

COLOR= in the STYLE statement

CLASSLEVELS= table attribute in the DEFINE TABLE statement

DYNAMIC statement in the DEFINE TABLE statement

MVAR statement in the DEFINE TABLE statement

BLANK_DUPS= in the DEFINE COLUMN statement

GENERIC= in the DEFINE COLUMN statement

HEADER= in the DEFINE COLUMN statement

STYLE= in the DEFINE COLUMN statement

TEXT statement in the DEFINE FOOTER statement

Other features:
Other ODS features::
ODS HTML statement
FILE statement with ODS= option
PUT statement with _ODS_ argument
Data set: Grain_Production
Format: $CNTRY.

Details

This example creates a style that is not based on any other style. When you create a style, you will usually base it on one of the styles that SAS provides (see Using the CLASS Statement). However, this example is provided to show you some of the basic ways to create a style.
It is important to understand that by default, certain table elements are created with certain style elements. For example, unless you specify a different style element with the STYLE= attribute, ODS produces SAS titles with the SystemTitle style element. Similarly, unless you specify otherwise, ODS produces headers with the Header style element. (For information about each style element, see ODS Style Elements.

Program

proc template;
   define style newstyle;
      style cellcontents /
         fontfamily="arial, helvetica"
         fontweight=medium
         backgroundcolor=blue
         fontstyle=roman
         fontsize=5
         color=white;
      style header /
         backgroundcolor=very light blue
         fontfamily="arial, helvetica"
         fontweight=medium
         fontstyle=roman
         fontsize=5
         color=white;
     style systemtitle /
         fontfamily="arial, helvetica"
         fontweight=medium
         backgroundcolor=white
         fontstyle=italic
         fontsize=6
         color=red;
     style footer from systemtitle /
         fontsize=3;
     style table /
         borderspacing=5
         borderwidth=10;
   end;
run;
proc template;
   define table table1;
   mvar sysdate9;
   dynamic colhd;
   classlevels=on;
   define column char_var;
      generic=on;
      blank_dups=on;
      header=colhd;
      style=cellcontents;
   end;
   define column num_var;
      generic=on;
      header=colhd;
      style=cellcontents;
   end;
   define footer table_footer;
      text "Prepared on " sysdate9;
   end;
   end;
run;
ods html file="newstyle-body.htm"
style=newstyle;
   title "Leading Grain Producers";
   title2 "in 1996";
data _null_;
   set grain_production;
   where type  in ("Rice", "Corn") and year=1996;
   file print ods=(
        template="table1"
        columns=(
           char_var=country(generic=on format=$cntry.
                    dynamic=(colhd="Country"))
           char_var=type(generic dynamic=(colhd="Year"))
           num_var=kilotons(generic=on format=comma12.
                   dynamic=(colhd="Kilotons"))
           )
        );
   put _ods_;
run;
ods html close;
ods html;

Program Description

Create a new style named NewStyle with the style element CellContents. The PROC TEMPLATE statement starts the TEMPLATE procedure. The DEFINE STYLE statement creates a new style called NewStyle. This STYLE statement defines the style element CellContents. This style element consists of the style attributes that appear in the STYLE statement. The FONTFAMILY= attribute tells the browser to use the Arial font if it is available, and to look for the Helvetica font if Arial is not available.
proc template;
   define style newstyle;
      style cellcontents /
         fontfamily="arial, helvetica"
         fontweight=medium
         backgroundcolor=blue
         fontstyle=roman
         fontsize=5
         color=white;
Create the style element Header. This STYLE statement creates the style element Header. By default, ODS uses Header to produce both spanning headers and column headings. This style element uses a different background color from CellContents. It uses the same font (Arial or Helvetica), the same font style (roman), the same font color (white), and the same font size (5) as CellContents.
      style header /
         backgroundcolor=very light blue
         fontfamily="arial, helvetica"
         fontweight=medium
         fontstyle=roman
         fontsize=5
         color=white;
Create the style element SystemTitle. This STYLE statement creates the style element SystemTitle. By default, ODS uses SystemTitle to produce SAS titles. This style element uses a color scheme of a red foreground on a white background. It uses the same font and font weight as Header and CellContents, but it adds an italic font style and uses a larger font size.
     style systemtitle /
         fontfamily="arial, helvetica"
         fontweight=medium
         backgroundcolor=white
         fontstyle=italic
         fontsize=6
         color=red;
Create the style element Footer. This STYLE statement creates the style element Footer. This style element inherits all the attributes of SystemTitle. However, the font size that it inherits is overwritten by the FONTSIZE= attribute in its template.
     style footer from systemtitle /
         fontsize=3;
Create the style element Table. This STYLE statement creates the style element Table. By default, ODS uses this style element to display tables.
     style table /
         borderspacing=5
         borderwidth=10;
End the style. The END statement ends the style template. The RUN statement executes the TEMPLATE procedure.
   end;
run;
Create the table template Table1. The PROC TEMPLATE statement starts the TEMPLATE procedure. The DEFINE TABLE statement creates a new table template called Table1.
proc template;
   define table table1;
Specify the symbol that references one macro variable. The MVAR statement defines a symbol, SysDate9, that references a macro variable. ODS will use the value of this macro variable as a string. References to the macro variable are resolved when ODS binds the table template to the data component to produce an output object. SYSDATE9 is an automatic macro variable whose value is always available.
   mvar sysdate9;
Specify the symbol that references a value to be supplied by the data component. The DYNAMIC statement defines a symbol, Colhd, that references a value that the data component supplies when ODS binds the template and the data component to produce an output object. The values for Colhd are provided in the FILE statement in the DATA step that appears later in the program. Using dynamic column headings gives you more flexibility than does hardcoding the headers in the table template.
   dynamic colhd;
Control the repetition of values that do not change from one row to the next row. The CLASSLEVELS= attribute suppresses the display of the value in a column that is marked with BLANK_DUPS=ON if the value changes in a previous column that is also marked with BLANK_DUPS=ON. Because BLANK_DUPS= is set in a generic column, set this attribute as well.
   classlevels=on;
Create the column Char_Var. This DEFINE statement and its attributes create the column template Char_Var. GENERIC= specifies that multiple variables can use the same column template. BLANK_DUPS= suppresses the display of the value in the column if it does not change from one row to the next (and, because CLASSLEVELS=ON for the table, if no values in preceding columns that are marked with BLANK_DUPS=ON changes). HEADER= specifies that the header for the column will be the text of the dynamic variable Colhd, whose value will be set by the data component. The STYLE= attribute specifies that the style element for this column template is CellContents.The END statement ends the template.
   define column char_var;
      generic=on;
      blank_dups=on;
      header=colhd;
      style=cellcontents;
   end;
Create the column template Num_Var. This DEFINE statement and its attributes create the column template Num_Var. GENERIC= specifies that multiple variables can use the same column template. HEADER= specifies that the header for the column will be the text of the dynamic variable Colhd, whose value will be set by the data component. The STYLE= attribute specifies that the style element for this column template is CellContents.The END statement ends the template.
   define column num_var;
      generic=on;
      header=colhd;
      style=cellcontents;
   end;
Create the footer element Table_Footer. The DEFINE statement and its substatement define the table element Table_Footer. The FOOTER argument declares Table_Footer as a footer. The TEXT statement specifies the text of the footer. When ODS binds the data component to the table template (in the DATA step that follows), it will resolve the value of the macro variable SYSDATE9.
   define footer table_footer;
      text "Prepared on " sysdate9;
   end;
End the table template. This END statement ends the table template. The RUN statement executes the PROC TEMPLATE step.
   end;
run;
Create HTML output and specify the location for storing the HTML output. Specify the style to use for the output.The HTML destination is open by default. However, to specify a style, you must use the ODS HTML statement with the STYLE= open specified.. The STYLE= option tells ODS to use NewStyle as the style when it formats the output.
ods html file="newstyle-body.htm"
style=newstyle;
Specify the titles for the report. The TITLE statements provide two titles for the output.
   title "Leading Grain Producers";
   title2 "in 1996";
Create the data component. This DATA step does not create a data set. Instead, it creates a data component and, eventually, an output object. The SET statement reads the data set Grain_Production. The WHERE statement subsets the data set so that the output object contains information only for rice and corn production in 1996.
data _null_;
   set grain_production;
   where type  in ("Rice", "Corn") and year=1996;
Route the DATA step results to ODS and use the Table1 table template. The combination of the fileref PRINT and the ODS option in the FILE statement routes the results of the DATA step to ODS. The TEMPLATE= suboption tells ODS to use the table template named Table1, which was previously created with PROC TEMPLATE.
For more information about using the DATA step with ODS, see Using ODS with the DATA Step.
   file print ods=(
        template="table1"
Specify the column template to use for each variable. The COLUMNS= suboption places DATA step variables into columns that are defined in the table template. For example, the first column-specification specifies that the first column of the output object contains the values of the variable COUNTRY and that it uses the column template named Char_Var. GENERIC= must be set to ON in both the table template and each column assignment in order for multiple variables to use the same column template. The FORMAT= suboption specifies a format for the column. The DYNAMIC= suboption provides the value of the dynamic variable Colhd for the current column. Notice that for the first column the column header is Country, and for the second column, which uses the same column template, the column header is Year.
        columns=(
           char_var=country(generic=on format=$cntry.
                    dynamic=(colhd="Country"))
           char_var=type(generic dynamic=(colhd="Year"))
           num_var=kilotons(generic=on format=comma12.
                   dynamic=(colhd="Kilotons"))
           )
        );
Write the data values to the data component. The _ODS_ option and the PUT statement write the data values for all columns to the data component. The RUN statement executes the DATA step.
   put _ods_;
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. Close the destination so that you can view the output with a browser. The ODS HTML statement opens the HTML destination to return ODS to its default setup.
ods html close;
ods html;

HTML Output: Specifying Colors and Fonts with User-Defined Attributes

Use the fonts to confirm that SAS titles use the SystemTitle style element, that column headings use the Header style element, that the footer uses the Table-Footer style element, and that the contents of both character and numeric cells use the CellContents style element. Use the width of the table border and the spacing between cells to confirm that the table itself is produced with the Table style element.
HTML Output
HTML Output: Specifying Colors and Fonts with User-Defined Attributes