<?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 SAS sum of next N rows for every row in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-sum-of-next-N-rows-for-every-row/m-p/762775#M241534</link>
    <description>&lt;P&gt;I have a dataset like this for each ID;&lt;/P&gt;&lt;DIV class="s-table-container"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="s-table-container"&gt;&lt;PRE&gt;data have;&lt;BR /&gt;input Months ID Number;
 2018-07-01  1    0	         
 2018-08-01  1    0	         
 2018-09-01  1    1	        
 2018-10-01  1    3	         
 2018-11-01  1    1	         
 2018-12-01  1    2	         
 2019-01-01  1    0	         
 2019-02-01  1    0         
 2019-03-01  1    1	         
 2019-04-01  1    0	        
 2019-05-01  1    0	        
 2019-06-01  1    0	         
 2019-07-01  1    1	         
 2019-08-01  1    0	         
 2019-09-01  1    0	         
 2019-10-01  1    2	         
 2019-11-01  1    0	         
 2019-12-01  1    0         
 2020-01-01  1    0	         
 2020-02-01  1    0	         
 2020-03-01  1    0         
 2020-04-01  1    0	        
 2020-05-01  1    0         
 2020-06-01  1    0         
 2020-07-01  1    0         
 2020-08-01  1    1        
 2020-09-01  1    0         
 2020-10-01  1    0         
 2020-11-01  1    1         
 2020-12-01  1    0         
 2021-01-01  1    0         
 2021-02-01  1    1         
 2021-03-01  1    1         
 2021-04-01  1    0        
 2018-07-01  2    0    
 .......   .......   .......  &lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;&lt;P&gt;(Similar values for each ID)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want a dataset like this;&lt;/P&gt;&lt;PRE&gt;data want;&lt;BR /&gt;&lt;BR /&gt;input Months ID Number Sum_Next_6Numbers;&lt;BR /&gt;2018-07-01 1 0  7&lt;BR /&gt;2018-08-01 1 0  7&lt;BR /&gt;2018-09-01 1 1  7&lt;BR /&gt;2018-10-01 1 3  4&lt;BR /&gt;2018-11-01 1 1  3&lt;BR /&gt;2018-12-01 1 2  1&lt;BR /&gt;2019-01-01 1 0  2&lt;BR /&gt;2019-02-01 1 0  2&lt;BR /&gt;2019-03-01 1 1  1&lt;BR /&gt;2019-04-01 1 0  3&lt;BR /&gt;2019-05-01 1 0  3&lt;BR /&gt;2019-06-01 1 0  3&lt;BR /&gt;2019-07-01 1 1  2&lt;BR /&gt;2019-08-01 1 0  2&lt;BR /&gt;2019-09-01 1 0  2&lt;BR /&gt;2019-10-01 1 2  0&lt;BR /&gt;2019-11-01 1 0  0&lt;BR /&gt;2019-12-01 1 0  0&lt;BR /&gt;2020-01-01 1 0  0&lt;BR /&gt;2020-02-01 1 0  1&lt;BR /&gt;2020-03-01 1 0  1&lt;BR /&gt;2020-04-01 1 0  1&lt;BR /&gt;2020-05-01 1 0  2&lt;BR /&gt;2020-06-01 1 0  2&lt;BR /&gt;2020-07-01 1 0  2&lt;BR /&gt;2020-08-01 1 1  2&lt;BR /&gt;2020-09-01 1 0  3&lt;BR /&gt;2020-10-01 1 0  3&lt;BR /&gt;2020-11-01 1 1  Nan&lt;BR /&gt;2020-12-01 1 0  Nan&lt;BR /&gt;2021-01-01 1 0  Nan&lt;BR /&gt;2021-02-01 1 1  Nan &lt;BR /&gt;2021-03-01 1 1  Nan&lt;BR /&gt;2021-04-01 1 0  Nan&lt;BR /&gt;2018-07-01 2 0  0&lt;BR /&gt;....... ....... ....... &lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;&lt;P&gt;I want to sum up next 6 row's values(for number column). If there is no 6 months left then this values should be Nan.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;e. g. for first row(2018-07-01) want to sum next 6 rows (2018-08-01,2018-09-01,2018-10-01,2018-11-01,2018-12-01,2019-01-01) of Number column.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way to do this? Thank you in advance.&lt;/P&gt;&lt;/DIV&gt;</description>
    <pubDate>Fri, 20 Aug 2021 07:15:55 GMT</pubDate>
    <dc:creator>zdc</dc:creator>
    <dc:date>2021-08-20T07:15:55Z</dc:date>
    <item>
      <title>SAS sum of next N rows for every row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-sum-of-next-N-rows-for-every-row/m-p/762775#M241534</link>
      <description>&lt;P&gt;I have a dataset like this for each ID;&lt;/P&gt;&lt;DIV class="s-table-container"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="s-table-container"&gt;&lt;PRE&gt;data have;&lt;BR /&gt;input Months ID Number;
 2018-07-01  1    0	         
 2018-08-01  1    0	         
 2018-09-01  1    1	        
 2018-10-01  1    3	         
 2018-11-01  1    1	         
 2018-12-01  1    2	         
 2019-01-01  1    0	         
 2019-02-01  1    0         
 2019-03-01  1    1	         
 2019-04-01  1    0	        
 2019-05-01  1    0	        
 2019-06-01  1    0	         
 2019-07-01  1    1	         
 2019-08-01  1    0	         
 2019-09-01  1    0	         
 2019-10-01  1    2	         
 2019-11-01  1    0	         
 2019-12-01  1    0         
 2020-01-01  1    0	         
 2020-02-01  1    0	         
 2020-03-01  1    0         
 2020-04-01  1    0	        
 2020-05-01  1    0         
 2020-06-01  1    0         
 2020-07-01  1    0         
 2020-08-01  1    1        
 2020-09-01  1    0         
 2020-10-01  1    0         
 2020-11-01  1    1         
 2020-12-01  1    0         
 2021-01-01  1    0         
 2021-02-01  1    1         
 2021-03-01  1    1         
 2021-04-01  1    0        
 2018-07-01  2    0    
 .......   .......   .......  &lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;&lt;P&gt;(Similar values for each ID)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want a dataset like this;&lt;/P&gt;&lt;PRE&gt;data want;&lt;BR /&gt;&lt;BR /&gt;input Months ID Number Sum_Next_6Numbers;&lt;BR /&gt;2018-07-01 1 0  7&lt;BR /&gt;2018-08-01 1 0  7&lt;BR /&gt;2018-09-01 1 1  7&lt;BR /&gt;2018-10-01 1 3  4&lt;BR /&gt;2018-11-01 1 1  3&lt;BR /&gt;2018-12-01 1 2  1&lt;BR /&gt;2019-01-01 1 0  2&lt;BR /&gt;2019-02-01 1 0  2&lt;BR /&gt;2019-03-01 1 1  1&lt;BR /&gt;2019-04-01 1 0  3&lt;BR /&gt;2019-05-01 1 0  3&lt;BR /&gt;2019-06-01 1 0  3&lt;BR /&gt;2019-07-01 1 1  2&lt;BR /&gt;2019-08-01 1 0  2&lt;BR /&gt;2019-09-01 1 0  2&lt;BR /&gt;2019-10-01 1 2  0&lt;BR /&gt;2019-11-01 1 0  0&lt;BR /&gt;2019-12-01 1 0  0&lt;BR /&gt;2020-01-01 1 0  0&lt;BR /&gt;2020-02-01 1 0  1&lt;BR /&gt;2020-03-01 1 0  1&lt;BR /&gt;2020-04-01 1 0  1&lt;BR /&gt;2020-05-01 1 0  2&lt;BR /&gt;2020-06-01 1 0  2&lt;BR /&gt;2020-07-01 1 0  2&lt;BR /&gt;2020-08-01 1 1  2&lt;BR /&gt;2020-09-01 1 0  3&lt;BR /&gt;2020-10-01 1 0  3&lt;BR /&gt;2020-11-01 1 1  Nan&lt;BR /&gt;2020-12-01 1 0  Nan&lt;BR /&gt;2021-01-01 1 0  Nan&lt;BR /&gt;2021-02-01 1 1  Nan &lt;BR /&gt;2021-03-01 1 1  Nan&lt;BR /&gt;2021-04-01 1 0  Nan&lt;BR /&gt;2018-07-01 2 0  0&lt;BR /&gt;....... ....... ....... &lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;&lt;P&gt;I want to sum up next 6 row's values(for number column). If there is no 6 months left then this values should be Nan.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;e. g. for first row(2018-07-01) want to sum next 6 rows (2018-08-01,2018-09-01,2018-10-01,2018-11-01,2018-12-01,2019-01-01) of Number column.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way to do this? Thank you in advance.&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 20 Aug 2021 07:15:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-sum-of-next-N-rows-for-every-row/m-p/762775#M241534</guid>
      <dc:creator>zdc</dc:creator>
      <dc:date>2021-08-20T07:15:55Z</dc:date>
    </item>
    <item>
      <title>Re: SAS sum of next N rows for every row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-sum-of-next-N-rows-for-every-row/m-p/762779#M241536</link>
      <description>&lt;P&gt;You can sort the dataset by ID and also within ID in reverse order (by descending) "months" and then use the LAG function to get the previous 6 values and sum them up whilst checking if you are in the same value of ID in the LAG as in the current record.&lt;/P&gt;&lt;P&gt;There is a paper on this topic which discusses this issue and offers also a different approach without the LAG function:&amp;nbsp;&lt;A href="https://support.sas.com/resources/papers/proceedings16/11221-2016.pdf" target="_blank"&gt;https://support.sas.com/resources/papers/proceedings16/11221-2016.pdf&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Aug 2021 08:02:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-sum-of-next-N-rows-for-every-row/m-p/762779#M241536</guid>
      <dc:creator>miaeyg</dc:creator>
      <dc:date>2021-08-20T08:02:11Z</dc:date>
    </item>
    <item>
      <title>Re: SAS sum of next N rows for every row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-sum-of-next-N-rows-for-every-row/m-p/762787#M241539</link>
      <description>&lt;P&gt;Do you have SAS/ETS?&lt;/P&gt;</description>
      <pubDate>Fri, 20 Aug 2021 08:54:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-sum-of-next-N-rows-for-every-row/m-p/762787#M241539</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-08-20T08:54:19Z</dc:date>
    </item>
    <item>
      <title>Re: SAS sum of next N rows for every row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-sum-of-next-N-rows-for-every-row/m-p/762795#M241542</link>
      <description>&lt;P&gt;Otherwise, here is an approach.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I changed the have data set to show that it handles by-groups.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Months : yymmdd10. ID Number;
