%let dsn= sashelp.heart;
ods select none;
ods output onewayfreqs=freq;
proc freq data=&dsn ;
table _character_ ;
run;
ods select all;
data mode;
set freq(drop=table);
_level_=coalescec(of _character_);
keep frequency _level_;
run;
data mode1;
merge freq(keep=table) mode;
run;
proc sort data=mode1 out=mode2;by table frequency;run;
data mode3;
set mode2;
by table;
table=upcase(scan(table,-1,' '));
if last.table;
drop frequency;
run;
data want;
set &dsn ;
array x{*} $ _character_;
if _n_=1 then do;
if 0 then set mode3;
declare hash h(dataset:'mode3');
h.definekey('table');
h.definedata('_level_');
h.definedone();
end;
do i=1 to dim(x);
if missing(x{i}) then do;h.find(key:upcase(vname(x{i})));x{i}=_level_;end;
end;
drop i _level_ table;
run;