TEMPLATE Procedure: Creating a Style Template

Example 2: Using User-Defined Attributes

Features:
DEFINE STYLE statement: :
STYLE statement with user-defined attributes
DEFINE TABLE statement: CLASSLEVELS= table attribute
DEFINE TABLE statement: DYNAMIC statement
DEFINE TABLE statement: MVAR statement
DEFINE COLUMN statement: BLANK_DUPS=
DEFINE COLUMN statement: GENERIC=
DEFINE COLUMN statement: HEADER=
DEFINE COLUMN statement: STYLE=
DEFINE COLUMN statement: :
BLANK_DUPS= attribute
CELLSTYLE-AS statement
GENERIC= attribute

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.

Program 1: Description

This example creates a style that is equivalent to the style that Creating a Stand-Alone Style creates. However, this style uses user-defined attributes to specify colors and fonts. This technique makes it possible to easily make changes in multiple places in the output.

Program 1: Creating the Style

proc template;
   define style newstyle2;
      style fonts /
         "cellfont"=("arial, helvetica", 4, medium roman)
         "headingfont"=("arial, helvetica", 5, bold roman)
         "titlefont"=("arial, helvetica", 6, bold italic);
      style colors /
         "light"=white
         "medium"=cxaaaaff
         "dark"=cx0000ff
         "bright"=red;
      style cellcontents /
         backgroundcolor=colors("dark")
         color=colors("light")
         font=fonts("cellfont");
      style header /
         backgroundcolor=colors("medium")
         color=colors("dark")
         font=fonts("headingfont");
      style systemtitle /
         backgroundcolor=colors("light")
         color=colors("bright")
         font=fonts("titlefont");
      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 body="newstyle2-body.htm"
         style=newstyle2;
   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 the style NewStyle2. The PROC TEMPLATE statement starts the TEMPLATE procedure. The DEFINE STYLE statement creates a new style called NewStyle2. This STYLE statement defines the style element Fonts. This style element consists of three user-defined attributes: CellFont, HeadingFont, and TitleFont. Each of these attributes describes a font. This style specifies the fontfamily, fontsize, fontweight, and the fontstyle for each of the three attributes. The font and fontwidth attributes are still defined by the default style.
proc template;
   define style newstyle2;
      style fonts /
         "cellfont"=("arial, helvetica", 4, medium roman)
         "headingfont"=("arial, helvetica", 5, bold roman)
         "titlefont"=("arial, helvetica", 6, bold italic);
Create the style element Colors. This STYLE statement defines the style element Colors. This style element consists of four user-defined attributes: light, medium, dark, and bright. The values for medium and dark are RGB values equivalent to very light blue and blue.
      style colors /
         "light"=white
         "medium"=cxaaaaff
         "dark"=cx0000ff
         "bright"=red;
Create the three style elements CellContents, Header, and SystemTitle. Create the style element Footer using inheritance. The style attributes are defined in terms of the user-defined attributes that were created earlier in the style. For example, the foreground color in CellContents is set to colors("light"). Looking at the template of Colors, you can see that this is white. However, by setting the colors up in a style element with user-defined attributes, you can change the color of everything that uses a particular color by changing a single value in the style element Colors.
      style cellcontents /
         backgroundcolor=colors("dark")
         color=colors("light")
         font=fonts("cellfont");
      style header /
         backgroundcolor=colors("medium")
         color=colors("dark")
         font=fonts("headingfont");
      style systemtitle /
         backgroundcolor=colors("light")
         color=colors("bright")
         font=fonts("titlefont");
      style footer from systemtitle /
         fontsize=3;
      style table /
         borderspacing=5
         borderwidth=10;
End the style. The END statement ends the style. The RUN statement executes PROC TEMPLATE.
   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 hardcoding the headers in the table template does.
   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 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 NewStyle2 as the style when it formats the output.
ods html body="newstyle2-body.htm"
         style=newstyle2;
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;
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;

Original HTML Output

This HTML output is identical to the output in the sectionHTML Output: Specifying Colors and Fonts with User-Defined Attributes, which was produced with a style that used predefined style attributes. 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 produced with the Table style element.
HTML Output
HTML Output

Program 2: Description

In the program Creating a Stand-Alone Style, to change the color scheme so that the blues are replaced by pink and red, change each occurrence of "blue" and "very light blue." In this program, because colors are defined as user-defined attributes, make the change only once.

Program 2: Changing User-Defined Attributes

      style colors /
         "light"=white
         "medium"=cxaaaaff
         "dark"=cx0000ff
         "bright"=red;
      style colors /
         "light"=white
         "medium"=pink
         "dark"=red
         "bright"=red;
         "cellfont"=("arial, helvetica", 4, medium roman)
         "cellfont"=("courier, arial, helvetica", 4, medium roman)

Program Description

To make the color scheme change, change only this section of code:
The following is the original portion on code from Program 1: Creating the Style.
      style colors /
         "light"=white
         "medium"=cxaaaaff
         "dark"=cx0000ff
         "bright"=red;
Change the attributes as follows:
      style colors /
         "light"=white
         "medium"=pink
         "dark"=red
         "bright"=red;
Similarly, to change the font in any style element that uses CellFont, change this section of code:
         "cellfont"=("arial, helvetica", 4, medium roman)
Here is one example of how to change the code:
         "cellfont"=("courier, arial, helvetica", 4, medium roman)

HTML Output: Changing Colors and Fonts of User-Defined Attributes

This HTML output shows the results of running the same program with these changes.
The font in the cells is now Courier. This change occurs in multiple places even though you made only one change to the code for the font.
HTML Output with Changed Colors and Fonts
HTML Output with Changed Colors and Fonts