format Months yymmdd10.;
datalines;
2018-07-01 1 0
2018-08-01 1 0
2018-09-01 1 1
2018-10-01 1 3
2018-11-01 1 1
2018-12-01 1 2
2019-01-01 1 0
2019-02-01 1 0
2019-03-01 1 1
2019-04-01 1 0
2019-05-01 1 0
2019-06-01 1 0
2019-07-01 1 1
2019-08-01 1 0
2019-09-01 1 0
2019-10-01 1 2
2019-11-01 1 0
2019-12-01 1 0
2020-01-01 1 0
2020-02-01 1 0
2020-03-01 1 0
2020-04-01 1 0
2020-05-01 1 0
2020-06-01 1 0
2020-07-01 1 0
2020-08-01 1 1
2020-09-01 1 0
2020-10-01 1 0
2020-11-01 1 1
2020-12-01 1 0
2021-01-01 1 0
2021-02-01 1 1
2021-03-01 1 1
2021-04-01 1 0
2018-07-01 2 0
2018-08-01 2 0
2018-09-01 2 1
2018-10-01 2 3
2018-11-01 2 1
2018-12-01 2 2
2019-01-01 2 0
2019-02-01 2 0
2019-03-01 2 1
2019-04-01 2 0
2019-05-01 2 0
2019-06-01 2 0
2019-07-01 2 1
2019-08-01 2 0
2019-09-01 2 0
2019-10-01 2 2
2019-11-01 2 0
2019-12-01 2 0
2020-01-01 2 0
2020-02-01 2 0
2020-03-01 2 0
2020-04-01 2 0
2020-05-01 2 0
2020-06-01 2 0
2020-07-01 2 0
2020-08-01 2 1
2020-09-01 2 0
2020-10-01 2 0
2020-11-01 2 1
2020-12-01 2 0
2021-01-01 2 0
2021-02-01 2 1
2021-03-01 2 1
2021-04-01 2 0
; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop = i n);
   set have curobs = c nobs = nobs;
   Sum_Next_6Numbers = 0;
   
   do p = c + 1 to 6 + c;

      if p &amp;gt; nobs then do;
         Sum_Next_6Numbers = .; leave;
      end;

      set have(keep = Number ID rename = (Number = n id = i)) point = p;

      if id ne i then do;
         Sum_Next_6Numbers = .; leave;
      end;

      Sum_Next_6Numbers + n;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;Obs Months      ID   Number Sum_Next_6Numbers 
