BookmarkSubscribeRSS Feed
hastm
Fluorite | Level 6

I am trying to replace all missing values with Mode. I have tried PROC STDIZE but it does not support Mode as replacement method.

 

proc stdize data = have  out = want  reponly  method = ???? ;
var _character_ ;
run;

4 REPLIES 4
Reeza
Super User
It does have an IN method that allows you to build your most freq table and then use that drive your recoding.
hastm
Fluorite | Level 6
can you please share some code to give me an idea
Reeza
Super User
Couple of minutes of searching found these, why don't you try some and see which one meets your needs. I unfortunately do not have the time to mock up example data and a solution at the moment. Someone else may offer an alternative option. The macro would be my starting approach.

https://www.listendata.com/2014/11/sas-macro-imputing-missing-data.html

https://stackoverflow.com/questions/26922779/fill-in-missing-values-with-mode-in-sas

Or something way more complicated:
https://stats.idre.ucla.edu/sas/seminars/multiple-imputation-in-sas/mi_new_1/
Ksharp
Super User
%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;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 2006 views
  • 2 likes
  • 3 in conversation