<?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: Replacing Missing values with average in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709169#M218028</link>
    <description>&lt;P&gt;Question:&amp;nbsp; why are your data not ordered chronologically?&amp;nbsp; For instance the 3rd-5th rows are as below (note dates are dd/mm/yyyy):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;10343452 Zilas 03/04/2012 main   6.6
10343452 Zilas 04/04/2011 main   .
10343452 Zilas 05/04/2017 main  7.7&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you are estimating data for 4/4/2011&amp;nbsp; (04apr2011) from&amp;nbsp; &amp;nbsp;03apr2012&amp;nbsp; and 05april2017.&amp;nbsp; &amp;nbsp; &amp;nbsp;Really?&amp;nbsp; &amp;nbsp;2011 from 2012 and 2017?&lt;/P&gt;</description>
    <pubDate>Sun, 03 Jan 2021 19:10:13 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2021-01-03T19:10:13Z</dc:date>
    <item>
      <title>Replacing Missing values with average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709136#M218000</link>
      <description>&lt;P&gt;Hi to all , Wish you Happy New Year 2021!&lt;/P&gt;&lt;P&gt;I would like to replace missing values with average and the rest that have missing till the end to leave them as are.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input cusip_id $8. Name $6. date_issue :ddmmyy10. type $5. Ret  dollar8.1;
format date_issue ddmmyy10. ;
datalines;
10343452 Zilas 01/04/2016 main  3.2
10343452 Zilas 02/04/2014 main     4.5
10343452 Zilas 03/04/2012 main   6.6
10343452 Zilas 04/04/2011 main   .
10343452 Zilas 05/04/2017 main  7.7
10343452 Zilas 06/04/2017 main  .
10343452 Zilas 07/04/2017 main  .
10343452 Zilas 08/04/2017 main  .
10343453 Astra 01/04/2016 main  6.2
10343453 Astra 02/04/2014 main     5.5
10343453 Astra 03/04/2012 main   2.6
10343453 Astra 04/04/2011 main   .
10343453 Astra 05/04/2011 main   .
10343453 Astra 06/04/2017 main  4.7
10343453 Astra 07/04/2017 main  .
10343453 Astra 08/04/2017 main  8
10343453 Astra 09/04/2017 main  .
10343453 Astra 10/04/2017 main  .
10343453 Astra 11/04/2017 main  .
10343453 Astra 12/04/2017 main  .
10343453 Astra 13/04/2017 main  .
;
run;

data want;
input cusip_id $8. Name $6. date_issue :ddmmyy10. type $5. Ret  dollar8.1;
format date_issue ddmmyy10. ;
datalines;
10343452 Zilas 01/04/2016 main  3.2
10343452 Zilas 02/04/2014 main     4.5
10343452 Zilas 03/04/2012 main   6.6
10343452 Zilas 04/04/2011 main   7.15
10343452 Zilas 05/04/2017 main  7.7
10343452 Zilas 06/04/2017 main  .
10343452 Zilas 07/04/2017 main  .
10343452 Zilas 08/04/2017 main  .
10343453 Astra 01/04/2016 main  6.2
10343453 Astra 02/04/2014 main     5.5
10343453 Astra 03/04/2012 main   2.6
10343453 Astra 04/04/2011 main   3.65
10343453 Astra 05/04/2011 main   3.65
10343453 Astra 06/04/2017 main  4.7
10343453 Astra 07/04/2017 main  6.35
10343453 Astra 08/04/2017 main  8
10343453 Astra 09/04/2017 main  .
10343453 Astra 10/04/2017 main  .
10343453 Astra 11/04/2017 main  .
10343453 Astra 12/04/2017 main  .
10343453 Astra 13/04/2017 main  .

