<?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 by 2 columns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-sum-of-next-N-rows-for-every-row-by-2-columns/m-p/765062#M242309</link>
    <description>&lt;P&gt;Hi,&amp;nbsp;I recently asked a question about SAS sum of next N rows for every row&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Programming/SAS-sum-of-next-N-rows-for-every-row/m-p/762775#M241534" target="_self"&gt;here&lt;/A&gt;&amp;nbsp;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is exactly the answer to the question and works well (by one column).&lt;/P&gt;&lt;LI-CODE lang="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;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So basically, I want to sum up next 6 row's values(number column) by 2 columns(id and id2). 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;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset like this for each ID and ID2;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-SPOILER&gt;&lt;LI-CODE lang="sas"&gt;data have;
input Months ID ID2 Number;
2018-07-01 1 1 0 
2018-08-01 1 1 0 
2018-09-01 1 1 0 
2018-10-01 1 1 1 
2018-11-01 1 1 0 
2018-12-01 1 1 1 
2019-01-01 1 1 0 
2019-02-01 1 1 0 
2019-03-01 1 1 0 
2019-04-01 1 1 0 
2019-05-01 1 1 0 
2019-06-01 1 1 0 
2019-07-01 1 1 0 
2019-08-01 1 1 0 
2019-09-01 1 1 0 
2019-10-01 1 1 0 
2019-11-01 1 1 0 
2019-12-01 1 1 0 
2020-01-01 1 1 0 
2020-02-01 1 1 0 
2020-03-01 1 1 0 
2020-04-01 1 1 0 
2020-05-01 1 1 0 
2020-06-01 1 1 0 
2020-07-01 1 1 0 
2020-08-01 1 1 0 
2020-09-01 1 1 0 
2020-10-01 1 1 0 
2020-11-01 1 1 1 
2020-12-01 1 1 0 
2021-01-01 1 1 0 
2021-02-01 1 1 0 
2021-03-01 1 1 0 
2021-04-01 1 1 0
2018-07-01 1 2 0 
2018-08-01 1 2 0 
2018-09-01 1 2 0 
2018-10-01 1 2 0 
2018-11-01 1 2 0 
2018-12-01 1 2 0 
2019-01-01 1 2 0 
2019-02-01 1 2 0 
2019-03-01 1 2 0 
2019-04-01 1 2 0 
2019-05-01 1 2 0 
2019-06-01 1 2 0 
2019-07-01 1 2 0 
2019-08-01 1 2 0 
2019-09-01 1 2 0 
2019-10-01 1 2 0 
2019-11-01 1 2 0 
2019-12-01 1 2 0 
2020-01-01 1 2 0 
2020-02-01 1 2 0 
2020-03-01 1 2 0 
2020-04-01 1 2 0 
2020-05-01 1 2 0 
2020-06-01 1 2 0 
2020-07-01 1 2 0 
2020-08-01 1 2 0 
2020-09-01 1 2 0 
2020-10-01 1 2 0 
2020-11-01 1 2 0 
2020-12-01 1 2 0 
2021-01-01 1 2 0 
2021-02-01 1 2 0 
2021-03-01 1 2 0 
2021-04-01 1 2 0
2018-07-01 2 1 0 
2018-08-01 2 1 0 
2018-09-01 2 1 0 
2018-10-01 2 1 0 
2018-11-01 2 1 0 
2018-12-01 2 1 0 
2019-01-01 2 1 0 
2019-02-01 2 1 0 
2019-03-01 2 1 0 
2019-04-01 2 1 0 
2019-05-01 2 1 0 
2019-06-01 2 1 0 
2019-07-01 2 1 0 
2019-08-01 2 1 0 
2019-09-01 2 1 0 
2019-10-01 2 1 0 
2019-11-01 2 1 0 
2019-12-01 2 1 0 
2020-01-01 2 1 0 
2020-02-01 2 1 0 
2020-03-01 2 1 0 
2020-04-01 2 1 0 
2020-05-01 2 1 0 
2020-06-01 2 1 0 
2020-07-01 2 1 0 
2020-08-01 2 1 1 
2020-09-01 2 1 0 
2020-10-01 2 1 0 
2020-11-01 2 1 0 
2020-12-01 2 1 0 
2021-01-01 2 1 0 
2021-02-01 2 1 0 
2021-03-01 2 1 0 
2021-04-01 2 1 0
....... ....... ....... &lt;/LI-CODE&gt;&lt;/LI-SPOILER&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want a dataset like this;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-SPOILER&gt;&lt;LI-CODE lang="sas"&gt;data want;
input Months ID ID2 Number Sum_Next_6Numbers;
2018-07-01 1 1 0 2
2018-08-01 1 1 0 2
2018-09-01 1 1 0 2
2018-10-01 1 1 1 1
2018-11-01 1 1 0 1
2018-12-01 1 1 1 0
2019-01-01 1 1 0 0
2019-02-01 1 1 0 0
2019-03-01 1 1 0 0
2019-04-01 1 1 0 0
2019-05-01 1 1 0 0
2019-06-01 1 1 0 0
2019-07-01 1 1 0 0
2019-08-01 1 1 0 0
2019-09-01 1 1 0 0
2019-10-01 1 1 0 0
2019-11-01 1 1 0 0
2019-12-01 1 1 0 0
2020-01-01 1 1 0 0
2020-02-01 1 1 0 0
2020-03-01 1 1 0 0
2020-04-01 1 1 0 0
2020-05-01 1 1 0 1
2020-06-01 1 1 0 1
2020-07-01 1 1 0 1
2020-08-01 1 1 0 1
2020-09-01 1 1 0 1
2020-10-01 1 1 0 1
2020-11-01 1 1 1 NaN
2020-12-01 1 1 0 NaN
2021-01-01 1 1 0 NaN
2021-02-01 1 1 0 NaN
2021-03-01 1 1 0 NaN
2021-04-01 1 1 0 NaN
2018-07-01 1 2 0 0
2018-08-01 1 2 0 0
2018-09-01 1 2 0 0
2018-10-01 1 2 0 0
2018-11-01 1 2 0 0
2018-12-01 1 2 0 0
2019-01-01 1 2 0 0
2019-02-01 1 2 0 0
2019-03-01 1 2 0 0
2019-04-01 1 2 0 0
2019-05-01 1 2 0 0
2019-06-01 1 2 0 0
2019-07-01 1 2 0 0
2019-08-01 1 2 0 0
2019-09-01 1 2 0 0
2019-10-01 1 2 0 0
2019-11-01 1 2 0 0
2019-12-01 1 2 0 0
2020-01-01 1 2 0 0
2020-02-01 1 2 0 0
2020-03-01 1 2 0 0
2020-04-01 1 2 0 0
2020-05-01 1 2 0 0
2020-06-01 1 2 0 0
2020-07-01 1 2 0 0
2020-08-01 1 2 0 0
2020-09-01 1 2 0 0
2020-10-01 1 2 0 0
2020-11-01 1 2 0 NaN
2020-12-01 1 2 0 NaN
2021-01-01 1 2 0 NaN
2021-02-01 1 2 0 NaN
2021-03-01 1 2 0 NaN
2021-04-01 1 2 0 NaN
2018-07-01 2 1 0 0
2018-08-01 2 1 0 0
2018-09-01 2 1 0 0
2018-10-01 2 1 0 0
2018-11-01 2 1 0 0
2018-12-01 2 1 0 0
2019-01-01 2 1 0 0
2019-02-01 2 1 0 0
2019-03-01 2 1 0 0
2019-04-01 2 1 0 0
2019-05-01 2 1 0 0
2019-06-01 2 1 0 0
2019-07-01 2 1 0 0
2019-08-01 2 1 0 0
2019-09-01 2 1 0 0
2019-10-01 2 1 0 0
2019-11-01 2 1 0 0
2019-12-01 2 1 0 0
2020-01-01 2 1 0 0
2020-02-01 2 1 0 1
2020-03-01 2 1 0 1
2020-04-01 2 1 0 1
2020-05-01 2 1 0 1
2020-06-01 2 1 0 1
2020-07-01 2 1 0 1
2020-08-01 2 1 1 0
2020-09-01 2 1 0 0
2020-10-01 2 1 0 0
2020-11-01 2 1 0 NaN
2020-12-01 2 1 0 NaN
2021-01-01 2 1 0 NaN
2021-02-01 2 1 0 NaN
2021-03-01 2 1 0 NaN
2021-04-01 2 1 0 NaN
....... ....... .......&lt;/LI-CODE&gt;&lt;/LI-SPOILER&gt;&lt;P&gt;I did it with a nested for loop in python. I'm new to sas and I'm confused.&amp;nbsp;Is there a way to do this in SAS? Thanks in advance.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 31 Aug 2021 14:08:48 GMT</pubDate>
    <dc:creator>zdc</dc:creator>
    <dc:date>2021-08-31T14:08:48Z</dc:date>
    <item>
      <title>SAS sum of next N rows for every row by 2 columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-sum-of-next-N-rows-for-every-row-by-2-columns/m-p/765062#M242309</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;I recently asked a question about SAS sum of next N rows for every row&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Programming/SAS-sum-of-next-N-rows-for-every-row/m-p/762775#M241534" target="_self"&gt;here&lt;/A&gt;&amp;nbsp;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is exactly the answer to the question and works well (by one column).&lt;/P&gt;&lt;LI-CODE lang="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;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So basically, I want to sum up next 6 row's values(number column) by 2 columns(id and id2). 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;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset like this for each ID and ID2;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-SPOILER&gt;&lt;LI-CODE lang="sas"&gt;data have;
