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;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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
  • 10762 views
  • 10 likes
  • 6 in conversation