;
run;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Many thanks in advance&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 03 Jan 2021 17:34:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709136#M218000</guid>
      <dc:creator>georgel</dc:creator>
      <dc:date>2021-01-03T17:34:27Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing Missing values with average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709154#M218013</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/360730"&gt;@georgel&lt;/a&gt;&amp;nbsp; Can you please explain average of what?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Is it average of prior and after non missing value?&lt;/P&gt;
&lt;P&gt;2. If yes, how did you get&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;10343453 Astra 04/04/2011 main   5.15
10343453 Astra 05/04/2011 main   5.15&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 03 Jan 2021 17:06:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709154#M218013</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2021-01-03T17:06:13Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing Missing values with average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709155#M218014</link>
      <description>&lt;P&gt;Hi novinosrin,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1.&amp;nbsp; Yes . 2.&amp;nbsp;(4.7+2.6)/2=3.65 I made a mistake in my initial table.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks in advance&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;George&lt;/P&gt;</description>
      <pubDate>Sun, 03 Jan 2021 17:23:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709155#M218014</guid>
      <dc:creator>georgel</dc:creator>
      <dc:date>2021-01-03T17:23:39Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing Missing values with average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709156#M218015</link>
      <description>&lt;P&gt;Let's clarify what you are asking for.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You apparently want to replace only missing values that fall into a gap between non-missing values of ret.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;At first, I thought you wanted to merely assign the mid-point between the closest preceding and closest following non-missing values.&amp;nbsp; That would work for rows 4 (gets ret=7.15 from 6.6 and 7.7)&amp;nbsp; and 15 (6.35 from 4.7 and 8).&amp;nbsp; But it doesn't explain rows 12 and 13 (they get 5.15 - but are preceded by 2.6 and followed by 4.7).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please explain.&lt;/P&gt;</description>
      <pubDate>Sun, 03 Jan 2021 17:32:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709156#M218015</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-01-03T17:32:22Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing Missing values with average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709157#M218016</link>
      <description>&lt;P&gt;Hi Keintz&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have corrected my initial post to be the average 3.65&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 03 Jan 2021 17:36:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709157#M218016</guid>
      <dc:creator>georgel</dc:creator>
      <dc:date>2021-01-03T17:36:39Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing Missing values with average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709158#M218017</link>
      <description>&lt;P&gt;Hi &lt;A class="trigger-hovercard" style="color: #007dc3;" href="https://communities.sas.com/t5/user/viewprofilepage/user-id/360730" target="_blank"&gt;georgel&lt;/A&gt;,&lt;/P&gt;
&lt;P&gt;Here is one possible solution (I am not sure if it is the most efficient one):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input cusip_id $8. Name $6. date_issue :ddmmyy10. type $5. Ret  dollar8.1;
format date_issue ddmmyy10. ;
datalines;
10343452 Zilas 01/04/2016 main  3.2
10343452 Zilas 02/04/2014 main     4.5
10343452 Zilas 03/04/2012 main   6.6
10343452 Zilas 04/04/2011 main   .
10343452 Zilas 05/04/2017 main  7.7
10343452 Zilas 06/04/2017 main  .
10343452 Zilas 07/04/2017 main  .
10343452 Zilas 08/04/2017 main  .
10343453 Astra 01/04/2016 main  6.2
10343453 Astra 02/04/2014 main     5.5
10343453 Astra 03/04/2012 main   2.6
10343453 Astra 04/04/2011 main   .
10343453 Astra 05/04/2011 main   .
10343453 Astra 06/04/2017 main  4.7
10343453 Astra 07/04/2017 main  .
10343453 Astra 08/04/2017 main  8.0
10343453 Astra 09/04/2017 main  .
10343453 Astra 10/04/2017 main  .
10343453 Astra 11/04/2017 main  .
10343453 Astra 12/04/2017 main  .
10343453 Astra 13/04/2017 main  .
;
run;

proc sort data=HAVE;
   by cusip_id;
run;

data PASS1;
   set HAVE;
   by cusip_id;
   retain R1;
   Order = _n_;
   if first.cusip_id then R1 = .;
   if Ret ne . then R1 = Ret;
