<?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 How to replace missing value with a nonmissing value within a group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-missing-value-with-a-nonmissing-value-within-a/m-p/695622#M212289</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset that each ID has multiple records. Within each group (ID), the variable A and B have only two values, missing and some other value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;/P&gt;
&lt;P&gt;input ID A B;&lt;/P&gt;
&lt;P&gt;cards;&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp;10&amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; 16&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; 16&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; 10&amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; 7&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I want to do is to replace the missing value with the non-missing value of the same variable within each group:&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp;10&amp;nbsp; 16&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp;10&amp;nbsp; 16&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp;10&amp;nbsp; 16&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp;10&amp;nbsp; 16&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp;7&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp;7&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp;7&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp;7&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I really appreciate any help with this question.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best regards,&lt;/P&gt;
&lt;P&gt;C&lt;/P&gt;</description>
    <pubDate>Sat, 31 Oct 2020 07:53:38 GMT</pubDate>
    <dc:creator>CynthiaWei</dc:creator>
    <dc:date>2020-10-31T07:53:38Z</dc:date>
    <item>
      <title>How to replace missing value with a nonmissing value within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-missing-value-with-a-nonmissing-value-within-a/m-p/695622#M212289</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset that each ID has multiple records. Within each group (ID), the variable A and B have only two values, missing and some other value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;/P&gt;
&lt;P&gt;input ID A B;&lt;/P&gt;
&lt;P&gt;cards;&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp;10&amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; 16&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; 16&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; 10&amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; 7&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I want to do is to replace the missing value with the non-missing value of the same variable within each group:&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp;10&amp;nbsp; 16&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp;10&amp;nbsp; 16&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp;10&amp;nbsp; 16&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp;10&amp;nbsp; 16&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp;7&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp;7&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp;7&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp;7&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I really appreciate any help with this question.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best regards,&lt;/P&gt;
&lt;P&gt;C&lt;/P&gt;</description>
      <pubDate>Sat, 31 Oct 2020 07:53:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-replace-missing-value-with-a-nonmissing-value-within-a/m-p/695622#M212289</guid>
      <dc:creator>CynthiaWei</dc:creator>
      <dc:date>2020-10-31T07:53:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace missing value with a nonmissing value within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-missing-value-with-a-nonmissing-value-within-a/m-p/695623#M212290</link>
      <description>&lt;P&gt;Use a double DO loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do until (last.id);
  set have;
  by id;
  _a = max(_a,a);
  _b = max(_b,b);
end;
do until (last.id);
  set have;
  by id;
  a = _a;
  b = _b;
  output;
end;
drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested, posted from my tablet.&lt;/P&gt;</description>
      <pubDate>Sat, 31 Oct 2020 08:10:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-replace-missing-value-with-a-nonmissing-value-within-a/m-p/695623#M212290</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-31T08:10:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace missing value with a nonmissing value within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-missing-value-with-a-nonmissing-value-within-a/m-p/695626#M212292</link>
      <description>&lt;P&gt;PS&lt;/P&gt;
&lt;P&gt;PLEASE (and that's a very big&amp;nbsp;&lt;STRONG&gt;PLEASE&lt;/STRONG&gt;) use the "little running man" button to post code. Really. I mean it.&lt;/P&gt;
&lt;P&gt;I positively hate to have to clean out all the iffy things (empty lines etc) that the main posting window does to code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW, I managed to run the code (after cleaning up your data step, see above) on SAS on Demand, so it's tested now.&lt;/P&gt;</description>
      <pubDate>Sat, 31 Oct 2020 08:22:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-replace-missing-value-with-a-nonmissing-value-within-a/m-p/695626#M212292</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-31T08:22:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace missing value with a nonmissing value within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-missing-value-with-a-nonmissing-value-within-a/m-p/695633#M212295</link>
      <description>&lt;P&gt;If it really is the case that there are non-missing values which are always the same within an ID, or missing values (never two different non-missing values within an ID), then in my opinion, the easiest solution is to use PROC STDIZE with the REPONLY option.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc stdize data=have reponly method=mean out=want;
by id;
var a b;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 31 Oct 2020 10:39:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-replace-missing-value-with-a-nonmissing-value-within-a/m-p/695633#M212295</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-10-31T10:39:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace missing value with a nonmissing value within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-missing-value-with-a-nonmissing-value-within-a/m-p/695647#M212307</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID A B;
cards;
1 10 .
1 . 16
1 . 16
1 10 .
2 5 .
2 5 .
2 5 .
2 . 7
;

data want(drop=aa bb);
   do _N_ = 1 by 1 until (last.ID);
      set have;
      by ID;
      aa = aa &amp;lt;&amp;gt; a;
      bb = bb &amp;lt;&amp;gt; b;
   end;
   do _N_ = 1 to _N_;
      set have;
      if a = . then a = aa;
      if b = . then b = bb;
      output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ID A  B 
1  10 16 
1  10 16 
1  10 16 
1  10 16 
2  5  7 
2  5  7 
2  5  7 
2  5  7 &lt;/PRE&gt;</description>
      <pubDate>Sat, 31 Oct 2020 13:01:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-replace-missing-value-with-a-nonmissing-value-within-a/m-p/695647#M212307</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-10-31T13:01:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace missing value with a nonmissing value within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-missing-value-with-a-nonmissing-value-within-a/m-p/695652#M212311</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/266374"&gt;@CynthiaWei&lt;/a&gt;&amp;nbsp; While it's a privilege to learn from the real "Proc Star-&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;" who would make SAS look innocent with his mastery of Procs, with yet another demonstration of a Proc that does with utmost elegance, here is another alternative of Datastep that utilizes the UPDATE construct to fill the missing values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID A B;
cards;
1 10 .
1 . 16
1 . 16
1 10 .
2 5 .
2 5 .
2 5 .
2 . 7
;

data want;
 do _n_=1 by 1 until(last.id);
  update have(obs=0) have;
  by id;
 end;
 do _n_=1 to _n_;
  output;
 end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The above technique might be a bit of learning curve when you intend to do further processing i.e. should the need is to go beyond &lt;EM&gt;after fill&amp;nbsp;&lt;/EM&gt;&amp;nbsp;in&amp;nbsp; a situation that would involve look up/matrix programming(&lt;EM&gt;hashes/multi dim arrays&lt;/EM&gt;) among others for further processing&lt;EM&gt;.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternatively, the MAX operator/function is indeed the traditional way as it works for both SQL/Datastep. For one, the syntax being convenient and many users are quite familiar with SQL as is often known as "readymeals" solution,&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
 create table want as
 select a.id,A,B
 from have(keep=id) a left join
 (select id, max(a) as A, max(b) as B from have group by id) b
 on a.id=b.id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you are familiar with automatic remerge functionality of Proc SQL, you might be tempted to attempt that auto fill, albeit the SQL processor wouldn't see the inclusion of a non summary list variable to force or trigger that. Therefore, you would need an explicit&amp;nbsp; self JOIN.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Fun stuff! Have a great day and Kind regards &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 31 Oct 2020 17:12:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-replace-missing-value-with-a-nonmissing-value-within-a/m-p/695652#M212311</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-10-31T17:12:20Z</dc:date>
    </item>
  </channel>
</rss>