1   2018-07-01  1    0      7 
2   2018-08-01  1    0      7 
3   2018-09-01  1    1      7 
4   2018-10-01  1    3      4 
5   2018-11-01  1    1      3 
6   2018-12-01  1    2      1 
7   2019-01-01  1    0      2 
8   2019-02-01  1    0      2 
9   2019-03-01  1    1      1 
10  2019-04-01  1    0      3 
11  2019-05-01  1    0      3 
12  2019-06-01  1    0      3 
13  2019-07-01  1    1      2 
14  2019-08-01  1    0      2 
15  2019-09-01  1    0      2 
16  2019-10-01  1    2      0 
17  2019-11-01  1    0      0 
18  2019-12-01  1    0      0 
19  2020-01-01  1    0      0 
20  2020-02-01  1    0      1 
21  2020-03-01  1    0      1 
22  2020-04-01  1    0      1 
23  2020-05-01  1    0      2 
24  2020-06-01  1    0      2 
25  2020-07-01  1    0      2 
26  2020-08-01  1    1      2 
27  2020-09-01  1    0      3 
28  2020-10-01  1    0      3 
29  2020-11-01  1    1      . 
30  2020-12-01  1    0      . 
31  2021-01-01  1    0      . 
32  2021-02-01  1    1      . 
33  2021-03-01  1    1      . 
34  2021-04-01  1    0      . 
35  2018-07-01  2    0      7 
36  2018-08-01  2    0      7 
37  2018-09-01  2    1      7 
38  2018-10-01  2    3      4 
39  2018-11-01  2    1      3 
40  2018-12-01  2    2      1 
41  2019-01-01  2    0      2 
42  2019-02-01  2    0      2 
43  2019-03-01  2    1      1 
44  2019-04-01  2    0      3 
45  2019-05-01  2    0      3 
46  2019-06-01  2    0      3 
47  2019-07-01  2    1      2 
48  2019-08-01  2    0      2 
49  2019-09-01  2    0      2 
50  2019-10-01  2    2      0 
51  2019-11-01  2    0      0 
52  2019-12-01  2    0      0 
53  2020-01-01  2    0      0 
54  2020-02-01  2    0      1 
55  2020-03-01  2    0      1 
56  2020-04-01  2    0      1 
57  2020-05-01  2    0      2 
58  2020-06-01  2    0      2 
59  2020-07-01  2    0      2 
60  2020-08-01  2    1      2 
61  2020-09-01  2    0      3 
62  2020-10-01  2    0      3 
63  2020-11-01  2    1      . 
64  2020-12-01  2    0      . 
65  2021-01-01  2    0      . 
66  2021-02-01  2    1      . 
67  2021-03-01  2    1      . 
68  2021-04-01  2    0      . &lt;/PRE&gt;
&lt;/LI-SPOILER&gt;</description>
      <pubDate>Fri, 20 Aug 2021 09:25:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-sum-of-next-N-rows-for-every-row/m-p/762795#M241542</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-08-20T09:25:35Z</dc:date>
    </item>
    <item>
      <title>Re: SAS sum of next N rows for every row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-sum-of-next-N-rows-for-every-row/m-p/762812#M241555</link>
      <description>I don't have SAS/ETS and this code works very well. Thank you very much I'm appreciated!</description>
      <pubDate>Fri, 20 Aug 2021 11:39:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-sum-of-next-N-rows-for-every-row/m-p/762812#M241555</guid>
      <dc:creator>zdc</dc:creator>
      <dc:date>2021-08-20T11:39:42Z</dc:date>
    </item>
    <item>
      <title>Re: SAS sum of next N rows for every row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-sum-of-next-N-rows-for-every-row/m-p/762813#M241556</link>
      <description>Thank you very much, this helped me a lot. I'm appreciated!</description>
      <pubDate>Fri, 20 Aug 2021 11:40:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-sum-of-next-N-rows-for-every-row/m-p/762813#M241556</guid>
      <dc:creator>zdc</dc:creator>
      <dc:date>2021-08-20T11:40:35Z</dc:date>
    </item>
  </channel>
</rss>