run;

proc sort data=PASS1 out=PASS2;
   by descending Order;
run;

data PASS3;
   set PASS2;
   by descending cusip_id;
   retain R2;
   if first.cusip_id then R2 = .;
   if Ret ne . then R2 = Ret;
run;

proc sort data=PASS3 out=PASS4;
   by Order;
run;

data WANT (drop=R1 R2 Order);
   set PASS4;
   if (Ret=.) and (R1 ne .) and (R2 ne .) then Ret = (R1+R2)/2;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Please note, that I changed one line in your raw data to be&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;10343453 Astra 08/04/2017 main  8.0&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;If you have 8 instead of 8.0 your data will be read in as 0.8 .&lt;/P&gt;
&lt;P&gt;Hope this helps.&lt;/P&gt;</description>
      <pubDate>Sun, 03 Jan 2021 17:49:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709158#M218017</guid>
      <dc:creator>LeonidBatkhan</dc:creator>
      <dc:date>2021-01-03T17:49:03Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing Missing values with average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709159#M218018</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input cusip_id $8. Name $6. date_issue :ddmmyy10. type $5. Ret  dollar8.1;
format date_issue ddmmyy10. ;
datalines;
10343452 Zilas 01/04/2016 main  3.2
10343452 Zilas 02/04/2014 main     4.5
10343452 Zilas 03/04/2012 main   6.6
10343452 Zilas 04/04/2011 main   .
10343452 Zilas 05/04/2017 main  7.7
10343452 Zilas 06/04/2017 main  .
10343452 Zilas 07/04/2017 main  .
10343452 Zilas 08/04/2017 main  .
10343453 Astra 01/04/2016 main  6.2
10343453 Astra 02/04/2014 main     5.5
10343453 Astra 03/04/2012 main   2.6
10343453 Astra 04/04/2011 main   .
10343453 Astra 05/04/2011 main   .
10343453 Astra 06/04/2017 main  4.7
10343453 Astra 07/04/2017 main  .
10343453 Astra 08/04/2017 main  8
10343453 Astra 09/04/2017 main  .
10343453 Astra 10/04/2017 main  .
10343453 Astra 11/04/2017 main  .
10343453 Astra 12/04/2017 main  .
10343453 Astra 13/04/2017 main  .
;
run;


data want;
 if _n_ = 1 then do ;
   dcl hash h() ;
   h.definekey ("_n") ;
   h.definedata ("ret" ) ;
   h.definedone () ;
 end ; 
 do __n=1 by 1 until(last.name);
  set have;
  by cusip_id name notsorted;
  if ret=. and not _k then  _k=__n;
  if ret then do;
   if _k&amp;gt;. then if __n-_k&amp;gt;=1 then do;
     _ret=mean(ret,_ret);
     do _n=_k to __n-1;
	  h.add(key:_n,data:_ret);
	 end;
	 _k=.;
   end;
   _ret=ret;
  end;
 end;
 do __n=1 to __n;
  set have;
  _n_=h.find(key:__n);
  output;
 end;
 h.clear();
 drop _:;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 03 Jan 2021 17:57:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709159#M218018</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2021-01-03T17:57:03Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing Missing values with average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709160#M218019</link>
      <description>&lt;P&gt;You need code to capture the most recent non-missing and the upcoming non-missing.&amp;nbsp; You can do this by passing through all the records for a given cusip_id and storing them in an array.&amp;nbsp; Whenever a non-missing is encountered, fill in any preceding element of the array that are missing using the average of the current and most recent non-missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then re-read the same cusip_id. whenever a missing ret if found, retrieve values from the array:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input cusip_id $8. Name $6. date_issue :ddmmyy10. type $5. Ret  dollar8.1;
