TEMPLATE Procedure: Creating Crosstabulation Table Templates

Example 3: Adding Custom Formats to Cellvalues

Features:

EDIT statement

Other features:
Other ODS features:
ODS HTML statement
ODS PATH statement

Details

This example does not use the DEFINE CROSSTABS statement. Instead, it uses the EDIT statement to edit the crosstabulation table template Base.Freq.CrossTabFreqs that was created in Creating a Crosstabulation Table Template with a Customized Legend by changing the formats of several cellvalues. In Creating a Crosstabulation Table Template with a Customized Legend, the following format values were used:
  • Frequency: BEST6
  • Percent, RowPercent, ColPercent: 6.2
In this example, the Frequency cellvalue is changed to COMMA12; and the Percent, RowPercent, and ColPercent cellvalues are changed to 6.3.

Program

 Proc Format;
   Value Govtfmt -3='Council Manager'
                  0='Commission'
                  3='Mayor Council'
                 .N='Not Applicable'
                  .='   ?';
   Value rowfg   -3='red'
                  0='purple'
                  3='blue'
                 .N='green'
                  .='black'
                  other='black';
   Value Robfmt   1='100 or  Less'
                  2='101-200'
                  3='201-300'
                  4='Over 300'
                 .N='Not     Known'
                  .='   ?';
    Value colfg   1='yellow'
                  2='red'
                  3='blue'
                  4='purple'
                 .N='green'
                  .='black'
                  other='black';
run;

data gov;
   Label Citygovt='City Government Form'
        Robgrp='Number of Meetings Scheduled';
   Input Citygovt Robgrp Weight;  Missing N;
   Format Citygovt Govtfmt. Robgrp Robfmt.;
   LOOP: OUTPUT; WEIGHT=WEIGHT-1; IF WEIGHT>0 THEN GOTO LOOP;
   DROP WEIGHT;
datalines;
0 1  6
0 3  3
0 2  7
0 4  5
N N 10
-3 1 47
-3 3 49
-3 2 63
-3 4 52
. 2  1
3 1 31
3 2 37
3 3 27
3 4 55
3 .  1
;
ods noproctitle;
ods path (prepend) work.templat(update);
proc template;
  edit Base.Freq.CrossTabFreqs;
     edit Frequency;
        format=COMMA12.;

     end;
     edit Percent;
        format=6.3;
     end;
     edit RowPercent;
        format=6.3;
     end;
     edit ColPercent;
        format=6.3;
     end;
  end;
run;
ods html file="userfmt.html" style=ocean;
title "Applying Custom Formats to Cellvalues";
proc freq;
   tables citygovt*robgrp / missprint;
run;
ods html close;

Program Description

Create the user-defined formats and the data set. The FORMAT procedure creates four user-defined formats that can be used in the crosstabulation template. The DATA step creates the Gov data set.
 Proc Format;
   Value Govtfmt -3='Council Manager'
                  0='Commission'
                  3='Mayor Council'
                 .N='Not Applicable'
                  .='   ?';
   Value rowfg   -3='red'
                  0='purple'
                  3='blue'
                 .N='green'
                  .='black'
                  other='black';
   Value Robfmt   1='100 or  Less'
                  2='101-200'
                  3='201-300'
                  4='Over 300'
                 .N='Not     Known'
                  .='   ?';
    Value colfg   1='yellow'
                  2='red'
                  3='blue'
                  4='purple'
                 .N='green'
                  .='black'
                  other='black';
run;

data gov;
   Label Citygovt='City Government Form'
        Robgrp='Number of Meetings Scheduled';
   Input Citygovt Robgrp Weight;  Missing N;
   Format Citygovt Govtfmt. Robgrp Robfmt.;
   LOOP: OUTPUT; WEIGHT=WEIGHT-1; IF WEIGHT>0 THEN GOTO LOOP;
   DROP WEIGHT;
datalines;
0 1  6
0 3  3
0 2  7
0 4  5
N N 10
-3 1 47
-3 3 49
-3 2 63
-3 4 52
. 2  1
3 1 31
3 2 37
3 3 27
3 4 55
3 .  1
;
Establish the ODS path. The ODS PATH statement specifies the locations to write to or read from when creating the PROC TEMPLATE templates. The ODS NOPROCTITLE statement suppresses the title of the FREQ procedure.
ods noproctitle;
ods path (prepend) work.templat(update);
Edit the crosstabulation template Base.Freq.CrossTabFreqs. The EDIT statement changes the crosstabulation table template
Base.Freq.CrossTabFreqs that was created in Creating a Crosstabulation Table Template with a Customized Legend.
proc template;
  edit Base.Freq.CrossTabFreqs;
Apply new formats to the cellvalues Frequency, Percent, RowPercent, and ColPercent. The FORMAT= attribute specifies a format for the cellvalues. The format COMMA12. is applied to Frequency, and the format 6.3 is applied to Percent, RowPercent, and ColPercent.
     edit Frequency;
        format=COMMA12.;

     end;
     edit Percent;
        format=6.3;
     end;
     edit RowPercent;
        format=6.3;
     end;
     edit ColPercent;
        format=6.3;
     end;
  end;
run;
Create the HTML output and specify the name of the HTML file. The ODS HTML statement with the STYLE= option specifies the style template Ocean for the output.
ods html file="userfmt.html" style=ocean;
Create the crosstabulation table and add a title. The FREQ procedure creates a Citygovt by Robgrp crosstabulation table. The TITLE statement specifies a title.
title "Applying Custom Formats to Cellvalues";
proc freq;
   tables citygovt*robgrp / missprint;
run;
Close the HTML destination. The ODS HTML CLOSE statement closes the HTML destination and all the files that are open for that destination.
ods html close;

Output

Crosstabulation Output with Custom Formats Applied to Cellvalues
Crosstabulation Output with Custom Formats Applied to Cellvalues