Thursday, January 06, 2011

Changing the Numeric Format of PROC FREQ Output

Although PROC TABULATE offers more flexibility in many ways, a lot of people (including me ) still prefer to use PROC FREQ for some of their table production activities. By default, PROC FREQ output uses no decimal places or commas for counts, and two decimal places for percents. This tip describes how to change these defaults for the three main types of PROC FREQ output: one-way tables, multiway tables in list format, and multiway tables presented as crosstabulations.

One-Way Tables
For one-way frequency tables, you can change these defaults by using PROC TEMPLATE to modify the table definition (or "template") that the Output Delivery System uses to display the table. The code below causes commas to be used for the Frequency and Cumulative Frequency columns, and uses only one decimal place to display the Percent and the Cumulative Percent.

ods path reset;
ods path (prepend) work.templat(update);
                                          
proc template;
  edit base.freq.onewaylist;                          
    edit frequency;          
      format=comma6.;                        
   end;                  
    edit cumfrequency;                                  
      format=comma6.;                    
    end;                    
    edit percent;                                  
      format=5.1;                          
    end;                        
    edit cumpercent;                                  
      format=5.1;                      
    end;                        
  end;;                                  
run;;                                    
                                  
                                          
proc freq data=sashelp.prdsale;
  tables division;
run;         

The ODS PATH statement at the beginning causes the modified version of the Base.Freq.OneWayList table definition to be written into the WORK library, so that it will only be in effect for the duration of the SAS program or session.

Multiway Tables in List Format
Sometimes it is handy to get PROC FREQ output in "list" format even when two or more variables are involved. You can use the LIST option on the TABLE statement to tell PROC FREQ to use list format rather than presenting the results as a crosstabulation. For example:

tables division * country /list;

If you try this, however, you will notice that the changes you made in PROC TEMPLATE have no effect on this output. This is because ODS uses a different table definition, Base.Freq.List, for multiway tabulations in list format. You can change the defaults for this type of output by running PROC TEMPLATE again, specifying Base.Freq.List in the EDIT statement. Alternatively, you can include both EDIT blocks in the same PROC TEMPLATE run.

Multiway Tables as Crosstabs
Modifying table definitions works fine, as long as a table definition is used to produce the output. While this is the case with the vast majority of SAS procedures, there are a few exceptions, and one of them is crosstabular output from PROC FREQ. (Other notable exceptions are the REPORT, PRINT, and TABULATE procedures.)
In Release 8.1, the SAS developers provided a partial solution to this problem by adding the FORMAT= option to the TABLES statement. When you are displaying your frequency output in crosstabular format, this statement allows you to specify a format that will be used to display the frequency, expected frequency, and deviation. For example:

tables division * country / format=comma6. ;

No comments:

Post a Comment

I love to hear from you! Leave a comment.
If your question is unrelated to this article, please use my Facebook page.