BookmarkSubscribeRSS Feed
GemmaR
SAS Employee

Hi Guys,

I’m trying to use the PROC MEANS to calculate the mode and output it to an output dataset but it seems that if the classficiation variable is unique then it returns a missing value.

Two questions, is that correct? And is there anyway around this?

Little bit of code attached to demonstrate.

Many thanks in advance for your help!

13 REPLIES 13
art297
Opal | Level 21

Not a perfect alternative, but the following will work for all cases except D where there aren't any non-unique replications:

proc univariate data=test modes;

          class char;

          var num;

          output out=test2 mode=mode;

run;

GemmaR
SAS Employee

Thanks Art, that's a solution I have come up with in the meantime but it's taking between a quarter and about a third longer to execute so I was hoping that I was missing something really simple in the proc means.

But at least I can say I have tried to find out when my customers start chuntering! Smiley Happy

Thanks again.

data_null__
Jade | Level 19

if n=1 mode=sum or mean or min or max .

art297
Opal | Level 21

DN: Please explain your comment.

data_null__
Jade | Level 19

The only differenc appears to be when N=1 so you can just make the MODE equal to the value. (MIN SUM MEAN MAX)

data test;

input char $ num;

datalines;

A 9

B 8

B 8

B 7

B 7

C 8

D 6

D 5

E 5

E 5

;

run;

proc summary data=test nway;

      class char;

      var num;

      output out=test2 mode=mode n=n min=min;

   run;

data test2;

   set test2;

   if n eq 1 then mode=min;

   run;

proc print;

   run;

proc univariate data=test noprint;

   class char;

   var num;

   output out=test3 mode=mode n=n min=min;

   run;

Proc print;

   run;

Proc compare base=test2 compare=test3;

   run;

art297
Opal | Level 21

Thanks!  I was wondering what you were referring to.

There is another problem, though, in that proc means and summary don't output multiple modes when they exist.  They simply select (as I recall) the lowest value.

data_null__
Jade | Level 19

To getmulti-modal output you need use the ODS OUTPUT data set from UNIVARIATE.  I can't recall the name.

I don't know when the mode is an interesting statistic.  I can't recall every putting it into a phama data summary.  

art297
Opal | Level 21

It is definitely of interest if one is trying to use stats to analyze their data, as a multi-modal distribution could easily violate the statistic's assumptions.  Of course, I am not a statistician Smiley Happy

data_null__
Jade | Level 19

Multi what?

art297
Opal | Level 21

OK, besides not being a programmer or statistician, I can't type either!  I changed multi-nomal to multi-modal in my response.

Ksharp
Super User

I think SAS is right.

mode is the number which has the biggest frequence. So If you only have one value or

several value has the same frequence, SAS don't know which value is the mode.

data test;
input char $ num;
datalines;
A 9
B 8
B 8
B 7
B 7
C 8
D 6
D 5
E 5
E 5
;
run;
proc freq data=test noprint;
table char*num/out=freq nopercent nocum;
run;
proc sort data=freq;by char descending count;run;
data want(keep=char num);
 set freq;
 by char;
 if first.char;
run;


Ksharp

art297
Opal | Level 21

Ksharp,  I don't understand what you are saying.  If there is an N-way tie for the most frequent response, all N values involved in the tie constitute the modes of the distribution.

Ksharp
Super User

Ou............... My bad .

data test;
input char $ num;
datalines;
A 9
B 9
B 8
B 7
B 7
C 8
D 6
D 5
E 5
E 5
;
run;
proc freq data=test noprint;
table char*num/out=freq nopercent nocum;
run;
proc sort data=freq;by char descending count;run; 
data want(keep=char num);
 set freq;
 by char count notsorted;
 retain found;
 if first.char then found=0;
 if not found then output;
 if last.count then found=1;
run;


Ksharp

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 13 replies
  • 19544 views
  • 3 likes
  • 4 in conversation