<?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: Fill missing value with the average of surrounding values in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Fill-missing-value-with-the-average-of-surrounding-values/m-p/450141#M13884</link>
    <description>Thank you for the reply. It works well.</description>
    <pubDate>Sat, 31 Mar 2018 14:53:28 GMT</pubDate>
    <dc:creator>Xusheng</dc:creator>
    <dc:date>2018-03-31T14:53:28Z</dc:date>
    <item>
      <title>Fill missing value with the average of surrounding values</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Fill-missing-value-with-the-average-of-surrounding-values/m-p/449942#M13875</link>
      <description>&lt;P&gt;Hi, I currently have a problem that requires filling the missing value with surrounding value mean. For example:&lt;/P&gt;&lt;DIV&gt;data have;&lt;/DIV&gt;&lt;DIV&gt;input a ;&lt;/DIV&gt;&lt;DIV&gt;datalines;&lt;/DIV&gt;&lt;DIV&gt;1&lt;/DIV&gt;&lt;DIV&gt;2&lt;/DIV&gt;&lt;DIV&gt;2&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;.&lt;/DIV&gt;&lt;DIV&gt;.&lt;/DIV&gt;&lt;DIV&gt;1&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;.&lt;/DIV&gt;&lt;DIV&gt;2&lt;/DIV&gt;&lt;DIV&gt;;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;P&gt;Here is what I need:&lt;/P&gt;&lt;DIV&gt;data want;&lt;/DIV&gt;&lt;DIV&gt;input a ;&lt;/DIV&gt;&lt;DIV&gt;datalines;&lt;/DIV&gt;&lt;DIV&gt;1&lt;/DIV&gt;&lt;DIV&gt;2&lt;/DIV&gt;&lt;DIV&gt;2&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;1.5&lt;/DIV&gt;&lt;DIV&gt;1.25&lt;/DIV&gt;&lt;DIV&gt;1&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;1.5&lt;/DIV&gt;&lt;DIV&gt;2&lt;/DIV&gt;&lt;DIV&gt;;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;As you may notice, the filling method is when there is only one missing, simply take the average of the surrounding values. If there are two continuous missing value, take the average of surrounding values to fill the first missing, then use the same method as only one missing value to fill the second missing.&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Any suggestion would be appreciated. Thank you.&lt;/DIV&gt;</description>
      <pubDate>Fri, 30 Mar 2018 14:38:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Fill-missing-value-with-the-average-of-surrounding-values/m-p/449942#M13875</guid>
      <dc:creator>Xusheng</dc:creator>
      <dc:date>2018-03-30T14:38:20Z</dc:date>
    </item>
    <item>
      <title>Re: Fill missing value with the average of surrounding values</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Fill-missing-value-with-the-average-of-surrounding-values/m-p/450015#M13876</link>
      <description>&lt;P&gt;PROC EXPAND can be used for interpolating the moving average. But here depending on your condition you need to make some looping to make sure that there is only one missing value between consecutive records to get the output that you desired.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input a ;
obs=_n_;
datalines;
1
2
2 
.
.
1 
.
2
;
run;
data count_max_mis;
set have ;
if not missing(a) then count=0;
else count+1;
run;

proc sql;
select max(count) into: n_miss
	from count_max_mis;
quit;

%macro interpolate_miss();
%DO i=1 %to &amp;amp;n_miss;

data Inter;
set have;
if not missing(a) then call symput("value","NOTNULL");
else if missing(a) and SYMGET("value")="NOTNULL" then call symput("value","NULL");
else if missing(a) then delete;
run;

PROC EXPAND data=Inter out=Updated_miss(drop=time) method=none;
convert a / transformout=(missonly  cmovave 3 );
run;

data Have;
update have Updated_miss;
by obs;
run;
%end;
%MEND interpolate_miss;
%interpolate_miss;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 30 Mar 2018 19:01:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Fill-missing-value-with-the-average-of-surrounding-values/m-p/450015#M13876</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-03-30T19:01:37Z</dc:date>
    </item>
    <item>
      <title>Re: Fill missing value with the average of surrounding values</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Fill-missing-value-with-the-average-of-surrounding-values/m-p/450034#M13877</link>
      <description>&lt;P&gt;What is the&amp;nbsp;advantage of such an interpolation method? Its simplicity maybe?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input a;
datalines;
1
2
2 
.
.
1
.
2
;

data want;
retain _a;
do nb = 1 by 1 until(not missing(a_));
    set have(rename=a=a_);
    end;
do i = 1 to nb;
    set have;
    if missing(a) then a = (_a + a_) / 2;
    _a = a;
    output;
    end;
drop nb i _a a_;
run;

proc print; run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 30 Mar 2018 19:46:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Fill-missing-value-with-the-average-of-surrounding-values/m-p/450034#M13877</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-03-30T19:46:53Z</dc:date>
    </item>
    <item>
      <title>Re: Fill missing value with the average of surrounding values</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Fill-missing-value-with-the-average-of-surrounding-values/m-p/450123#M13882</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input a ;
datalines;
1
2
2 
.
.
1 
.
2
;
run;

data want;
 set have end=last;
 array x{1000} _temporary_;
 x{_n_}=a;
 if last then do;
  do i=1 to _n_;
    if missing(x{i}) then do;
      prev=x{i-1};next=.;
	  do j=i+1 to _n_;
        if not missing(x{j}) then do;next=x{j};leave;end;
	  end;
	  x{i}=(prev+next)/2;
	end;
  end;

  do k=1 to _n_;
    a=x{k};output;
  end;
 end;
keep a;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 31 Mar 2018 11:44:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Fill-missing-value-with-the-average-of-surrounding-values/m-p/450123#M13882</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-03-31T11:44:41Z</dc:date>
    </item>
    <item>
      <title>Re: Fill missing value with the average of surrounding values</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Fill-missing-value-with-the-average-of-surrounding-values/m-p/450140#M13883</link>
      <description>Thank you for the reply.</description>
      <pubDate>Sat, 31 Mar 2018 14:52:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Fill-missing-value-with-the-average-of-surrounding-values/m-p/450140#M13883</guid>
      <dc:creator>Xusheng</dc:creator>
      <dc:date>2018-03-31T14:52:19Z</dc:date>
    </item>
    <item>
      <title>Re: Fill missing value with the average of surrounding values</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Fill-missing-value-with-the-average-of-surrounding-values/m-p/450141#M13884</link>
      <description>Thank you for the reply. It works well.</description>
      <pubDate>Sat, 31 Mar 2018 14:53:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Fill-missing-value-with-the-average-of-surrounding-values/m-p/450141#M13884</guid>
      <dc:creator>Xusheng</dc:creator>
      <dc:date>2018-03-31T14:53:28Z</dc:date>
    </item>
  </channel>
</rss>

