SAS FAQ
How can I change the way variables are displayed in proc freq?

The following program builds a data called auto, which we will use for our examples.

DATA auto ;
  LENGTH make $ 20 ;
  INPUT make $ 1-17 price mpg rep78 modtype ;
CARDS;
AMC Concord        4099 22 3 2
AMC Pacer          4749 17 3 2
Audi 5000          9690 17 5 3
Audi Fox           6295 23 3 1
BMW 320i           9735 25 4 1
Buick Century      4816 20 3 2
Buick Electra      7827 15 4 2
Buick LeSabre      5788 18 3 2
Cad. Eldorado     14500 14 2 3
Olds Starfire      4195 24 1 2
Olds Toronado     10371 16 3 2
Plym. Volare       4060 18 2 2
Pont. Catalina     5798 18 4 2
Pont. Firebird     4934 18 1 1
Pont. Grand Prix   5222 19 3 2
Pont. Le Mans      4723 19 3 1
;
RUN;

Proc freq prints frequencies in ascending order, as determined by variable value. For example, we request frequencies for modtype below and the table shows modeltype 1 followed by modtype 2 followed by modtype 3.

PROC FREQ DATA=auto;
  TABLES modtype;
RUN;
                                Cumulative  Cumulative
MODTYPE   Frequency   Percent   Frequency    Percent
-----------------------------------------------------
      1          4      25.0           4       25.0
      2         10      62.5          14       87.5
      3          2      12.5          16      100.0 

Suppose we create value format labels to add descriptive text for the three models of cars using proc format, as illustrated below.

PROC FORMAT;
   VALUE typefmt
        1 = 'Sporty '
        2 = 'Midsize'
        3 = 'Luxury' ;
RUN;

Our tables will print as follows:

PROC FREQ DATA = auto;
   TABLES modtype;
   FORMAT modtype typefmt.; 
RUN;
                               Cumulative  Cumulative
MODTYPE   Frequency   Percent   Frequency    Percent
-----------------------------------------------------
Sporty           4      25.0           4       25.0
Midsize         10      62.5          14       87.5
Luxury           2      12.5          16      100.0 

The table still prints in an order determined by the actual variable value. Sporty cars are displayed first because the variable is coded 1. We can, however, change the order the frequencies are displayed by using the order = formatted option.  This option prints frequencies in alphabetical order as determined by the formatted value, as illustrated below.

PROC FREQ DATA=auto ORDER=FORMATTED;
  TABLES modtype;
  FORMAT modtype typefmt.;
RUN;
                              Cumulative  Cumulative
MODTYPE   Frequency   Percent   Frequency    Percent
-----------------------------------------------------
Luxury           2      12.5           2       12.5
Midsize         10      62.5          12       75.0
Sporty           4      25.0          16      100.0

You can use the order = freq option if you want to print values in descending order of frequency.

PROC FREQ DATA= auto ORDER=FREQ;
  TABLES modtype;
  FORMAT modtype typefmt.;
RUN;
                               Cumulative  Cumulative
MODTYPE   Frequency   Percent   Frequency    Percent
-----------------------------------------------------
Midsize         10      62.5          10       62.5
Sporty           4      25.0          14       87.5
Luxury           2      12.5          16      100.0 

How to cite this page

Report an error on this page or leave a comment

The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.