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 2025: Register Now

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!

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
  • 10540 views
  • 2 likes
  • 6 in conversation