TEMPLATE Procedure: Creating Table Templates

Example 4: Setting the Style Element for Cells Based on Their Values

Features:
DEFINE TABLE statement:
NMVAR statement
NOTES statement
TRANSLATE INTO statement
DEFINE COLUMN statement:
BLANK_DUPS= attribute
CELLSTYLE AS statement
GENERIC= attribute
Other features:
Other ODS features:
DELETE statement
FILE statement with ODS= option
PUT statement with _ODS_ argument
Data set: Grain_Production
Format: $CNTRY.

Details

This example creates a template that uses different colors and font attributes for the text inside cells, depending on their values.
Note: This example uses filenames that might not be valid in all operating environments. To successfully run the example in your operating environment, you might need to change the file specifications. See ODS HTML Statements for Running Examples in Different Operating Environments.

Program

options nodate pageno=1 pagesize=60 linesize=72;
title "Leading Grain Producers";
proc template;
   define table shared.cellstyle;
   translate _val_=. into "No data";
   notes "NMVAR defines symbols that will be used to determine the colors
 of the cells.";
  nmvar low "Use default style."
        medium "Use yellow foreground color and bold font weight"
        high "Use red foreground color and a bold, italic font.";
   classlevels=on;
   define column char_var;
      generic=on;
      blank_dups=on;
   end;
   define column num_var;
      generic=on;
      justify=on;
%let low=10000;
%let medium=50000;
%let high=100000;
      cellstyle _val_ <= &low as data,
                _val_ <= &medium as data
                         {color=green fontstyle=italic},
                _val_ <= &high as data
                         {color=yellow fontweight=bold},
                    1 as data
                         {color=red fontstyle=italic
                          fontweight=bold};
   end;
      
   end;
run;
data _null_;
   set grain_production;
   file print ods=(
        template="shared.cellstyle"
        columns=(
           char_var=year(generic=on)
           char_var=country(generic=on format=$cntry.)
           char_var=type(generic=on)
           num_var=kilotons(generic=on format=comma12.)
           )
        );
   put _ods_;
run;
proc template;
    delete shared.cellstyle;
run;

Program Description

Set the SAS system options. The OPTIONS statement controls several aspects of the LISTING output. None of these options affects the HTML output. The TITLE statement specifies a title.
options nodate pageno=1 pagesize=60 linesize=72;
title "Leading Grain Producers";
Create the table template Shared.Cellstyle. The DEFINE statement creates the table template Shared.Cellstyle in the first template store in the path that is available to write to. By default, this template store is Sasuser.Templat.
proc template;
   define table shared.cellstyle;
Specify that missing values show the text "No data" in the report. The TRANSLATE INTO statement translates missing values (.) into the string No data.
   translate _val_=. into "No data";
Store the information about the table in the table template. The NOTES statement provides information about the table. NOTES statements remain a part of the compiled table template whereas SAS comments do not.
   notes "NMVAR defines symbols that will be used to determine the colors
 of the cells.";
Specify the symbols that reference three macro variables. The NMVAR statement defines three symbols that reference macro variables. ODS will convert the variable's values to numbers (stored as doubles) before using them. References to the macro variables are resolved when ODS binds the template and the data component to produce an output object. The text inside quotation marks provides information about the symbols. This information becomes a part of the compiled table template whereas SAS comments do not. LOW, MEDIUM, and HIGH will contain the values to use as the determinants of the style element that displays the cell. The values are provided just before the DATA step that produces the report.
  nmvar low "Use default style."
        medium "Use yellow foreground color and bold font weight"
        high "Use red foreground color and a bold, italic font.";
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 template Char_Var. The 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 value changes in a preceding column that is marked with BLANK_DUPS=ON changes).The END statement ends the template.
   define column char_var;
      generic=on;
      blank_dups=on;
   end;
Create the column template Num_Var. The DEFINE statement and its attributes create the column template Num_Var. GENERIC= specifies that multiple variables can use the same column template.
   define column num_var;
      generic=on;
Align the values in the column without regard to the format field. JUSTIFY= justifies the values in the column without regard to the format field. For numeric variables, the default justification is RIGHT, so even the translated character value No data that is used for missing values is right-justified. Without JUSTIFY=ON in this column template, the value No data is formatted as a character variable (left-justified) within a format field that has the same width as the column.
      justify=on;
Assign values to three macro variables. The %LET statements assign values to the macro variables LOW, MEDIUM, and HIGH.
%let low=10000;
%let medium=50000;
%let high=100000;
Specify which style element and style attributes to use for different values in the column. The CELLSTYLE AS statement specifies the style element and style attributes to use for different values in this column. If a value is less than or equal to the value of the variable LOW, the cell uses the unaltered Data style element. If a value is greater than LOW but less than or equal to the value of MEDIUM, the cell uses the style element Data with a foreground color of green and an italic font. Similarly, other values use a foreground color of yellow or red and combinations of a bold font weight and an italic font style. The CELLSTYLE AS statement affects only the HTML destination END statement ends the column template.
      cellstyle _val_ <= &low as data,
                _val_ <= &medium as data
                         {color=green fontstyle=italic},
                _val_ <= &high as data
                         {color=yellow fontweight=bold},
                    1 as data
                         {color=red fontstyle=italic
                          fontweight=bold};
   end;
      
End the table template. This END statement ends the table template. The RUN statement ends the PROC TEMPLATE step.
   end;
run;
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.
data _null_;
   set grain_production;
Route the DATA step results to ODS and use the Shared.CellStyle 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 Shared.CellStyle, which was previously created with PROC TEMPLATE.
   file print ods=(
        template="shared.cellstyle"
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 YEAR and that it uses the column template named Char_Var. GENERIC= must be set to ON, both in the table template and in each column assignment, in order for multiple variables to use the same column template.
        columns=(
           char_var=year(generic=on)
           char_var=country(generic=on format=$cntry.)
           char_var=type(generic=on)
           num_var=kilotons(generic=on format=comma12.)
           )
        );
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.
   put _ods_;
run;
Remove the customized table template.The DELETE statement removes the customized table that was created in this example. When using the DELETE statement, ODS looks for shared.cellstyle in Sasuser.Templat and Work.Templat first. If it is there, it will delete it. If not, it will search Sashelp.Tmplmst.
proc template;
    delete shared.cellstyle;
run;

HTML Output of a Customized Table

Both the table customizations and the style customizations appear in the HTML output. Table customizations include the suppression of values that do not change from one row to the next, and the translation of missing values to No data. The style customizations include the colors and font styles that are specified in the CELLSTYLE AS statement.
HTML Output (Viewed with Microsoft Internet Explorer)
HTML Output (Viewed with Microsoft Internet Explorer)