TEMPLATE Procedure: Creating Table Templates

Example 5: Setting the Style Element for a Specific Column, Row, and Cell

Features:

DEFINE STYLE statement : REPLACE statement

DEFINE TABLE statement: CELLSTYLE AS statement

DEFINE COLUMN statement: DEFINE HEADER statement: TEXT statement

DEFINE HEADER statement: TEXT statement

Other features:
Other ODS features:
FILE statement with ODS= option
ODS HTML statement: STYLE= option
ODS PDF statement: STYLE= option
PUT statement: _ODS_ argument
ODS TRACE statement
Data set: Exprev

Details

This example combines a customized style with a customized table template to produce output with a checkerboard pattern of table cells.

Program

options obs=20;
title;
proc template;
   define style greenbar;
   parent=styles.printer;
   replace headersandfooters from cell /
         backgroundcolor=light green
         color=black
         fontsize=3
         fontweight=bold
       ;
    end;
 run;
ods html body="greenbar.html" style=greenbar;
ods pdf file="greenbar.pdf" style=greenbar;
ods trace on;
proc template;
   define table Checkerboard;
cellstyle mod(_row_,2) && mod(_col_,2) as
data{backgroundcolor=yellow fontweight=bold },
                not(mod(_row_,2)) && not(mod(_col_,2)) as
data{backgroundcolor=yellow fontweight=bold },
                1 as data;     
define header top;
   text "Checkerboard Table Template";
end; 
      define column country;
         dataname=country;
         define header bar;
            text "Country";
         end;
         header=bar;
      end;
      define column OrderDate;
         dataname=Order_Date;
         define header bar;
            text "Order Date";
         end;
         header=bar;
      end;
      define column ShipDate;
         dataname=Ship_Date;
         define header bar;
            text "Ship Date";
         end;
         header=bar;
      end;
      define column SaleType;
         dataname=Sale_Type;
         define header bar;
            text "Sale Type";
         end;
         header=bar;
      end;
end;
run;
data _null_;
   set work.exprev;
file print ods=(template="Checkerboard");
  put _ods_;
run;
ods html close;
ods pdf close;
ods html;

Program Description

Set the SAS system options.
options obs=20;
title;
Create the new style Greenbar. The PROC TEMPLATE statement starts the TEMPLATE procedure. The DEFINE STYLE statement creates a new style Greenbar.
proc template;
   define style greenbar;
Specify the parent style from which the Greenbar style inherits its attributes. The PARENT= attribute specifies the style from which the Greenbar definition inherits its style elements and attributes. All the style elements and their attributes that are specified in the parent's definition are used in the current definition unless the current definition overrides them.
   parent=styles.printer;
Change the colors used in the headers and footers. The REPLACE statement adds a style element to the Greenbar style from the parent style, but the background is light green, the foreground is black, and the font is bold and has a size of 3.
   replace headersandfooters from cell /
         backgroundcolor=light green
         color=black
         fontsize=3
         fontweight=bold
       ;
End the style. The END statement ends the style. The RUN statement executes the PROC TEMPLATE step.
    end;
 run;
Create the PDF output and specify the style that you want to use for the output. The ODS HTML statement sends all output objects to the file greenbar.html. The STYLE= option tells ODS to use Greenbar as the style when it formats the output. The ODS PDF statement opens the PDF destination and creates PDF output. It sends all output objects to the file greenbar.pdf in the current directory. The STYLE= option tells ODS to use Greenbar as the style when it formats the output.
ods html body="greenbar.html" style=greenbar;
ods pdf file="greenbar.pdf" style=greenbar;
ods trace on;
Create the table template Checkerboard. The DEFINE statement creates the table template Checkerboard 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 Checkerboard;
Specify which style element and style attributes to use for different cells. The CELLSTYLE-AS statement specifies the style element and style attributes to use for cells in each of the rows and columns. The CELLSTYLE-AS statement creates the checkerboard effect in the output. If both the row and column are odd numbered, then the cell is yellow. Similarly, if both the row and column are even numbered, then the cell is yellow.
cellstyle mod(_row_,2) && mod(_col_,2) as
data{backgroundcolor=yellow fontweight=bold },
                not(mod(_row_,2)) && not(mod(_col_,2)) as
data{backgroundcolor=yellow fontweight=bold },
                1 as data;     
Create the header template Top. The DEFINE HEADER statement defines the table header Top. The TEXT statement specifies the text of the header “Checkerboard Table Template”. The END statement ends the header template.
define header top;
   text "Checkerboard Table Template";
end; 
Create the column template Country. The DEFINE COLUMN statement creates the column template Country. The DEFINE HEADER statement creates the header template Bar. The DATANAME= column attribute specifies the name of the column Country in the data component to associate with the column template Country. The TEXT statement specifies the text to use in the header. The first END statement ends the header template. The HEADER statement declares Bar as the header in the table. The second END statement ends the column template.
      define column country;
         dataname=country;
         define header bar;
            text "Country";
         end;
         header=bar;
      end;
Create the column template OrderDate. The DEFINE COLUMN statement creates the column template OrderDate. The DATANAME= column attribute specifies the name of the column OrderDate in the data component to associate with the column template OrderDate. The DEFINE HEADER statement creates the header template Bar. The TEXT statement specifies the text “Order Date” to use in the header. The first END statement ends the header template. The HEADER statement declares Bar as the header in the table. The second END statement ends the column template.
      define column OrderDate;
         dataname=Order_Date;
         define header bar;
            text "Order Date";
         end;
         header=bar;
      end;
Create the column template ShipDate. The DEFINE COLUMN statement creates the column template ShipDate. The DATANAME= column attribute specifies the name of the column template ShipDate in the data component to associate with the column template ShipDate. The DEFINE HEADER statement creates the header template Bar. The TEXT statement specifies the text “Ship Date” to use in the header. The first END statement ends the header template. The HEADER statement declares Bar as the header in the table. The second END statement ends the column template.
      define column ShipDate;
         dataname=Ship_Date;
         define header bar;
            text "Ship Date";
         end;
         header=bar;
      end;
Create the column template SaleType. The DEFINE COLUMN statement creates the column template SaleType. The DATANAME= column attribute specifies the name of the column template SaleType in the data component to associate with the column template SaleType. The DEFINE HEADER statement creates the header template Bar. The TEXT statement specifies the text “Sale Type” to use in the header. The first END statement ends the header template. The HEADER statement declares Bar as the header in the table. The second END statement ends the column template.
      define column SaleType;
         dataname=Sale_Type;
         define header bar;
            text "Sale Type";
         end;
         header=bar;
      end;
End the table template. The END statement ends the table template. The RUN statement executes the TEMPLATE procedure.
end;
run;
Create the data component. This DATA step does not create a data set. Instead, it creates a data component that is used to produce an output object. The SET statement reads the data set Work.Exprev.
data _null_;
   set work.exprev;
Route the DATA step results to ODS and use the Checkerboard 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 Checkerboard.
file print ods=(template="Checkerboard");
  put _ods_;
run;
Stop the creation of PDF output. The ODS HTML CLOSE statement closes the HTML destination and all the files that are associated with it. The ODS PDF CLOSE statement closes the PDF destination and all the files that are associated with it. You must close the PDF destination before you can view the output.
ods html close;
ods pdf close;
ods html;

Output

HTML Output (Viewed with Internet Explorer 6.0)
HTML Output (Viewed with Internet Explorer 6.0)
PDF Output (Viewed with Acrobat Reader 5.0)
PDF Output (Viewed with Acrobat Reader 5.0)