format date_issue ddmmyy10. ;
datalines;
10343452 Zilas 01/04/2016 main  3.2
10343452 Zilas 02/04/2014 main     4.5
10343452 Zilas 03/04/2012 main   6.6
10343452 Zilas 04/04/2011 main   .
10343452 Zilas 05/04/2017 main  7.7
10343452 Zilas 06/04/2017 main  .
10343452 Zilas 07/04/2017 main  .
10343452 Zilas 08/04/2017 main  .
10343453 Astra 01/04/2016 main  6.2
10343453 Astra 02/04/2014 main     5.5
10343453 Astra 03/04/2012 main   2.6
10343453 Astra 04/04/2011 main   .
10343453 Astra 05/04/2011 main   .
10343453 Astra 06/04/2017 main  4.7
10343453 Astra 07/04/2017 main  .
10343453 Astra 08/04/2017 main  8.0    /*changed 8 to 8.0 to accomodate dollar8.1 format*/
10343453 Astra 09/04/2017 main  .
10343453 Astra 10/04/2017 main  .
10343453 Astra 11/04/2017 main  .
10343453 Astra 12/04/2017 main  .
10343453 Astra 13/04/2017 main  .
run;


data want (drop=_:);
  array rets {30} _temporary_;
  call missing(of rets{*});
  do _i=1 by 1 until (last.cusip_id);
    set have;
    by cusip_id;
    if ret^=. then do;
      _mean_ret=mean(lag(ret),ret);
      if _i&amp;gt; 1 then do _j=_i-1 to 1 by -1 while(rets{_j}=.);
	    rets{_j}=_mean_ret;
	  end;
    end;
  end;

  do _i=1 by 1 until (last.cusip_id);
    set have;
	by cusip_id;
    if missing(ret) then ret=rets{_i};
    output;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;A note:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The mean(lag(ret),ret) only retrieves the lagged value when the current ret is not missing.&amp;nbsp; This means that the lag(ret) will also always be non-missing (except at the beginning).&amp;nbsp; That's because the LAG function is actually a fifo queue, which is only updated when ret is not missing.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Also, make the array size at least as large as the most populated CUSIP_ID.&amp;nbsp; I used 30 above.&lt;/P&gt;</description>
      <pubDate>Sun, 03 Jan 2021 18:07:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709160#M218019</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-01-03T18:07:37Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing Missing values with average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709161#M218020</link>
      <description>&lt;P&gt;Impressive! Thanks a million Keintz&lt;/P&gt;</description>
      <pubDate>Sun, 03 Jan 2021 18:11:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709161#M218020</guid>
      <dc:creator>georgel</dc:creator>
      <dc:date>2021-01-03T18:11:32Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing Missing values with average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709162#M218021</link>
      <description>&lt;P&gt;Many thanks novinosrin. I have tried it but one observation has been removed&lt;/P&gt;</description>
      <pubDate>Sun, 03 Jan 2021 18:13:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709162#M218021</guid>
      <dc:creator>georgel</dc:creator>
      <dc:date>2021-01-03T18:13:42Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing Missing values with average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709163#M218022</link>
      <description>&lt;P&gt;Thanks Leonid&lt;/P&gt;</description>
      <pubDate>Sun, 03 Jan 2021 18:15:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709163#M218022</guid>
      <dc:creator>georgel</dc:creator>
      <dc:date>2021-01-03T18:15:00Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing Missing values with average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709164#M218023</link>
      <description>&lt;P&gt;Oh sorry, which one did it drop or remove?&lt;/P&gt;</description>
      <pubDate>Sun, 03 Jan 2021 18:18:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709164#M218023</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2021-01-03T18:18:02Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing Missing values with average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709165#M218024</link>
      <description>&lt;P&gt;novinosrin, Accept my apologies I rerun and it works perfectly!.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I admit that your solution is more efficient since&amp;nbsp; the accepted solution I have to resize the matrix&lt;/P&gt;&lt;P&gt;Perfect work! Thanks a million&amp;nbsp;novinosrin&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;George&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 03 Jan 2021 18:26:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709165#M218024</guid>
      <dc:creator>georgel</dc:creator>
      <dc:date>2021-01-03T18:26:22Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing Missing values with average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709166#M218025</link>
      <description>&lt;P&gt;No my solution is not efficient than&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp; 's solution. Temp ARRAY is much faster. As a matter of fact, I don't think any of my solutions can come close to Mark's and in fact he was the one who taught me many stuff in SAS over the last 5 years. Oh gosh, Mark must have gotten much older lol hahaha&lt;/P&gt;</description>
      <pubDate>Sun, 03 Jan 2021 18:28:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709166#M218025</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2021-01-03T18:28:59Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing Missing values with average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709169#M218028</link>
      <description>&lt;P&gt;Question:&amp;nbsp; why are your data not ordered chronologically?&amp;nbsp; For instance the 3rd-5th rows are as below (note dates are dd/mm/yyyy):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;10343452 Zilas 03/04/2012 main   6.6
