TEMPLATE Procedure: Creating a Style Template

Example 5: Defining Multiple Style Elements in One STYLE Statement

Features:
STYLE statement: :
FROM option
Style attribute: BACKGROUNDCOLOR=
Style attribute: BORDERWIDTH=
Style attribute: BORDERSPACING=
Style attribute: FONTFAMILY=
Style attribute: FONTSIZE=
Style attribute: FONTSTYLE=
Style attribute: FONTWEIGHT=
Style attribute: COLOR=
DEFINE TABLE statement: :
CLASSLEVELS= table attribute
DYNAMIC statement
MVAR statement
DEFINE COLUMN statement: :
BLANK_DUPS=
GENERIC=
HEADER=
STYLE=

DEFINE FOOTER statement: : TEXT 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 defines multiple style elements concurrently. When style element names are specified multiple times, all of the attributes from all instances of that name are collected to create the final set of style attributes. Defining multiple style elements in one STYLE statement makes it possible to create shorter, easier to read programs and to make changes to style attributes in a single STYLE statement rather than in many STYLE statements.
For example, if you wanted to add the style element BorderColor=black to the style elements CellContents, Header, and SystemTitle in the program below, you could add it once, to the first STYLE statement, instead of adding it three times, to each individual STYLE statement.

Program

proc template;
   define style newstyle;
      style cellcontents, header, systemtitle /
         fontfamily="arial, helvetica"
         fontweight=medium
         backgroundcolor=blue
         fontstyle=roman
         fontsize=5
         color=white;
      class header /
         backgroundcolor=very light blue;
      class systemtitle /
         backgroundcolor=white
         color=red
         fontstyle=italic
         fontsize=6;
      style footer from systemtitle /
         fontsize=3;
      class table /
         borderspacing=5
         borderwidth=10;
      end;
run;
ods html body="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 NewStyle. The PROC TEMPLATE statement starts the TEMPLATE procedure. The DEFINE STYLE statement creates a new style called NewStyle.
proc template;
   define style newstyle;
Create the CellContents, Header, and SystemTitle style elements.This STYLE statement defines three style elements: CellContents, Header, and SystemTitle. They are all composed 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. These three style elements use a color scheme of a white foreground on a blue background, and the font for all three is medium roman with a size of five.
      style cellcontents, header, systemtitle /
         fontfamily="arial, helvetica"
         fontweight=medium
         backgroundcolor=blue
         fontstyle=roman
         fontsize=5
         color=white;
Modify the Header style element. The STYLE statement with the FROM option specified creates the new instance of Header from the previous instance of Header, but changes the background color from white to very light blue. By default, ODS uses Header to produce both spanning headers and column headings.
      class header /
         backgroundcolor=very light blue;
Modify the SystemTitle style element. By default, ODS uses SystemTitle to produce SAS titles.
      class systemtitle /
         backgroundcolor=white
         color=red
         fontstyle=italic
         fontsize=6;
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.
      class table /
         borderspacing=5
         borderwidth=10;
      end;
run;
Create HTML output and specify the location for storing the HTML output. Specify the style to use for the output. The ODS HTML statement opens the HTML destination and creates HTML output. It sends all output objects to the external file NewStyle-Body in the current directory. The STYLE= option tells ODS to use NewStyle as the style when it formats the output.
ods html body="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. For the program that creates the table template Table1, see Creating the Table1 Table Definition.
   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;
Close the HTML destination. The ODS HTML statement closes the HTML destination and all the files that are associated with it. Specify the ODS HTML statement again to return ODS to its default setup.
ods html close;
ods html;

HTML Output: Specifying Colors and Fonts

You can 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