I have a couple of questions and let me know if I need to elaborate more!
1.) How can I get the top 5 and the bottom 5 observations of a (categorical) value while also showing the percentages ?
2.) How do I make it so a large piece of data where it's trying to find a relationship, download to excel? So far I've used:
proc compare base=work.details;
var income;
with ed;
title 'Comparison of Variables in the Same Data Set';
run;
However, it doesn't translate to Excel so there's not a way for me to make a graph.
1) Do you mean categories with the top 5 frequencies, or top 5 percents?
2) "How do I make it so a large piece of data where it's trying to find a relationship, download to excel?" Explain further, I don't understand.
1) You want to use PROC FREQ on your data, with the option ORDER=FREQ
2) You want to produce a scatter plot, you can use PROC SGPLOT with the SCATTER statement
It sounds like you want the Top and Bottom 5 categories by percentage. See that article, "An easy way to make a "Top 10" table and bar chart in SAS."
Here is an example of how to use the technique to get the Top and Bottom 5:
/* write the sorted frequencies to a data set */
proc freq data=sashelp.cars ORDER=FREQ noprint;
table make / out=FreqOut;
run;
/* print the top 5 */
%let MaxN = 5;
title "Top &MaxN Categories";
proc print data=FreqOut(obs=&MaxN);
run;
/* print the bottom 5 */
proc sort data=FreqOut; by percent; run;
title "Bottom &MaxN Categories";
proc print data=FreqOut(obs=&MaxN);
run;
Hello,
For 1.) , see these two blogs :
Selecting the top n% and bottom n% of observations from a data set
By Kathryn McLawhorn on SAS Users July 21, 2017
https://blogs.sas.com/content/sgf/2017/07/21/selecting-the-top-n-and-bottom-n-of-observations-from-a...
4 ways to find the k smallest and largest data values in SAS
By Rick Wicklin on The DO Loop January 26, 2022
https://blogs.sas.com/content/iml/2022/01/26/k-smallest-largest-data.html
Koen
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.