<?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: Efficiently find cells that have a symbol and replace with the minimum value across entire colum in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Efficiently-find-cells-that-have-a-symbol-and-replace-with-the/m-p/737746#M230016</link>
    <description>You may find this post informative, especially if you want to go beyond just replacing with minimum. It covers your use case as well.&lt;BR /&gt;&lt;A href="https://sasexamplecode.com/replace-missing-values-in-sas/" target="_blank"&gt;https://sasexamplecode.com/replace-missing-values-in-sas/&lt;/A&gt;</description>
    <pubDate>Wed, 28 Apr 2021 22:15:14 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2021-04-28T22:15:14Z</dc:date>
    <item>
      <title>Efficiently find cells that have a symbol and replace with the minimum value across entire column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficiently-find-cells-that-have-a-symbol-and-replace-with-the/m-p/737735#M230009</link>
      <description>&lt;P&gt;Hello, can someone please advise me on the best way to perform the following (whether through macro, array, and/or do loop)?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;For each variable, I want to search through all observations in the column and see if there is a "&amp;lt;" symbol. For variables with such a symbol, I want to completely ignore the number following the "&amp;lt;" and instead replace this entire observation with the minimum observation (smallest value) in the entire column. For variables without this symbol, I don't need to do anything and want to leave the data as is.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;VAR1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; VAR2&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;853.1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;FONT color="#993300"&gt;&amp;lt;12.1&lt;/FONT&gt; (&lt;EM&gt;replace this with the smallest value in the entire VAR2 column, ignoring 12.1 and any other observations that have "&amp;lt;"&lt;/EM&gt;)&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;173.4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;888.3&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;481.6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;385.2&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I attempted a macro, but it is not working, and now I wondering if an array or do loop would work better?&lt;/P&gt;
&lt;P&gt;The issue is that the variables WITH "&amp;lt;" are registered as characters by SAS, while the variables WITHOUT "&amp;lt;" are seen as numeric. And I have many variables, so I'm trying to figure out a way to efficiently apply code to all variables, leaving the variables without "&amp;lt;" alone.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;INCORRECT MACRO&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro imputation (var);



data tab1_&amp;amp;var; set data (keep = ID &amp;amp;var);

if not (find(&amp;amp;var,'&amp;lt;')) then do;
&amp;amp;var_good = 111111; end; /* essentially don't need to do anything here, but I didn't know how to get SAS to "do nothing" */

else if find(&amp;amp;var,'&amp;lt;') then do;
&amp;amp;var = 999999; end;



&amp;amp;var_ = &amp;amp;var * 1; /* convert to numeric */
drop &amp;amp;var &amp;amp;var_good; run;


proc sql;
create table tab2_&amp;amp;var as
select *, min(&amp;amp;var_) as min_&amp;amp;var&amp;nbsp; &amp;nbsp;/* find the min observation in entire column, create a new column with this value, and attach to the previous dataset*/
from tab1_&amp;amp;var;
quit;


data tab3_&amp;amp;var; set tab2_&amp;amp;var;

if &amp;amp;var_ = 999999 then &amp;amp;var = min_&amp;amp;var; /* only assign the minimum observation to cells that had a "&amp;lt;" */
else &amp;amp;var = &amp;amp;var_; run;


data table_&amp;amp;var; set tab3_&amp;amp;var (keep = ID &amp;amp;var); run;



proc sort data=table_&amp;amp;var;
by ID ;
run;

%mend;
run;



%imputation(var1);
%imputation(var2);
%imputation(var3);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;(The final step will be horizontally merge all the sorted datasets by ID... so that all data is now correct and complete).&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any guidance would be greatly appreciated! Thank you!!!&lt;/P&gt;</description>
      <pubDate>Wed, 28 Apr 2021 22:12:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficiently-find-cells-that-have-a-symbol-and-replace-with-the/m-p/737735#M230009</guid>
      <dc:creator>kai_cody</dc:creator>
      <dc:date>2021-04-28T22:12:44Z</dc:date>
    </item>
    <item>
      <title>Re: Efficiently find cells that have a symbol and replace with the minimum value across entire colum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficiently-find-cells-that-have-a-symbol-and-replace-with-the/m-p/737744#M230015</link>
      <description>&lt;P&gt;You might have somewhat better luck by making sure the variable is numeric. "Minimum" value of character variables is a problematic concept as with character values '1111111' is "less than" '9' because the comparisons are done character by character until there is a difference. So the first '1' is "less than" '9'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the variable is numeric, which means the "&amp;lt;12" would have been created as missing then the minimum function will return the correct non-missing value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One way &lt;STRONG&gt;after&lt;/STRONG&gt; you make or place the variables into numeric values.&lt;/P&gt;
&lt;PRE&gt;data have;
   input id var1  var2 var3;
datalines;
2 10   123.32  .4
3 .    0.19    .
5 10   .       1.2
1 25   44      2.3
7 .    55      0
;

Proc summary data=have;
   var var1 var2 var3;
   output out=work.impute min= /autoname
   ;
run;

Proc sql;
   create table imputed as
   select a.id, coalesce(a.var1,b.var1_min) as var1
          ,coalesce(a.var2,b.var2_min) as var2
          ,coalesce(a.var3,b.var3_min) as var3
   from have as a, work.impute as b
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;The coalesce function returns the first non-missing value in the parameters as examined from left to right.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To create numeric values from your apparent character values (otherwise the likely wouldn't be any &amp;lt;) then either reread the raw data with a proper numeric informat or in a data step use something like:&amp;nbsp;&amp;nbsp; newvar = input(oldvar, 8.);&lt;/P&gt;</description>
      <pubDate>Wed, 28 Apr 2021 22:06:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficiently-find-cells-that-have-a-symbol-and-replace-with-the/m-p/737744#M230015</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-04-28T22:06:59Z</dc:date>
    </item>
    <item>
      <title>Re: Efficiently find cells that have a symbol and replace with the minimum value across entire colum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficiently-find-cells-that-have-a-symbol-and-replace-with-the/m-p/737746#M230016</link>
      <description>You may find this post informative, especially if you want to go beyond just replacing with minimum. It covers your use case as well.&lt;BR /&gt;&lt;A href="https://sasexamplecode.com/replace-missing-values-in-sas/" target="_blank"&gt;https://sasexamplecode.com/replace-missing-values-in-sas/&lt;/A&gt;</description>
      <pubDate>Wed, 28 Apr 2021 22:15:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficiently-find-cells-that-have-a-symbol-and-replace-with-the/m-p/737746#M230016</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-04-28T22:15:14Z</dc:date>
    </item>
  </channel>
</rss>

