That worked fine,
Thanks again! You saved my life.
Have great day.
Hitesh
It worked very well..I got the right answer..
Thanks again
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!
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.