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...
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';
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';
I didn't know you could use formats for this. That opens up a lot of possibilities! Thanks for the help!
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.