## PROC FREQ with levels

Solved
Occasional Contributor
Posts: 12

# PROC FREQ with levels

Hello, new-ish SAS user (about 3 months). I'm sure this has been answered before, but I don't know what to search for.

How does one go about grouping values together, for the purposes of something like PROC FREQ? As an example, let's say we have the following data set (Student Number, Teacher, Grade they got in the class):

``````DATA students;
input StudNo Teacher \$5. Grade \$2.;
datalines;
1001 Jones A
1002 Smith C
1005 Jones B
1008 Jones A
1013 Smith A
1015 Jones B
1020 Smith D
1025 Smith C
1027 Jones F
1028 Smith F
1030 Jones A
1033 Smith B
;
run;``````

In this example, I want to run a PROC FREQ to look at the distributions of grades that each teacher gives, grouping together "good" grades (A,B) and "bad" grades (C,D, and F). One way to do this would be the following:

``````DATA grades;
SET students;
run;

run;``````

So, the obvious question: how could one do this using only the PROC step? The way I did it above has the potential to be very inefficient for larger data sets...

Accepted Solutions
Solution
‎04-16-2018 05:49 PM
Super User
Posts: 6,769

## Re: PROC FREQ with levels

[ Edited ]

You would need to create a format to group.  For example:

proc format;

run;

Then apply the format when creating a table:

proc freq data=have;

run;

Issues to consider:  Was the original logic complete?  For example, could GRADE by "a" instead of "A", or could GRADE by blank on some observations?  Right now, all of those fall into the "bad" category.  There are ways to code around this, but you have to decide if these are real possibilities.

All Replies
Solution
‎04-16-2018 05:49 PM
Super User
Posts: 6,769

## Re: PROC FREQ with levels

[ Edited ]

You would need to create a format to group.  For example:

proc format;

run;

Then apply the format when creating a table:

proc freq data=have;

run;

Issues to consider:  Was the original logic complete?  For example, could GRADE by "a" instead of "A", or could GRADE by blank on some observations?  Right now, all of those fall into the "bad" category.  There are ways to code around this, but you have to decide if these are real possibilities.

Occasional Contributor
Posts: 12