Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating the MODE.

Status
Not open for further replies.

ChrisW75

Programmer
Dec 22, 2003
727
AU
Hi guys,
I've got a bit of a sticky problem here. I need to calculate the mode (most common value) across about half a dozen columns. There doesn't seem to be a "MODE" function though.
I was hoping I could do something like this:-
Code:
data _null_;
  a = 1;
  b = 4;
  c = 4;
  d = 6;
  e = 10;
  f = 12;
  g = 13;

  median = median(a,b,c,d,e,f,g);
  put median=;

  mode = mode(a,b,c,d,e,f,g);
  put mode=;
run;
but there's no MODE function.
It looks like I'm going to need to manually code this. Does anyone have any ideas? The data was originally given to me transpose, so I could have used Proc Univariate to get the mode out, however, there are 41,000 records, which became 41,000 columns, which SAS can't handle (it truncated at 32,767 columns).
Does anyone have any code, or an algorithm for working out the mode?
Cheers


Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
I got it in the end, but I doesn't feel particularly elegant...
Code:
data mode (drop=count1-count7 i j k compare biggest);
  a = 1;
  b = 4;
  c = 4;
  d = 6;
  e = 10;
  f = 12;
  g = 13;

  array vars {7}  4  a b c d e f g;
  array count {7} 4  count1-count7;

  do i = 1 to 7;
    compare = vars[i];
    do j = 1 to 7;
      if vars[j] = compare then count[i] + 1;
    end;
  end;

  biggest = max(of count1-count7);

  do k = 1 to 7;
    if count[k] = biggest then leave;
  end;

  mode = vars[k];

run;

I could probably use some macro code to make it more "automagical", but I pretty much decided by this point that it would be best to use Excel which has a MODE function built in.

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top