10343452 Zilas 04/04/2011 main   .
10343452 Zilas 05/04/2017 main  7.7&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you are estimating data for 4/4/2011&amp;nbsp; (04apr2011) from&amp;nbsp; &amp;nbsp;03apr2012&amp;nbsp; and 05april2017.&amp;nbsp; &amp;nbsp; &amp;nbsp;Really?&amp;nbsp; &amp;nbsp;2011 from 2012 and 2017?&lt;/P&gt;</description>
      <pubDate>Sun, 03 Jan 2021 19:10:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709169#M218028</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-01-03T19:10:13Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing Missing values with average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709170#M218029</link>
      <description>&lt;P&gt;Dear Mark,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much for paying attention to details!. This is a great support from you and&amp;nbsp; I highly appreciated it.&lt;/P&gt;&lt;P&gt;No, this is just a random sample non-real one!!&amp;nbsp; I have my real data. I havent notice this!. Basically to telll the story. I want to have benchmarks for each of my&amp;nbsp; own set of companies all of them from US&amp;nbsp; but I find that some of my benchmarks have only one missing return. The algortihm that I created it finds another benchmark-company only for the missing day and I dont want this. (real example from CRSP: WRDS:20030815&amp;nbsp; &amp;nbsp; CUSIP:&amp;nbsp;88800230&amp;nbsp; Missing Return )&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;George&lt;/P&gt;</description>
      <pubDate>Sun, 03 Jan 2021 19:54:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709170#M218029</guid>
      <dc:creator>georgel</dc:creator>
      <dc:date>2021-01-03T19:54:41Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing Missing values with average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709177#M218035</link>
      <description>&lt;P&gt;If your data are already sorted, then there is a somewhat cleaner way to replace internal missing values:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data retmeans (keep=cusip_id date_issue _midpoint) / view=retmeans;
  merge have (where=(ret^=.)                 keep=cusip_id ret date_issue)
        have (where=(_nxt_ret^=.) firstobs=2 keep=cusip_id ret rename=(cusip_id=_nxt_cusip ret=_nxt_ret));
  if cusip_id=_nxt_cusip then _midpoint=mean(ret,_nxt_ret);
run;

