<?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: Help with Moving Sum in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Moving-Sum/m-p/701717#M214896</link>
    <description>&lt;P&gt;Please show what you expect/want for output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you thing the bit in your log that looks like this might have some bearing?&lt;/P&gt;
&lt;PRE&gt;totalobs=10 value=10 i=9 total=29 j=12 nobs=10 _ERROR_=1 _N_=9
totalobs=10 value=10 i=10 total=30 j=13 nobs=10 _ERROR_=1 _N_=10

&lt;/PRE&gt;
&lt;P&gt;When POINT exceeds the number of records in the data set what do you think happens?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe (guessing)&lt;/P&gt;
&lt;PRE&gt;data want2; 
set have nobs=totalobs;
i+1;
total=0;
do j=i to i+2;
  set have (keep = value ) point=j nobs=nobs;
  if j&amp;gt;nobs then stop;
  total= total + value;
  *output; 
end;
run;
&lt;/PRE&gt;</description>
    <pubDate>Wed, 25 Nov 2020 23:32:49 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-11-25T23:32:49Z</dc:date>
    <item>
      <title>Help with Moving Sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Moving-Sum/m-p/701716#M214895</link>
      <description>&lt;P&gt;Hi, My code calculate moving sum (i to i+3). The last 2 rows have issue where it double count the last value. Can you please help me to fix it? Thanks, HHC&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input value;
datalines;
1
2
3
4
5
6
7
8
9
10
;run;

data want; 
set have nobs=totalobs;
i+1;
total=0;
do j=i to i+2;
  set have (keep = value ) point=j nobs=nobs;;
  total= total + value;
  *output; 
end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;data have;&lt;BR /&gt;input value;&lt;BR /&gt;datalines;&lt;BR /&gt;1&lt;BR /&gt;2&lt;BR /&gt;3&lt;BR /&gt;4&lt;BR /&gt;5&lt;BR /&gt;6&lt;BR /&gt;7&lt;BR /&gt;8&lt;BR /&gt;9&lt;BR /&gt;10&lt;BR /&gt;;run;&lt;/P&gt;
&lt;P&gt;data want; &lt;BR /&gt;set have nobs=totalobs;&lt;BR /&gt;i+1;&lt;BR /&gt;total=0;&lt;BR /&gt;do j=i to min(i+2,totalobs);&lt;BR /&gt;set have (keep = value rename =(value=v)) point=j ;&lt;BR /&gt;total= total + v;&lt;BR /&gt;*output; &lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Dec 2020 17:17:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Moving-Sum/m-p/701716#M214895</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2020-12-02T17:17:34Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Moving Sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Moving-Sum/m-p/701717#M214896</link>
      <description>&lt;P&gt;Please show what you expect/want for output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you thing the bit in your log that looks like this might have some bearing?&lt;/P&gt;
&lt;PRE&gt;totalobs=10 value=10 i=9 total=29 j=12 nobs=10 _ERROR_=1 _N_=9
totalobs=10 value=10 i=10 total=30 j=13 nobs=10 _ERROR_=1 _N_=10

&lt;/PRE&gt;
&lt;P&gt;When POINT exceeds the number of records in the data set what do you think happens?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe (guessing)&lt;/P&gt;
&lt;PRE&gt;data want2; 
set have nobs=totalobs;
i+1;
total=0;
do j=i to i+2;
  set have (keep = value ) point=j nobs=nobs;
  if j&amp;gt;nobs then stop;
  total= total + value;
  *output; 
end;
run;
&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 Nov 2020 23:32:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Moving-Sum/m-p/701717#M214896</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-11-25T23:32:49Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Moving Sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Moving-Sum/m-p/701727#M214898</link>
      <description>&lt;P&gt;Perhaps-&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do j=i to min(i+2,totalobs);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;?&lt;/P&gt;</description>
      <pubDate>Thu, 26 Nov 2020 01:22:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Moving-Sum/m-p/701727#M214898</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-11-26T01:22:51Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Moving Sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Moving-Sum/m-p/701729#M214899</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Perhaps-&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do j=i to min(i+2,totalobs);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I looked at that too. One of the reasons I asked for expected out come.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Nov 2020 01:29:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Moving-Sum/m-p/701729#M214899</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-11-26T01:29:24Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Moving Sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Moving-Sum/m-p/701812#M214927</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input value;
datalines;
1
2
3
4
5
6
7
8
9
10
;run;
data want;
 merge have 
       have(firstobs=2 keep=value rename=(value=value1))
       have(firstobs=3 keep=value rename=(value=value2)) ;
total=sum(value,value1,value2);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 Nov 2020 12:24:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Moving-Sum/m-p/701812#M214927</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-11-26T12:24:25Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Moving Sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Moving-Sum/m-p/701814#M214928</link>
      <description>&lt;P&gt;Since you need to look ahead,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;'s answer is the way to go. if you wanted to "look back", a temporary array would be best:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let size=3;

data want;
set have;
array window {0:%eval(&amp;amp;size.-1)} _temporary_;
window{mod(_n_,&amp;amp;size.)} = value;
total = sum(of window{*});
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 Nov 2020 12:39:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Moving-Sum/m-p/701814#M214928</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-26T12:39:54Z</dc:date>
    </item>
  </channel>
</rss>

