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

Hello All,

How can I find 3 most frequently occurring values in my dataset using SAS?

Regards,

Aleksandra

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

If you don't need a dataset Proc Freq with the order=freq will generate a table with the most frequent at the top and provide the count at the same time. This also has an advantage of easily adding multiple, or even all variables in a data set with one pass through the data though that can create a lot of output.

Proc freq data=haveorder=freq;

tables variable;

run;

View solution in original post

5 REPLIES 5
evp000
Quartz | Level 8

From one variable or more?  You could probably use the count() function with proc sql, get counts of your values then sort them.

beate
Fluorite | Level 6

Assuming your data are in mydat and the variable you would like to know the most frequent values of is myvar, you could use:

/* get frequency of myvar in your data set */

proc freq data=mydat;

tables myvar / noprint out=tmp (keep=myvar count);

run;

/* sort in descending order by frequency */

proc sort data=tmp;

by descending count;

run;

/* get the frequency of the third most frequent item, need to do this as there might be multiple value of myvar tied for this value; note that

   code will also work if there are fewer than 3 distinct values for myvar in your data set. */

data _NULL_;

set tmp;

if _n_ <= 3 then call symput('maxcount',count);

run;

/* only keep observations with a frequency of maxcount or higher */

data tmp;

set tmp (where=(count >= &maxcount));

run;

proc print data=tmp;

var myvar;

run;

Beate

ballardw
Super User

If you don't need a dataset Proc Freq with the order=freq will generate a table with the most frequent at the top and provide the count at the same time. This also has an advantage of easily adding multiple, or even all variables in a data set with one pass through the data though that can create a lot of output.

Proc freq data=haveorder=freq;

tables variable;

run;

SteveDenham
Jade | Level 19

That was the technique I was looking for, , when I came up with the other post on the other fork of this thread.  If I could hand out a Correct, you'd get it.

Steve Denham

Rick_SAS
SAS Super FREQ

And you can use a variation of 's answer to create a bar chart of the top k categories.

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 ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 9817 views
  • 2 likes
  • 6 in conversation