<?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: Filling the missing value with previous values that less than 5 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Filling-the-missing-value-with-previous-values-that-less-than-5/m-p/564429#M158320</link>
    <description>&lt;P&gt;Something like this should work (untested, I don't have SAS handy) :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT; 
  do until(last.FIRM);
   set HAVE; 
   by FIRM;
   VAL1NMISS=sum(VAL1NMISS, missing(VALUE1));
   VAL2NMISS=sum(VAL2NMISS, missing(VALUE2));
   VAL3NMISS=sum(VAL3NMISS, missing(VALUE3));
  end;
  do until(last.FIRM);
   set HAVE; 
   by FIRM;
   VALUE1=ifn(VAL1NMISS&amp;lt;5 &amp;amp; VALUE1=. &amp;amp; ^first.FIRM, lag(VALUE1), VALUE1);
   VALUE2=ifn(VAL2NMISS&amp;lt;5 &amp;amp; VALUE2=. &amp;amp; ^first.FIRM, lag(VALUE2), VALUE2);
   VALUE3=ifn(VAL3NMISS&amp;lt;5 &amp;amp; VALUE3=. &amp;amp; ^first.FIRM, lag(VALUE3), VALUE3);
   output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 07 Jun 2019 11:07:43 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2019-06-07T11:07:43Z</dc:date>
    <item>
      <title>Filling the missing value with previous values that less than 5</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-the-missing-value-with-previous-values-that-less-than-5/m-p/563965#M158151</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I really need help in filling the missing value.&lt;/P&gt;&lt;P&gt;I want to fill the missing value of value1, value2 and value3 by ''firm'' with last observation.&lt;/P&gt;&lt;P&gt;But I just want to fill the variable of each firm that with the missing value that less than 5.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;input firm $2. year Month value1 value2 value3;&lt;BR /&gt;cards;&lt;BR /&gt;A 2006 03 7 7 5&lt;BR /&gt;A 2006 03 8 8 .&lt;BR /&gt;A 2006 03 . 9 1&lt;BR /&gt;A 2006 06 5 . .&lt;BR /&gt;A 2006 06 8 7 4&lt;BR /&gt;A 2006 06 . 4 3&lt;BR /&gt;A 2006 09 . . 2&lt;BR /&gt;A 2006 09 9 7 1&lt;BR /&gt;A 2006 09 . . 5&lt;BR /&gt;A 2007 03 . 7 .&lt;BR /&gt;A 2007 03 . 7 8&lt;BR /&gt;A 2007 03 . 7 .&lt;BR /&gt;A 2007 06 . 3 2&lt;BR /&gt;A 2007 06 . 7 6&lt;BR /&gt;A 2007 06 . 8 .&lt;BR /&gt;B 2006 03 . 2 1&lt;BR /&gt;B 2006 03 12 7 .&lt;BR /&gt;B 2006 03 10 . .&lt;BR /&gt;B 2007 03 9 1 2&lt;BR /&gt;B 2007 03 1 . .&lt;BR /&gt;B 2007 03 . 3 4&lt;BR /&gt;B 2007 03 . . .&lt;BR /&gt;B 2007 03 . 3 4&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want:&lt;/P&gt;&lt;P&gt;A 2006 03 7 7 5&lt;BR /&gt;A 2006 03 8 8 &lt;STRONG&gt;5&lt;/STRONG&gt;&lt;BR /&gt;A 2006 03 . 9 1&lt;BR /&gt;A 2006 06 5 &lt;STRONG&gt;9&lt;/STRONG&gt; &lt;STRONG&gt;1&lt;/STRONG&gt;&lt;BR /&gt;A 2006 06 8 7 4&lt;BR /&gt;A 2006 06 . 4 3&lt;BR /&gt;A 2006 09 . &lt;STRONG&gt;4&lt;/STRONG&gt; 2&lt;BR /&gt;A 2006 09 9 7 1&lt;BR /&gt;A 2006 09 . &lt;STRONG&gt;7&lt;/STRONG&gt; 5&lt;BR /&gt;A 2007 03 . 7 &lt;STRONG&gt;5&lt;/STRONG&gt;&lt;BR /&gt;A 2007 03 . 7 8&lt;BR /&gt;A 2007 03 . 7 &lt;STRONG&gt;8&lt;/STRONG&gt;&lt;BR /&gt;A 2007 06 . 3 2&lt;BR /&gt;A 2007 06 . 7 6&lt;BR /&gt;A 2007 06 . 8 &lt;STRONG&gt;6&lt;/STRONG&gt;&lt;BR /&gt;B 2006 03 . 2 1&lt;BR /&gt;B 2006 03 12 7 &lt;STRONG&gt;1&lt;/STRONG&gt;&lt;BR /&gt;B 2006 03 10 &lt;STRONG&gt;7&lt;/STRONG&gt; &lt;STRONG&gt;1&lt;/STRONG&gt;&lt;BR /&gt;B 2007 03 9 1 2&lt;BR /&gt;B 2007 03 1 &lt;STRONG&gt;1&lt;/STRONG&gt; &lt;STRONG&gt;2&lt;/STRONG&gt;&lt;BR /&gt;B 2007 03 &lt;STRONG&gt;1&lt;/STRONG&gt; 3 4&lt;BR /&gt;B 2007 03 &lt;STRONG&gt;1&lt;/STRONG&gt; &lt;STRONG&gt;3&lt;/STRONG&gt; &lt;STRONG&gt;4&lt;/STRONG&gt;&lt;BR /&gt;B 2007 03 &lt;STRONG&gt;1&lt;/STRONG&gt; 3 4&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So there are two requirements when filling the missing value:&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. Fill the missing value of each variable by 'firm' with previous data.&lt;/P&gt;&lt;P&gt;2. If the number of missing value &amp;gt; 5, then we do not fill that variable. In this case, because the number of the missing value of value1 of firm A &amp;gt; 5, then it will not be filled by the lasted data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jun 2019 04:49:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-the-missing-value-with-previous-values-that-less-than-5/m-p/563965#M158151</guid>
      <dc:creator>Maomao_Hui</dc:creator>
      <dc:date>2019-06-06T04:49:27Z</dc:date>
    </item>
    <item>
      <title>Re: Filling the missing value with previous values that less than 5</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-the-missing-value-with-previous-values-that-less-than-5/m-p/564197#M158228</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/230213"&gt;@Maomao_Hui&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't specify it, byt it seems from your want-data that rule 2 applies only to value1. The following code gives your wanted result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input firm $2. year Month value1 value2 value3;
cards;
A 2006 03 7 7 5
A 2006 03 8 8 .
A 2006 03 . 9 1
A 2006 06 5 . .
A 2006 06 8 7 4
A 2006 06 . 4 3
A 2006 09 . . 2
A 2006 09 9 7 1
A 2006 09 . . 5
A 2007 03 . 7 .
A 2007 03 . 7 8
A 2007 03 . 7 .
A 2007 06 . 3 2
A 2007 06 . 7 6
A 2007 06 . 8 .
B 2006 03 . 2 1
B 2006 03 12 7 .
B 2006 03 10 . .
B 2007 03 9 1 2
B 2007 03 1 . .
B 2007 03 . 3 4
B 2007 03 . . .
B 2007 03 . 3 4
;
run;

data want; set test; by firm;
	retain ovalue1 ovalue2 ovalue3;
	drop ovalue1 ovalue2 ovalue3;
	if value1 = . then value1 = ovalue1;
	if value2 = . then value2 = ovalue2;
	if value3 = . then value3 = ovalue3;
	if value1 &amp;lt; 5 then ovalue1 = value1; else ovalue1 = .;
	if value2 &amp;gt; . then ovalue2 = value2; else ovalue2 = .;
	if value3 &amp;gt; . then ovalue3 = value3; else ovalue3 = .;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Jun 2019 16:34:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-the-missing-value-with-previous-values-that-less-than-5/m-p/564197#M158228</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-06-06T16:34:41Z</dc:date>
    </item>
    <item>
      <title>Re: Filling the missing value with previous values that less than 5</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-the-missing-value-with-previous-values-that-less-than-5/m-p/564422#M158315</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12887"&gt;@ErikLund_Jensen&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much for your kind reply. I am so sorry that I am not clear enough. Actually, I just want to fill the variables with few missing values. For example, the number of the missing value of value1 of firm A is 10, so I ignore it.&lt;/P&gt;&lt;P&gt;Firm A, the &lt;STRONG&gt;number&lt;/STRONG&gt; of the missing value of value1 is &lt;STRONG&gt;10&lt;/STRONG&gt;, &lt;STRONG&gt;not fill it&lt;/STRONG&gt;.&amp;nbsp; The number of the missing value of value 2 is 3, fill it. The &lt;STRONG&gt;number&lt;/STRONG&gt; of the missing value of value 3 of firm A is &lt;STRONG&gt;5&lt;/STRONG&gt;, so should also &lt;STRONG&gt;not be filled&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;Firm B, the number of the missing value of value1 is 3, fill it.&amp;nbsp; The number of the missing value of value 2 is 4, fill it. The number of the missing value of value 3 is 4, fill it.&lt;/P&gt;&lt;P&gt;Hope this time my explanation would be clearer, and thanks again for your quick reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hui&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; test&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;input&lt;/SPAN&gt; firm &lt;SPAN class="token punctuation"&gt;$&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;2&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;year&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;Month&lt;/SPAN&gt; value1 value2 value3&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token keyword"&gt;cards&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;SPAN class="token data string"&gt;A 2006 03 7 7 5
A 2006 03 8 8 .
A 2006 03 . 9 1
A 2006 06 5 . .
A 2006 06 8 7 4
A 2006 06 . 4 3
A 2006 09 . . 2
A 2006 09 9 7 1
A 2006 09 . . 5
A 2007 03 . 7 .
A 2007 03 . 7 8
A 2007 03 . 7 .
A 2007 06 . 3 2
A 2007 06 . 7 6
A 2007 06 . 8 .
B 2006 03 . 2 1
B 2006 03 12 7 .
B 2006 03 10 . .
B 2007 03 9 1 2
B 2007 03 1 . .
B 2007 03 . 3 4
B 2007 03 . . .
B 2007 03 . 3 4&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; want;&lt;BR /&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;A 2006 03 7 7 5
A 2006 03 8 8 .
A 2006 03 . 9 1
A 2006 06 5 &lt;STRONG&gt;9&lt;/STRONG&gt; .
A 2006 06 8 7 4
A 2006 06 . 4 3
A 2006 09 . &lt;STRONG&gt;4&lt;/STRONG&gt; 2
A 2006 09 9 7 1
A 2006 09 . &lt;STRONG&gt;7&lt;/STRONG&gt; 5
A 2007 03 . 7 .
A 2007 03 . 7 8
A 2007 03 . 7 .
A 2007 06 . 3 2
A 2007 06 . 7 6
A 2007 06 . 8 .
B 2006 03 . 2 1
B 2006 03 12 7 &lt;STRONG&gt;1&lt;/STRONG&gt;
B 2006 03 10 &lt;STRONG&gt;7&lt;/STRONG&gt; &lt;STRONG&gt;1&lt;/STRONG&gt;
B 2007 03 9 1 2
B 2007 03 1 &lt;STRONG&gt;1&lt;/STRONG&gt; &lt;STRONG&gt;2&lt;/STRONG&gt;
B 2007 03 &lt;STRONG&gt;1&lt;/STRONG&gt; 3 4
B 2007 03 &lt;STRONG&gt;1&lt;/STRONG&gt; &lt;STRONG&gt;3&lt;/STRONG&gt; &lt;STRONG&gt;4&lt;/STRONG&gt;
B 2007 03 &lt;STRONG&gt;1&lt;/STRONG&gt; 3 4&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jun 2019 10:17:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-the-missing-value-with-previous-values-that-less-than-5/m-p/564422#M158315</guid>
      <dc:creator>Maomao_Hui</dc:creator>
      <dc:date>2019-06-07T10:17:41Z</dc:date>
    </item>
    <item>
      <title>Re: Filling the missing value with previous values that less than 5</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-the-missing-value-with-previous-values-that-less-than-5/m-p/564429#M158320</link>
      <description>&lt;P&gt;Something like this should work (untested, I don't have SAS handy) :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT; 
  do until(last.FIRM);
   set HAVE; 
   by FIRM;
   VAL1NMISS=sum(VAL1NMISS, missing(VALUE1));
   VAL2NMISS=sum(VAL2NMISS, missing(VALUE2));
   VAL3NMISS=sum(VAL3NMISS, missing(VALUE3));
  end;
  do until(last.FIRM);
   set HAVE; 
   by FIRM;
   VALUE1=ifn(VAL1NMISS&amp;lt;5 &amp;amp; VALUE1=. &amp;amp; ^first.FIRM, lag(VALUE1), VALUE1);
   VALUE2=ifn(VAL2NMISS&amp;lt;5 &amp;amp; VALUE2=. &amp;amp; ^first.FIRM, lag(VALUE2), VALUE2);
   VALUE3=ifn(VAL3NMISS&amp;lt;5 &amp;amp; VALUE3=. &amp;amp; ^first.FIRM, lag(VALUE3), VALUE3);
   output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 07 Jun 2019 11:07:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-the-missing-value-with-previous-values-that-less-than-5/m-p/564429#M158320</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-06-07T11:07:43Z</dc:date>
    </item>
    <item>
      <title>Re: Filling the missing value with previous values that less than 5</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-the-missing-value-with-previous-values-that-less-than-5/m-p/564438#M158325</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/230213"&gt;@Maomao_Hui&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think the code I supplied will create your new want-data set, if the rule is applied to all 3 variables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want; set test; by firm;
	retain ovalue1 ovalue2 ovalue3;
	drop ovalue1 ovalue2 ovalue3;
	if value1 = . then value1 = ovalue1;
	if value2 = . then value2 = ovalue2;
	if value3 = . then value3 = ovalue3;
	if value1 &amp;lt; 5 then ovalue1 = value1; else ovalue1 = .;
	if value2 &amp;lt; 5 then ovalue2 = value2; else ovalue2 = .;
	if value3 &amp;lt; 5 then ovalue3 = value3; else ovalue3 = .;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jun 2019 11:56:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-the-missing-value-with-previous-values-that-less-than-5/m-p/564438#M158325</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-06-07T11:56:54Z</dc:date>
    </item>
    <item>
      <title>Re: Filling the missing value with previous values that less than 5</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-the-missing-value-with-previous-values-that-less-than-5/m-p/564603#M158390</link>
      <description>&lt;P&gt;Thank you so much. I also made a bit adjustment as follow:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data WANT;&lt;BR /&gt;do until(last.FIRM);&lt;BR /&gt;set HAVE;&lt;BR /&gt;by FIRM;&lt;BR /&gt;VAL1NMISS=sum(VAL1NMISS, missing(VALUE1));&lt;BR /&gt;VAL2NMISS=sum(VAL2NMISS, missing(VALUE2));&lt;BR /&gt;VAL3NMISS=sum(VAL3NMISS, missing(VALUE3));&lt;BR /&gt;end;&lt;BR /&gt;do until(last.FIRM);&lt;BR /&gt;set HAVE;&lt;BR /&gt;by FIRM;&lt;BR /&gt;VALUE1=ifn(VAL1NMISS&amp;lt;5 &amp;amp; VALUE1=. &amp;amp; ^first.FIRM, coalesce(VALUE1,n1), VALUE1);n1=VALUE1;&lt;BR /&gt;VALUE2=ifn(VAL2NMISS&amp;lt;5 &amp;amp; VALUE2=. &amp;amp; ^first.FIRM, coalesce(VALUE2,n2), VALUE2);n2=VALUE2;&lt;BR /&gt;VALUE3=ifn(VAL3NMISS&amp;lt;5 &amp;amp; VALUE3=. &amp;amp; ^first.FIRM, coalesce(VALUE3,n3), VALUE3);n3=VALUE3;&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Sat, 08 Jun 2019 01:13:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-the-missing-value-with-previous-values-that-less-than-5/m-p/564603#M158390</guid>
      <dc:creator>Maomao_Hui</dc:creator>
      <dc:date>2019-06-08T01:13:49Z</dc:date>
    </item>
  </channel>
</rss>

