When working with data, we frequently want to see how many number of times a particular value occurred in a variable across observations in the dataset.
We need programming features to achieve this. There are multiple ways in which you can get this result in SAS.
In this post, we will see how to get the result using frequency procedure (proc freq).
Let us assume that we have the dataset which has information for 19 students of a class.
Name |
Sex |
Age |
Height |
Weight |
Alfred |
M |
14 |
69 |
112.5 |
Alice |
F |
13 |
56.5 |
84 |
Barbara |
F |
13 |
65.3 |
98 |
Carol |
F |
14 |
62.8 |
102.5 |
Henry |
M |
14 |
63.5 |
102.5 |
James |
M |
12 |
57.3 |
83 |
Jane |
F |
12 |
59.8 |
84.5 |
Janet |
F |
15 |
62.5 |
112.5 |
Jeffrey |
M |
13 |
62.5 |
84 |
John |
M |
12 |
59 |
99.5 |
Joyce |
F |
11 |
51.3 |
50.5 |
Judy |
F |
14 |
64.3 |
90 |
Louise |
F |
12 |
56.3 |
77 |
Mary |
F |
15 |
66.5 |
112 |
Philip |
M |
16 |
72 |
150 |
Robert |
M |
12 |
64.8 |
128 |
Ronald |
M |
15 |
67 |
133 |
Thomas |
M |
11 |
57.5 |
85 |
William |
M |
15 |
66.5 |
112 |
Let us assume that we want to count the number of males and females in this data. The male/female information is stored in a variable named sex in this data.
To get the result, we can use the below sas code.
proc freq data=class;
tables sex;
run;
When the code is executed, we get the output below.
Most of the time, we need to store this output in a dataset so that we can reuse it in downstream processing. We can achieve this by using the code below.
proc freq data=class;
tables sex/out=counts;
run;