Help using Base SAS procedures

PROC FREQ with levels

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

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;
     length good_or_bad $4.;
     if (grade = 'A' or grade='B') then good_or_bad = 'good';
     else good_or_bad = 'bad';
     run;

PROC FREQ data=grades;
     TABLE teacher*good_or_bad;
     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
a week ago
Super User
Posts: 6,413

Re: PROC FREQ with levels

[ Edited ]

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

 

proc format;

value $goodbad 'A', 'B' = 'good' other='bad';

run;

 

Then apply the format when creating a table:

 

proc freq data=have;

tables teacher * grade;

format grade $goodbad.;

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.

 

value $goodbad 'A', 'a', 'B', 'b' = 'good'   other='bad';

View solution in original post


All Replies
Solution
a week ago
Super User
Posts: 6,413

Re: PROC FREQ with levels

[ Edited ]

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

 

proc format;

value $goodbad 'A', 'B' = 'good' other='bad';

run;

 

Then apply the format when creating a table:

 

proc freq data=have;

tables teacher * grade;

format grade $goodbad.;

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.

 

value $goodbad 'A', 'a', 'B', 'b' = 'good'   other='bad';

Highlighted
Occasional Contributor
Posts: 12

Re: PROC FREQ with levels

Posted in reply to Astounding

I didn't know you could use formats for this. That opens up a lot of possibilities! Thanks for the help!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 167 views
  • 3 likes
  • 2 in conversation