<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Help: Macro to replace missing values with the most frequent value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-Macro-to-replace-missing-values-with-the-most-frequent/m-p/665326#M198917</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
set sashelp.class;
if _n_ in (1 4 7) then call missing(age, sex);
run;




%MACRO impute_freq (dat, var_name);
proc freq data=&amp;amp;dat(where=(&amp;amp;var_name is not missing)) order=freq noprint;
table &amp;amp;var_name/out=mode;
run;
data &amp;amp;dat;
 set &amp;amp;dat;
 if _n_=1 then set mode(keep=&amp;amp;var_name rename=(&amp;amp;var_name=_&amp;amp;var_name));
 if missing(&amp;amp;var_name) then &amp;amp;var_name=_&amp;amp;var_name;
 drop _&amp;amp;var_name;
run;
%MEND impute_freq;

%impute_freq(have,age)
%impute_freq(have,sex)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 26 Jun 2020 12:15:14 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2020-06-26T12:15:14Z</dc:date>
    <item>
      <title>Help: Macro to replace missing values with the most frequent value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-Macro-to-replace-missing-values-with-the-most-frequent/m-p/665163#M198834</link>
      <description>&lt;P&gt;I'd like to write a macro to replace any missing variables (numeric or character) with the most frequent value of that variable in a new column.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't have much so far, but here is what I've come up with. I basically don't know how to determine the most frequent value.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%MACRO impute_freq (dat, var_name);&lt;/P&gt;&lt;P&gt;proc freq data = &amp;amp;dat;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;by descending &amp;amp;var_name;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%if &amp;amp;var_name = . %then&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; newcol =&amp;nbsp;???&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%else newcol = &amp;amp;var_name;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%MEND impute_freq;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 21:10:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-Macro-to-replace-missing-values-with-the-most-frequent/m-p/665163#M198834</guid>
      <dc:creator>meriS</dc:creator>
      <dc:date>2020-06-25T21:10:51Z</dc:date>
    </item>
    <item>
      <title>Re: Help: Macro to replace missing values with the most frequent value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-Macro-to-replace-missing-values-with-the-most-frequent/m-p/665166#M198836</link>
      <description>That usually is not the recommended approach to dealing with missing data though it's easily achievable. &lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/seminars/multiple-imputation-in-sas/mi_new_1/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/seminars/multiple-imputation-in-sas/mi_new_1/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;You can also look into PROC STDIZE which has several methods to replace missing values.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 25 Jun 2020 21:17:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-Macro-to-replace-missing-values-with-the-most-frequent/m-p/665166#M198836</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-06-25T21:17:36Z</dc:date>
    </item>
    <item>
      <title>Re: Help: Macro to replace missing values with the most frequent value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-Macro-to-replace-missing-values-with-the-most-frequent/m-p/665186#M198848</link>
      <description>&lt;P&gt;Yes, with PROC STDIZE and METHOD=IN(ds), no macros are needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I can't think of a good reason to use the most frequent value; I would recommend using either the mean or the median.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;UNTESTED CODE&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have;
    var variable1 variable2 ... ;
    output out=mode(drop=_:) mode=;
run;
data modes;
     set mode;
     array x variable1 variable2 ... ;
     _type_='LOCATION';
    output;
    do i=1 to dim(x);
         x(i)=1;
    end;
    _type_='SCALE';
    output;
    drop i;
run;
proc stdize data=have out=want reponly method=in(modes);
    var variable1 variable2 ... ;
run;

    &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But really, don't use the MODE, use either MEDIAN or MEAN. A word to the wise ...&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And in addition, if you want to replace missings with the median or mean, its even easier, as this is built directly into PROC STDIZE.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jun 2020 11:33:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-Macro-to-replace-missing-values-with-the-most-frequent/m-p/665186#M198848</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-06-26T11:33:49Z</dc:date>
    </item>
    <item>
      <title>Re: Help: Macro to replace missing values with the most frequent value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-Macro-to-replace-missing-values-with-the-most-frequent/m-p/665257#M198885</link>
      <description>&lt;P&gt;One way of doing it using a macro, despite being doable without&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro Assign_Mode_to_Null(table,var);
proc sql noprint;
select &amp;amp;var into :mode
from (select &amp;amp;var, count(&amp;amp;var) as N 
      from &amp;amp;table 
      group by &amp;amp;var)
having N=max(N) and monotonic()=1; 
quit;

data want (drop=dsid type rc); 
dsid=open("&amp;amp;table",'i'); 
type=vartype(dsid,varnum(dsid,"&amp;amp;var")); 
rc=close(dsid);
set &amp;amp;table;
if missing(&amp;amp;var) then do; 
if type='N' then &amp;amp;var=&amp;amp;mode; 
if type='C' then do; &amp;amp;var="&amp;amp;mode"; drop &amp;amp;mode; end;
end;
run;
%mend Assign_Mode_to_Null;

%Assign_Mode_to_Null(have,var_name);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jun 2020 07:33:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-Macro-to-replace-missing-values-with-the-most-frequent/m-p/665257#M198885</guid>
      <dc:creator>sustagens</dc:creator>
      <dc:date>2020-06-26T07:33:23Z</dc:date>
    </item>
    <item>
      <title>Re: Help: Macro to replace missing values with the most frequent value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-Macro-to-replace-missing-values-with-the-most-frequent/m-p/665326#M198917</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
set sashelp.class;
if _n_ in (1 4 7) then call missing(age, sex);
run;




%MACRO impute_freq (dat, var_name);
proc freq data=&amp;amp;dat(where=(&amp;amp;var_name is not missing)) order=freq noprint;
table &amp;amp;var_name/out=mode;
run;
data &amp;amp;dat;
 set &amp;amp;dat;
 if _n_=1 then set mode(keep=&amp;amp;var_name rename=(&amp;amp;var_name=_&amp;amp;var_name));
 if missing(&amp;amp;var_name) then &amp;amp;var_name=_&amp;amp;var_name;
 drop _&amp;amp;var_name;
run;
%MEND impute_freq;

%impute_freq(have,age)
%impute_freq(have,sex)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 26 Jun 2020 12:15:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-Macro-to-replace-missing-values-with-the-most-frequent/m-p/665326#M198917</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-06-26T12:15:14Z</dc:date>
    </item>
  </channel>
</rss>

