The FREQ Procedure

Example 2.1 Output Data Set of Frequencies

The eye and hair color of children from two different regions of Europe are recorded in the data set Color. Instead of recording one observation per child, the data are recorded as cell counts, where the variable Count contains the number of children exhibiting each of the 15 eye and hair color combinations. The data set does not include missing combinations.

The following DATA step statements create the SAS data set Color:

data Color;
   input Region Eyes $ Hair $ Count @@;
   label Eyes  ='Eye Color'
         Hair  ='Hair Color'
         Region='Geographic Region';
   datalines;
1 blue  fair   23  1 blue  red     7  1 blue  medium 24
1 blue  dark   11  1 green fair   19  1 green red     7
1 green medium 18  1 green dark   14  1 brown fair   34
1 brown red     5  1 brown medium 41  1 brown dark   40
1 brown black   3  2 blue  fair   46  2 blue  red    21
2 blue  medium 44  2 blue  dark   40  2 blue  black   6
2 green fair   50  2 green red    31  2 green medium 37
2 green dark   23  2 brown fair   56  2 brown red    42
2 brown medium 53  2 brown dark   54  2 brown black  13
;

The following PROC FREQ statements read the Color data set and create an output data set that contains the frequencies, percentages, and expected cell frequencies of the two-way table of Eyes by Hair. The TABLES statement requests three tables: a frequency table for Eyes, a frequency table for Hair, and a crosstabulation table for Eyes by Hair. The OUT= option creates the FreqCount data set, which contains the crosstabulation table frequencies. The OUTEXPECT option outputs the expected table cell frequencies to FreqCount, and the SPARSE option includes cell frequencies of 0 in the output data set. The WEIGHT statement specifies that the variable Count contains the observation weights. These statements create Output 2.1.1 through Output 2.1.3.

proc freq data=Color;
   tables Eyes Hair Eyes*Hair / out=FreqCount outexpect sparse;
   weight Count;
   title 'Eye and Hair Color of European Children';
run;
proc print data=FreqCount noobs;
   title2 'Output Data Set from PROC FREQ';
run;

Output 2.1.1 displays the two frequency tables produced by PROC FREQ: one showing the distribution of eye color, and one showing the distribution of hair color. By default, PROC FREQ lists the variables values in alphabetical order. The 'Eyes*Hair' specification produces a crosstabulation table, shown in Output 2.1.2, with eye color defining the table rows and hair color defining the table columns. A cell frequency of 0 for green eyes and black hair indicates that this eye and hair color combination does not occur in the data.

The output data set FreqCount (Output 2.1.3) contains frequency counts and percentages for the last table requested in the TABLES statement, Eyes by Hair. Because the SPARSE option is specified, the data set includes the observation that has a frequency of 0. The variable Expected contains the expected frequencies, as requested by the OUTEXPECT option.

Output 2.1.1: Frequency Tables

Eye and Hair Color of European Children

The FREQ Procedure

Eye Color
EyesFrequency PercentCumulative
Frequency
Cumulative
Percent
blue22229.1322229.13
brown34144.7556373.88
green19926.12762100.00

Hair Color
HairFrequency PercentCumulative
Frequency
Cumulative
Percent
black222.89222.89
dark18223.8820426.77
fair22829.9243256.69
medium21728.4864985.17
red11314.83762100.00


Output 2.1.2: Crosstabulation Table

Frequency
Percent
Row Pct
Col Pct
Table of Eyes by Hair
Eyes(Eye Color)Hair(Hair Color)
blackdarkfairmediumredTotal
blue
6
0.79
2.70
27.27
51
6.69
22.97
28.02
69
9.06
31.08
30.26
68
8.92
30.63
31.34
28
3.67
12.61
24.78
222
29.13
 
 
brown
16
2.10
4.69
72.73
94
12.34
27.57
51.65
90
11.81
26.39
39.47
94
12.34
27.57
43.32
47
6.17
13.78
41.59
341
44.75
 
 
green
0
0.00
0.00
0.00
37
4.86
18.59
20.33
69
9.06
34.67
30.26
55
7.22
27.64
25.35
38
4.99
19.10
33.63
199
26.12
 
 
Total
22
2.89
182
23.88
228
29.92
217
28.48
113
14.83
762
100.00


Output 2.1.3: Output Data Set of Frequencies

Eye and Hair Color of European Children
Output Data Set from PROC FREQ

EyesHairCOUNTEXPECTEDPERCENT
blueblack66.4090.7874
bluedark5153.0246.6929
bluefair6966.4259.0551
bluemedium6863.2208.9239
bluered2832.9213.6745
brownblack169.8452.0997
browndark9481.44612.3360
brownfair90102.03111.8110
brownmedium9497.10912.3360
brownred4750.5686.1680
greenblack05.7450.0000
greendark3747.5304.8556
greenfair6959.5439.0551
greenmedium5556.6717.2178
greenred3829.5104.9869


Last updated: April 10, 2023