Beginning Tutorials
Paper 69-25
PROC FREQ: Its More Than Counts
Richard Severino, The Queens Medical Center, Honolulu, HI
Copyright By Assignmentchef assignmentchef
The FREQ procedure can be used for more than just obtaining a simple frequency distribution or a 2-way cross-tabulation. Multi- dimension tables can be analyzed using proc FREQ. There are many options which control what statistical test is performed as well as what output is produced. Some of the tests require that the data satisfy certain conditions. Some options produce a set of results from which one must select appropriately for the situation at hand. Which of the results produced by using the CHISQ option should one use? What is the WEIGHT statement for? Why would one create an output data set with the OUT= option? This paper (beginning tutorial) will answer these questions as many of the options available in Proc FREQ are reviewed.
INTRODUCTION
The name alone might lead anyone to think that primary use of PROC FREQ is to generate tables of frequencies. According to the SAS documentation, the FREQ procedure produces one- way to n-way frequency and cross-tabulation tables. In the second edition of The Little SAS Book, Delwiche and Slaughter state that the most obvious reason for using PROC FREQ is to create tables showing the distribution of categorical data values. In fact, PROC FREQ is more than just a procedure for counting and cross tabulating. PROC FREQ is capable of producing test statistics and other statistical measures in order to analyze categorical data based on the cell frequencies in 2-way or higher tables.
There are quite a few options one can use in PROC FREQ and the output often includes additional information the user did not request or expect. A first time user trying to obtain a simple chi- square test statistic from a 2-way table may be surprised to see that the CHISQ option gives them more than just the-Square. What are the different statistical tests and measures available in PROC FREQ? Can the output be controlled? Can you eliminate the unwanted or inappropriate test statistics? These are some of the questions that this paper will address.
The general syntax for PROC FREQ is:
PROC FREQ options;
BY variable-list;
TABLES requests / options;
WEIGHT variable;
OUTPUT
EXACT statistic-keywords < / computation-option >; TEST options;
with the last statement, TEST, being a new addition in version 7. As the options are discussed, any that are new with version 7 and not available in version 6.12 will be identified.
The only required statement is PROC FREQ; which will produce a one-way frequency table for each variable in the data set. For example, suppose we are using a data set consisting of the coffee data in chapter 4 of The Little SAS Book. The data consists of two variables: the type of coffee ordered and the window it was ordered from. If we run the following code:
proc freq;
then the resulting output would look like that in Output 1. Output 1. Default output for PROC FREQ
Coffee Data
Output when running: PROC FREQ; RUN;
Cumulative Cumulative COFFEE Frequency Percent Frequency Percent
–
cap 6 20.7 esp 8 27.6 ice 4 13.8 kon 11 37.9
Frequency Missing = 1
6 20.7
14 48.3
18 62.1
29100.0
Cumulative Cumulative WINDOW Frequency Percent Frequency Percent
– d 13 43.3 13 43.3 w 17 56.7 30 100.0
It is best to use the TABLES statement to specify the variables for which a frequency distribution or cross-tabulation is desired. Failing to do so will result in a frequency distribution which lists all the unique values of any continuous variables in the data set as well as the categorical ones. It is good practice to include the DATA= option especially when using multiple data sets . More than one TABLE statement can be used in PROC FREQ, and more than one table request can be made on each TABLE statement.
We can divide all of the statements and options available in PROC FREQ into three primary categories:
Controlling the frequency or cross-tabulation output as far as content and appearance is concerned
Requesting statistical tests or measures
Writing tables and results to SAS data sets.
I will begin by addressing one-way tables. Those readers already familiar with one-way tables and the options that can be used with them may wish to skip to the section on two-way and higher tables.
ONE-WAY TABLES
The simplest output from PROC FREE is a one-way frequency table which lists the unique values of the variable, a count of the number of observations at each value, the percent this count represents, a cumulative count and a cumulative percent.
Suppose that we have data on the pain level experienced 24 hours after one of 3 different surgical procedures used to repair a hernia is performed. The data consists of 3 variables: the
Beginning Tutorials
medical center where the procedure was performed, the procedure performed and the level of pain (none, tolerable, intolerable) reported by the patient 24 hours later. The data is shown in the following data step:
Data pain ;
input site group pain ;
labelsite = Test Center
group = Procedure
pain = Pain Level ;
cards; 112 120 121
To obtain a frequency distribution of the pain level, we would run the following:
proc freq data=pain;
tables pain;
which would result in the one-way table in Output 2. Output 2. One-way Frequency for Pain Data.
In the column with the heading PAIN we see the 3 unique values for pain in the data: 0,1 and 2. The Frequency column shows the number of observations for each value of PAIN and the Percent column shows what percent of the data have each value of PAIN. The Cumulative Frequency and Cumulative Percent are simply running totals. For PAIN = 1, the cumulative frequency of 21 means that 21 cases have a PAIN value of 1 or 0. The corresponding cumulative percent of 77.8% is obtained by dividing the cumulative frequency, 21, by the total count, 27, and multiplying by 100.
The cumulative frequency and percent are most meaningful if the variable is at least ordinal. In this example, the variable PAIN is ordinal, i.e. the pain level increases as the value increases, and it makes sense to say 21 cases, or 77.8% of the sample, reported a pain level of 1 or less.
If we use PROC FORMAT to define a format to be used for the variable PAIN, we can then include a format statement in PROC FREE as in the statements below which result in the output shown in Output3.
proc format;
value pain 0=None 1=Tolerable
2=Intolerable ;
proc freq data=pain;
tables pain;
format pain pain.;
Output 3. Using the FORMAT statement in PROC FREE
Notice that the numeric values of PAIN have been replaced with some meaningful description.
Now lets say that the variable GROUP is coded such that Procedures A, B and C are coded as 2, 1, and 3 respectively. A format can be defined and used in PROC FREE to produce the one-way table in Output 4.
Output 4. Using the FORMAT statement in PROC FREE
Notice that procedure B appears before procedure A. By default, PROC FREE orders the data according to the unformatted values of the variable, and since the unformatted value of GROUP for procedure A is 2, it comes after 1 which is the unformatted value for procedure B. We can rectify the situation by using the option ORDER=FORMATTED in the PROC FREE statement.
Output 5 shows the one-way table obtained from the following code:
proc freq data=pain order=formatted; tables group;
format pain pain.;
Output 5. Using ORDER=FORMATTED
Assume that the differences between procedures A, B and C are qualitative rather than quantitative. The variable GROUP is therefore a nominal variable whose values have no natural order and the cumulative frequencies and percent produced by PROC FREE are rather meaningless. Including the option NOCUM on the TABLES statement will eliminate the cumulative frequency and percent from the table as shown in Output 6.
Pain Data with FORMAT statement
Pain Level
Cumulative Cumulative PAIN Frequency Percent Frequency Percent
Intolerable
6 22.2
15 55.6
6 22.2
6 22.2
21 77.8
27100.0
Pain Data with FORMAT statement
Cumulative Cumulative GROUP Frequency Percent Frequency Percent – B 9 33.3 9 33.3 A 9 33.3 18 66.7 C 9 33.3 27 100.0
Pain Level
Cumulative Cumulative PAIN Frequency Percent Frequency Percent
0 6 22.2 6 22.2 1 15 55.6 21 77.8 2 6 22.2 27 100.0
Pain Data ORDER=FORMATTED
Cumulative Cumulative GROUP Frequency Percent Frequency Percent A 9 33.3 9 33.3 B 9 33.3 18 66.7 C 9 33.3 27 100.0
Beginning Tutorials
proc freq data=pain order=formatted; title1 Pain Data ;
title2 ;
tables group / nocum;
format group group. ;
Output 6. Cumulative Frequency and Percent Eliminated form the Output: NOCUM
proc freq data=asthma;
weight count;
tables asthma;
format asthma asth.;
Output 8. Cumulative Frequency and Percent Eliminated form the Output: NOCUM
Of the options available on the TABLES statement, only NOCUM, NOPERCENT, NOPRINT and OUT=
The NOPRINT option may be used if we want to use the frequencies or percents from the one-way table as input to a data step, macro or other SAS program for customized processing.
The options for statistical tests or measures do not produce any output for one-way tables. However, one might be interested in computing a 95% confidence interval for a single proportion from a one-way table. Consider the data in example 13.1 of Common Statistical Methods for Clinical Research with SAS Examples by. The data is from a study in which 25 patients with genital warts are given a combination of treatments which resulted in 14 patients being cured. The standard treatment has a known cure rate of 40% and so the question is whether the success rate of the combination of treatments is consistent with that of the standard treatment. We can compute a 95% confidence interval for the proportion of successful treatments based on the study and then see if the interval includes .4 (40%) or not. To do this we will make use of the WEIGHT statement and the OUT= option in PROC FREE. The DATA step, PROC FREE statements and resulting output follow.
data ex13_1;
input cured $ count;
proc freq data=ex13_1;
weight count;
tables cured / nocum out=curedci ;
The option NOCUM has suppressed the printing of the cumulative counts and frequencies which are not necessary as there are only twocategories. TheOUT=CUREDCIhaswrittenthetableas shown to a SAS data set named CUREDCI. The data set CUREDCI has 3 variables (CURED, COUNT and PERCENT) and 2 observations (one for NO, one for YES).
Pain Data using NOCUM Option
GROUP Frequency Percent A 933.3 B 933.3 C 933.3
Asthma Data Using WEIGHT statement
Cumulative Cumulative ASTHMA Frequency Percent Frequency Percent
– No Asthma 200 66.7 200 66.7 Asthma 100 33.3 300 100.0
Notice the absence of the cumulative frequency and cumulative percent. Output 7 shows that we can also eliminate the percent column by including the option NOPERCENT in the TABLES statement:
proc freq data=pain order=formatted; tables group / nocum nopercent;
format group group. ;
Output 7. Result of using NOCUM and NOPERCENT
In the Pain data example, we have the actual data from which to create the one-way frequency tables. That is, the data above consists of the response for each patient in each group at each site.
The WEIGHT statement is used when we already have the counts. For example, if we are told that in a study to estimate the prevalence of asthma, 300 people from 3 different cities were examined with the following results: of: of the 100 people examined in each city, 35, 40 and 25 were found to have asthma in Los Angeles, and St. Louis respectively. The following data step reads the summarized data, and then using the WEIGHT statement in PROC FREE, we produce a one-way table ( Output 8) for the overall sample prevalence of asthma in the 3 cities combined.
data asthma;
input city asthma count;
proc format;
value asth 0=No Asthma 1 Asthma ;
Pain Data Using NOCUM and NOPERCENT –
GROUP Frequency A9 B9 C9
Beginning Tutorials
Output 9. One-way Table for Data froms Example 13.1
The following data step uses this output data set to compute an approximate 95% confidence interval for the cure rate.
data curedci; set curedci ;
P = PERCENT/100 ;
N = COUNT + LAG(COUNT) ;
LB = P ( 1.96*SQRT( P*(1-P)/N ) ) ; * reset lower bound to 0 if <0 ;IF LB < 0 THEN LB = 0 ;UB = P + ( 1.96*SQRT( P*(1-P)/N ) ) ; * reset upper bound to 1 if >1 ;
IF UB > 1 Then UB = 1 ;
if _N_ = 2;
label p = Proportion Cured
LB = Lower Bound
UB = Upper Bound ;
Output 10 shows the results of the above data step when printed using PROC PRINT. While it is not within the scope of this paper to explain the details of the data step, the code is included here for illustrative purposes. From the printout we see that a 40% cure rate is included in the 95% confidence interval and so we would conclude that there is no evidence that the combination of treatments is any different form the standard treatment.
Output 10. Printout of Approximate 95% Confidence Interval for a Single Proportion
In version 8 of SAS, the BINOMIAL option on the TABLES statement will yield similar results to those in Output 10 without any extra steps. Output 11 shows the results of running the following PROC FREE in version 8. Note that the EXACT statement has also been included. This is available in version 8 and is used here to obtain the exact test in addition to the test using the normal approximation
proc freq data=ex13_1 order=data;
weight count;
EXACT BINOMIAL;
tables cured / nocum BINOMIAL (P=0.4) ;
Notice that the TABLES statement above includes (P=0.4) in addition to BINOMIAL. This specifies that the null hypothesis to be tested is H0:p=0.4. If (P= value) is omitted then the null hypothesis tested will be H0:p=0.5 .
Output 11. Results with BINOMIAL Option and EXACT statement
Data from Example 13.1
CURED Frequency Percent NO 11 44.0 YES 14 56.0
The FREE Procedure
cured Frequency Percent YES 14 56.00 NO 11 44.00
Binomial Proportion for cured = YES
Proportion (P)
95% Lower Conf Limit
95% Upper Conf Limit
Exact Conf Limits
95% Lower Conf Limit
95% Upper Conf Limit
Test of H0: Proportion = 0.4
ASE under H0
One-sided Pr >Z
Two-sided Pr > |Z|
Exact Test
One-sided Pr >=P
Two-sided = 2 * One-sided
There are 2 sets of confidence limits given in Output 11. The first set, identical to the limits in Output 10, are an approximation based on the normal distribution. The next set, labeled Exact Conf Limits are based on the Binomial distribution.
Results based on exact tests are especially desirable when the sample size is small.
TWO-WAY TABLES
The 22 Table
The simplest cross-tabulation is a 22 (said 2 by 2) table. It is called a 22 table because it is a cross-tabulation of two categorical variables, each with only two categories. In a cross- tabulation, one variable will be the row variable and the other will be the column variable.
The general form of a 22 cross-tabulation is shown in Figure 1. There are 4 cells. A cell is one combination of categories formed from the column and row variables.
Approximate 95% Confidence Interval For Proportion Cured
Proportion
Lower Bound
Upper Bound
A 22 table
Row Variable
Column Variable 12
2 c d r2 c1 c2 n
In Figure 1, a is the number of cases that are in category one of the column variable and category one of the row variable while b is the number of cases in category two of the column variable and
Beginning Tutorials
category one of the row variable. The total number of cases in category one of the row variable is r1=a+b, while the total number of cases in the second category of the row variable is r2=c+d. Similarly, the total number of cases in the first and second categories of the column variable are c1= a+c and c2 = b+d respectively. The total number of cases is n = a+b+c+d = r1+r2 = c1+c2.
Consider the RESPIRE data described on page 41 of Categorical DataAnalysisUsingtheSASSystem. Thevariablesinthedata set are TREATMNT, a character variable containing values for treatment (placebo or test), RESPONSE, another character variable containing the value of response defined as respiratory improvement (y=yes, n=no), CENTER, a numeric variable containing the number of the center at which the study was performed, and COUNT, a numeric variable containing the number of observations that have the respective TREATMNT, RESPONSE and CENTER values. To obtain the cross-tabulation of treatment with response shown in Output 12, we use the PROC FREE statement:
proc freq data=respire;
weight count;
tables treatmnt*response;
Note that the WEIGHT statement was used and that there were no
options specified on the TABLES statement. Output 12. Respire Data: 22 Table of
TREATMNT*RESPONSE
row percent of 42.22% indicates that 42.22% (38/90) of the cases receiving placebo responded and the column percent of 36.54% indicates that 36.54% (38/104) of the cases responding were given placebo.
Suppose we are interested in comparing the response rates between treatments. In this case we may want to ignore the column percent as well as the overall percent. The printing of the percent and column percent in each cell can be suppressed by specifyingtheNOPERCENTandNOCOLoptionsontheTABLES statement. Output 13 shows the result of running the following statements:
proc freq data=respire;
weight count;
tables treatmnt*response / nocol nopercent;
Output 13. 22 Table with Percent and Column Percent Values Suppressed
TABLE OF TREATMNT BY RESPONSE
TREATMNT RESPONSE
Frequency|
Col Pct |n |y | Total +++ placebo | 52| 38 | 90
|28.89 |21.11 |50.00
|57.78 |42.22 |
|68.42 |36.54 |
+++
test | 24| 66| 90 | 13.33 | 36.67 | 50.00
|26.67 |73.33 |
| 31.58 | 63.46 | +++
Total76104180
42.2257.78 100.00
In general, to obtain a 22 table using PROC FREE, simply include the statement
TABLES R*C ;
where R and C represent the row and column variables respectively.
Percent, Row Percent, Col Percent
CS: assignmentchef QQ: 1823890830 Email: [email protected]
Reviews
There are no reviews yet.