input Months ID ID2 Number;
2018-07-01 1 1 0 
2018-08-01 1 1 0 
2018-09-01 1 1 0 
2018-10-01 1 1 1 
2018-11-01 1 1 0 
2018-12-01 1 1 1 
2019-01-01 1 1 0 
2019-02-01 1 1 0 
2019-03-01 1 1 0 
2019-04-01 1 1 0 
2019-05-01 1 1 0 
2019-06-01 1 1 0 
2019-07-01 1 1 0 
2019-08-01 1 1 0 
2019-09-01 1 1 0 
2019-10-01 1 1 0 
2019-11-01 1 1 0 
2019-12-01 1 1 0 
2020-01-01 1 1 0 
2020-02-01 1 1 0 
2020-03-01 1 1 0 
2020-04-01 1 1 0 
2020-05-01 1 1 0 
2020-06-01 1 1 0 
2020-07-01 1 1 0 
2020-08-01 1 1 0 
2020-09-01 1 1 0 
2020-10-01 1 1 0 
2020-11-01 1 1 1 
2020-12-01 1 1 0 
2021-01-01 1 1 0 
2021-02-01 1 1 0 
2021-03-01 1 1 0 
2021-04-01 1 1 0
2018-07-01 1 2 0 
2018-08-01 1 2 0 
2018-09-01 1 2 0 
2018-10-01 1 2 0 
2018-11-01 1 2 0 
2018-12-01 1 2 0 
2019-01-01 1 2 0 
2019-02-01 1 2 0 
2019-03-01 1 2 0 
2019-04-01 1 2 0 
2019-05-01 1 2 0 
2019-06-01 1 2 0 
2019-07-01 1 2 0 
2019-08-01 1 2 0 
2019-09-01 1 2 0 
2019-10-01 1 2 0 
2019-11-01 1 2 0 
2019-12-01 1 2 0 
2020-01-01 1 2 0 
2020-02-01 1 2 0 
2020-03-01 1 2 0 
2020-04-01 1 2 0 
2020-05-01 1 2 0 
2020-06-01 1 2 0 
2020-07-01 1 2 0 
2020-08-01 1 2 0 
2020-09-01 1 2 0 
2020-10-01 1 2 0 
2020-11-01 1 2 0 
2020-12-01 1 2 0 
2021-01-01 1 2 0 
2021-02-01 1 2 0 
2021-03-01 1 2 0 
2021-04-01 1 2 0
2018-07-01 2 1 0 
2018-08-01 2 1 0 
2018-09-01 2 1 0 
2018-10-01 2 1 0 
2018-11-01 2 1 0 
2018-12-01 2 1 0 
2019-01-01 2 1 0 
2019-02-01 2 1 0 
2019-03-01 2 1 0 
2019-04-01 2 1 0 
2019-05-01 2 1 0 
2019-06-01 2 1 0 
2019-07-01 2 1 0 
2019-08-01 2 1 0 
2019-09-01 2 1 0 
2019-10-01 2 1 0 
2019-11-01 2 1 0 
2019-12-01 2 1 0 
2020-01-01 2 1 0 
2020-02-01 2 1 0 
2020-03-01 2 1 0 
2020-04-01 2 1 0 
2020-05-01 2 1 0 
2020-06-01 2 1 0 
2020-07-01 2 1 0 
2020-08-01 2 1 1 
2020-09-01 2 1 0 
2020-10-01 2 1 0 
2020-11-01 2 1 0 
2020-12-01 2 1 0 
2021-01-01 2 1 0 
2021-02-01 2 1 0 
2021-03-01 2 1 0 
2021-04-01 2 1 0
....... ....... ....... &lt;/LI-CODE&gt;&lt;/LI-SPOILER&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want a dataset like this;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-SPOILER&gt;&lt;LI-CODE lang="sas"&gt;data want;
input Months ID ID2 Number Sum_Next_6Numbers;
2018-07-01 1 1 0 2
2018-08-01 1 1 0 2
2018-09-01 1 1 0 2
2018-10-01 1 1 1 1
2018-11-01 1 1 0 1
2018-12-01 1 1 1 0
2019-01-01 1 1 0 0
2019-02-01 1 1 0 0
2019-03-01 1 1 0 0
2019-04-01 1 1 0 0
2019-05-01 1 1 0 0
2019-06-01 1 1 0 0
2019-07-01 1 1 0 0
2019-08-01 1 1 0 0
2019-09-01 1 1 0 0
2019-10-01 1 1 0 0
2019-11-01 1 1 0 0
2019-12-01 1 1 0 0
2020-01-01 1 1 0 0
2020-02-01 1 1 0 0
2020-03-01 1 1 0 0
2020-04-01 1 1 0 0
2020-05-01 1 1 0 1
2020-06-01 1 1 0 1
2020-07-01 1 1 0 1
2020-08-01 1 1 0 1
2020-09-01 1 1 0 1
2020-10-01 1 1 0 1
2020-11-01 1 1 1 NaN
2020-12-01 1 1 0 NaN
2021-01-01 1 1 0 NaN
2021-02-01 1 1 0 NaN
2021-03-01 1 1 0 NaN
2021-04-01 1 1 0 NaN
2018-07-01 1 2 0 0
2018-08-01 1 2 0 0
2018-09-01 1 2 0 0
2018-10-01 1 2 0 0
2018-11-01 1 2 0 0
2018-12-01 1 2 0 0
2019-01-01 1 2 0 0
2019-02-01 1 2 0 0
2019-03-01 1 2 0 0
2019-04-01 1 2 0 0
2019-05-01 1 2 0 0
2019-06-01 1 2 0 0
2019-07-01 1 2 0 0
2019-08-01 1 2 0 0
2019-09-01 1 2 0 0
2019-10-01 1 2 0 0
2019-11-01 1 2 0 0
2019-12-01 1 2 0 0
2020-01-01 1 2 0 0
2020-02-01 1 2 0 0
2020-03-01 1 2 0 0
2020-04-01 1 2 0 0
2020-05-01 1 2 0 0
2020-06-01 1 2 0 0
2020-07-01 1 2 0 0
2020-08-01 1 2 0 0
2020-09-01 1 2 0 0
2020-10-01 1 2 0 0
2020-11-01 1 2 0 NaN
2020-12-01 1 2 0 NaN
2021-01-01 1 2 0 NaN
2021-02-01 1 2 0 NaN
2021-03-01 1 2 0 NaN
2021-04-01 1 2 0 NaN
2018-07-01 2 1 0 0
2018-08-01 2 1 0 0
2018-09-01 2 1 0 0
2018-10-01 2 1 0 0
2018-11-01 2 1 0 0
2018-12-01 2 1 0 0
2019-01-01 2 1 0 0
2019-02-01 2 1 0 0
2019-03-01 2 1 0 0
2019-04-01 2 1 0 0
2019-05-01 2 1 0 0
2019-06-01 2 1 0 0
2019-07-01 2 1 0 0
2019-08-01 2 1 0 0
2019-09-01 2 1 0 0
2019-10-01 2 1 0 0
2019-11-01 2 1 0 0
2019-12-01 2 1 0 0
2020-01-01 2 1 0 0
2020-02-01 2 1 0 1
2020-03-01 2 1 0 1
2020-04-01 2 1 0 1
2020-05-01 2 1 0 1
2020-06-01 2 1 0 1
2020-07-01 2 1 0 1
2020-08-01 2 1 1 0
2020-09-01 2 1 0 0
2020-10-01 2 1 0 0
2020-11-01 2 1 0 NaN
2020-12-01 2 1 0 NaN
2021-01-01 2 1 0 NaN
2021-02-01 2 1 0 NaN
2021-03-01 2 1 0 NaN
2021-04-01 2 1 0 NaN
....... ....... .......&lt;/LI-CODE&gt;&lt;/LI-SPOILER&gt;&lt;P&gt;I did it with a nested for loop in python. I'm new to sas and I'm confused.&amp;nbsp;Is there a way to do this in SAS? Thanks in advance.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 31 Aug 2021 14:08:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-sum-of-next-N-rows-for-every-row-by-2-columns/m-p/765062#M242309</guid>
      <dc:creator>zdc</dc:creator>
      <dc:date>2021-08-31T14:08:48Z</dc:date>
    </item>
    <item>
      <title>Re: SAS sum of next N rows for every row by 2 columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-sum-of-next-N-rows-for-every-row-by-2-columns/m-p/765098#M242321</link>
      <description>&lt;P&gt;Do you have SAS/ETS? This is incredibly trivial with PROC EXPAND - sort of like using the right package in Python.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;title;

data test;
   input year qtr x;
   date = yyq( year, qtr );
   format date yyqc.;
datalines;
1989 3 5238
1989 4 5289
1990 1 5375
1990 2 5443
1990 3 5514
1990 4 5527
1991 1 5557
1991 2 5615
;

proc expand data=test out=out method=none;
   id date;
   convert x = x_lag2   / transformout=(lag 2);
   convert x = x_lag1   / transformout=(lag 1);
   convert x;
   convert x = x_lead1  / transformout=(lead 1);
   convert x = x_lead2  / transformout=(lead 2);
   convert x = x_movave / transformout=(movave 3);
run;

title "Transformed Series";
proc print data=out;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 31 Aug 2021 16:17:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-sum-of-next-N-rows-for-every-row-by-2-columns/m-p/765098#M242321</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-08-31T16:17:48Z</dc:date>
    </item>
  </channel>
</rss>

