BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Schwa
Fluorite | Level 6

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...

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

2 REPLIES 2
Astounding
PROC Star

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';

Schwa
Fluorite | Level 6

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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