<?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 Moving average based on condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Moving-average-based-on-condition/m-p/801580#M315464</link>
    <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;
&lt;P&gt;For each id, I want to calculate moving average using values not equal to 0.&lt;/P&gt;
&lt;P&gt;For the data below with moving average of 3 lag 1, the average for record 4 should take (3+9)/2; record 5 will have 9/1.&lt;/P&gt;
&lt;P&gt;The proc expand with WHERE v2&amp;gt;0 is not working that way.&lt;/P&gt;
&lt;P&gt;Can you please help?&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input date id v2;
datalines;
1 1 3
2 1 0
3 1 9
4 1 0
5 1 5
1 2 0
2 2 0
3 2 9
4 2 0
5 2 5
;run;

*NOT right;
proc expand data=have out=want;
where v2&amp;gt;0;
convert v2 = avg/transformout=(MOVave  3 lag 1);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 11 Mar 2022 03:03:05 GMT</pubDate>
    <dc:creator>hhchenfx</dc:creator>
    <dc:date>2022-03-11T03:03:05Z</dc:date>
    <item>
      <title>Moving average based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-average-based-on-condition/m-p/801580#M315464</link>
      <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;
&lt;P&gt;For each id, I want to calculate moving average using values not equal to 0.&lt;/P&gt;
&lt;P&gt;For the data below with moving average of 3 lag 1, the average for record 4 should take (3+9)/2; record 5 will have 9/1.&lt;/P&gt;
&lt;P&gt;The proc expand with WHERE v2&amp;gt;0 is not working that way.&lt;/P&gt;
&lt;P&gt;Can you please help?&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input date id v2;
datalines;
1 1 3
2 1 0
3 1 9
4 1 0
5 1 5
1 2 0
2 2 0
3 2 9
4 2 0
5 2 5
;run;

*NOT right;
proc expand data=have out=want;
where v2&amp;gt;0;
convert v2 = avg/transformout=(MOVave  3 lag 1);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Mar 2022 03:03:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-average-based-on-condition/m-p/801580#M315464</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-03-11T03:03:05Z</dc:date>
    </item>
    <item>
      <title>Re: Moving average based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-average-based-on-condition/m-p/801588#M315471</link>
      <description>&lt;P&gt;Shouldn't it be&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;convert v2 = avg/transformout=(MOVave 1 lag 3);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Mar 2022 03:43:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-average-based-on-condition/m-p/801588#M315471</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-03-11T03:43:46Z</dc:date>
    </item>
    <item>
      <title>Re: Moving average based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-average-based-on-condition/m-p/801589#M315472</link>
      <description>&lt;P&gt;convert v2 = avg/transformout=(MOVave 1 lag 3);&lt;/P&gt;
&lt;P&gt;will take average of 3 prior cell regardless of cell value, so it will not work.&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;</description>
      <pubDate>Fri, 11 Mar 2022 04:13:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-average-based-on-condition/m-p/801589#M315472</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-03-11T04:13:34Z</dc:date>
    </item>
    <item>
      <title>Re: Moving average based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-average-based-on-condition/m-p/801590#M315473</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I do the vertical/Excel kind of way.&lt;/P&gt;
&lt;P&gt;It works.&lt;/P&gt;
&lt;P&gt;If you have a shorter method, please let me know.&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input date id value;
datalines;
1 1 3
2 1 0
3 1 9
4 1 0
5 1 5
1 2 1
2 2 0
3 2 9
4 2 0
5 2 5
;run;

proc sort data=have; by id descending date;run;

data want; set have;
drop id1 date1 v1;
SUM=0;
Count=0;
do i=_N_ +1 to _N_+3;

	set have (rename = (date=date1 id=id1 value=v1)) point=i;
		if id1=id and v1^=0 then do;
			sum=sum+v1;
			count=count+1;
		end;
		average=sum/count;
end;
output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Mar 2022 04:22:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-average-based-on-condition/m-p/801590#M315473</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-03-11T04:22:34Z</dc:date>
    </item>
    <item>
      <title>Re: Moving average based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-average-based-on-condition/m-p/801638#M315502</link>
      <description>&lt;P&gt;Easiest is very likely 1) add a new variable that is missing when the variable you want to average is missing&lt;/P&gt;
&lt;P&gt;2) expand that new variable&lt;/P&gt;
&lt;P&gt;3) (may want to remove the added variable)&lt;/P&gt;</description>
      <pubDate>Fri, 11 Mar 2022 11:41:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-average-based-on-condition/m-p/801638#M315502</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-03-11T11:41:53Z</dc:date>
    </item>
    <item>
      <title>Re: Moving average based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-average-based-on-condition/m-p/801659#M315512</link>
      <description>&lt;P&gt;You want the average of the previous three observations, excluding thoses with V2=0.&amp;nbsp; How about a DATA step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input date id v2;
datalines;
1 1 3
2 1 0
3 1 9
4 1 0
5 1 5
1 2 0
2 2 0
3 2 9
4 2 0
5 2 5
;run;

data want;
  array prev_three {3} _temporary_;
  set have;
  by id;
  if first.id then call missing(of prev_three{*});
  avg=mean(of prev_three{*});
  prev_three{mod(_n_,3)+1}=ifn(v2&amp;gt;0,v2,.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The important thing here is that AVG is calculated &lt;EM&gt;&lt;STRONG&gt;prior&lt;/STRONG&gt;&lt;/EM&gt; to replacing the 3rd previous value of V2 in the PREV_THREE array with the current value.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Mar 2022 13:42:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-average-based-on-condition/m-p/801659#M315512</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-03-11T13:42:05Z</dc:date>
    </item>
    <item>
      <title>Re: Moving average based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-average-based-on-condition/m-p/801717#M315537</link>
      <description>&lt;P&gt;Your code is amazing.&lt;/P&gt;
&lt;P&gt;It work much much faster than my code!!!&lt;/P&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;</description>
      <pubDate>Fri, 11 Mar 2022 18:13:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-average-based-on-condition/m-p/801717#M315537</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-03-11T18:13:43Z</dc:date>
    </item>
  </channel>
</rss>