data want (drop=_:);
  merge have  retmeans (keep=cusip_id date_issue in=inmid);
  by cusip_id date_issue;
  if inmid then set retmeans ; /* Get _MIDPOINT */
  if ret=. then ret=_midpoint;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note the data set RETMEANS is a data set &lt;EM&gt;&lt;STRONG&gt;view&lt;/STRONG&gt;&lt;/EM&gt;, not a data set &lt;EM&gt;&lt;STRONG&gt;file&lt;/STRONG&gt;&lt;/EM&gt;.&amp;nbsp; That means it will not be written to disk, and that it will only be implemented when called for in a later data step.&amp;nbsp; Think of it as a synchronized process that is run only when used in the DATA WANT step.&amp;nbsp; So while the program is structed in two steps, there is only one step that actually causes data processing - i.e. it's efficient.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The retmeans data set will have one record per non-missing RET.&amp;nbsp; It will contain the id, date and the _MIDPOINT, the mean of the current RET and the following RET (note the FIRSTOBS=2).&amp;nbsp; Except for the last non-missing RET of each ID where _MIDPOINT will be left missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The second data step refers to RETMEANS once in a MERGE statement (excluding the _MIDPOINT variable) and once the SET statement.&amp;nbsp; Any variable in the (unconditional) MERGE statement will be replaced with ever iteration of the data step.&amp;nbsp; But any variable associated only with the (conditional) SET statement will not be replaced/reset until the next execution of the set statement.&amp;nbsp; So _MIDPOINT will be retained and available for all dates between two non-missing RET's.&lt;/P&gt;</description>
      <pubDate>Sun, 03 Jan 2021 23:16:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709177#M218035</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-01-03T23:16:33Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing Missing values with average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709205#M218050</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input cusip_id $8. Name $6. date_issue :ddmmyy10. type $5. Ret ;
format date_issue ddmmyy10. ;
datalines;
10343452 Zilas 01/04/2016 main  3.2
10343452 Zilas 02/04/2014 main     4.5
10343452 Zilas 03/04/2012 main   6.6
10343452 Zilas 04/04/2011 main   .
10343452 Zilas 05/04/2017 main  7.7
10343452 Zilas 06/04/2017 main  .
10343452 Zilas 07/04/2017 main  .
10343452 Zilas 08/04/2017 main  .
10343453 Astra 01/04/2016 main  6.2
10343453 Astra 02/04/2014 main     5.5
10343453 Astra 03/04/2012 main   2.6
10343453 Astra 04/04/2011 main   .
10343453 Astra 05/04/2011 main   .
10343453 Astra 06/04/2017 main  4.7
10343453 Astra 07/04/2017 main  .
10343453 Astra 08/04/2017 main  8
10343453 Astra 09/04/2017 main  .
10343453 Astra 10/04/2017 main  .
10343453 Astra 11/04/2017 main  .
10343453 Astra 12/04/2017 main  .
10343453 Astra 13/04/2017 main  .
;
run;
data want;
if 0 then set have;
retain lag_ret .;
do until(last.cusip_id or not missing(ret));
 set have;
 by cusip_id;
 if first.cusip_id then call missing(lag_ret);
end;
mean=(lag_ret+ret)/2;
do until(last.cusip_id or not missing(ret));
 set have;
 by cusip_id;
 new_ret=coalesce(ret,mean);
 output;
end;
lag_ret=ret;
drop mean lag_ret ret;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Jan 2021 12:04:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709205#M218050</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-01-04T12:04:25Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing Missing values with average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709253#M218062</link>
      <description>&lt;P&gt;Dear Mark,&lt;/P&gt;&lt;P&gt;Thank you very much for your great support and explanations.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;George&lt;/P&gt;</description>
      <pubDate>Mon, 04 Jan 2021 17:39:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709253#M218062</guid>
      <dc:creator>georgel</dc:creator>
      <dc:date>2021-01-04T17:39:28Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing Missing values with average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709268#M218070</link>
      <description>&lt;P&gt;Just &lt;A href="https://blogs.sas.com/content/iml/2017/12/04/mean-imputation-sas.html" target="_self"&gt;use PROC STDIZE, as described in this article&lt;/A&gt;. But also be aware that there are &lt;A href="https://blogs.sas.com/content/iml/2017/12/06/problems-mean-imputation.html" target="_self"&gt;disadvantages to replacing missing values by the mean of the nonmissing values&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Jan 2021 18:37:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-Missing-values-with-average/m-p/709268#M218070</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2021-01-04T18:37:30Z</dc:date>
    </item>
  </channel>
</rss>

