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

That worked fine,

Thanks again! You saved my life.

Have great day.

Hitesh

Hitesh
Fluorite | Level 6

It worked very well..I got the right answer..

Thanks again

Dengyoh
Calcite | Level 5

Hi Xie Keshan,

Thanks for sharing your code - I'm new to SAS and still trying to get my head around how the code works but it does!

I was wondering what needs to be changed in the code for it to return the first variable (in this instance age1) if there are multiple modes instead of returning a random value?

Many thanks!

Ksharp
Super User

Code: Program


data x;
infile cards truncover;
input ID   $  Age1   Age2   Age3   Age4   Age5   Age6   ;
cards;
A01 10.0 . . . 10.0 11.0 10
A03 . . . 13.1 12.0 13.5 
A05 . 12.0 12.0 12.0 12.5 13.0 12
A07 11.8 11.0 11.0 . 13.0 10.0 11
A09 14.0 14.0 10.0 . 10.0 15.0 
A11 . . 17.0 . 16.2 17.0 17
A13 11.0 . 11.3 11.0 11.3 11.0 11
A15 . . . 10.7 11.0 10.7 10.7
A17 . 14.0 . 12.1 14.0 12.1 
A19 14.0 . . . . . 14
A21 . 12.0 . . . . 12
A23 . . . . 12.0 14.0 
A25 12.5 13.0 12.7 13.5 11.9 12.6
;
run;

data want(drop=k count i idx max );
if _n_ eq 1 then do;
declare hash ha();
declare hiter hi('ha');
  ha.definekey('k');
  ha.definedata('k','count','idx');
  ha.definedone();

declare hash random(ordered:'d');
declare hiter hir('random');
  random.definekey('idx');
  random.definedata('k','count');
  random.definedone();

end;
set x;
  array x{*} age: ;
do i=dim(x) to 1 by -1;
if not missing(x{i}) then do;
   k=x{i};idx=i;
   if ha.find()=0 then do;count+1;ha.replace();end;
   else do;count=1;ha.add();end;
end;
end;
do while(hi.next()=0);
random.add();
end;
max=0;
do while(hir.next()=0);
if count ge max then do;max=count;mod=k;end;
end;
ha.clear();   random.clear();
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 18 replies
  • 10141 views
  • 10 likes
  • 6 in conversation