<?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: How to calculate sum of two consecutive numbers in a column using loops in sas? in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-to-calculate-sum-of-two-consecutive-numbers-in-a-column/m-p/631478#M20936</link>
    <description>&lt;P&gt;&lt;SPAN&gt;Yes I want a general solution that handles n consecutive numbers.&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 12 Mar 2020 08:37:58 GMT</pubDate>
    <dc:creator>Saurabh_Rana</dc:creator>
    <dc:date>2020-03-12T08:37:58Z</dc:date>
    <item>
      <title>How to calculate sum of two consecutive numbers in a column using loops in sas?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-calculate-sum-of-two-consecutive-numbers-in-a-column/m-p/631460#M20927</link>
      <description>&lt;P&gt;I want to find sum of 2 consecutive numbers or 3 consecutive numbers group wise in a column as shown in the example below:-&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example- Sum of 2 consecutive numbers&lt;/P&gt;&lt;P&gt;Input Data&lt;/P&gt;&lt;P&gt;A 1&lt;/P&gt;&lt;P&gt;A 2&lt;/P&gt;&lt;P&gt;A 3&lt;/P&gt;&lt;P&gt;A 4&lt;/P&gt;&lt;P&gt;A 5&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;B 1&lt;/P&gt;&lt;P&gt;B 3&lt;/P&gt;&lt;P&gt;B 5&lt;/P&gt;&lt;P&gt;B 7&lt;/P&gt;&lt;P&gt;B 9&lt;/P&gt;&lt;P&gt;B 11&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A 3&lt;/P&gt;&lt;P&gt;A 5&lt;/P&gt;&lt;P&gt;A 7&lt;/P&gt;&lt;P&gt;A 9&lt;/P&gt;&lt;P&gt;A 5&lt;/P&gt;&lt;P&gt;B 4&lt;/P&gt;&lt;P&gt;B 8&lt;/P&gt;&lt;P&gt;B 12&lt;/P&gt;&lt;P&gt;B 16&lt;/P&gt;&lt;P&gt;B 20&lt;/P&gt;&lt;P&gt;B 11&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The last observation in each group is as it is since it is the last number.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2020 07:04:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-calculate-sum-of-two-consecutive-numbers-in-a-column/m-p/631460#M20927</guid>
      <dc:creator>Saurabh_Rana</dc:creator>
      <dc:date>2020-03-12T07:04:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate sum of two consecutive numbers in a column using loops in sas?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-calculate-sum-of-two-consecutive-numbers-in-a-column/m-p/631461#M20928</link>
      <description>&lt;P&gt;Here is one way&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $ num;
datalines;
A 1
A 2
A 3
A 4
A 5
B 1
B 3
B 5
B 7
B 9
B 11
;

data want (keep=id sum);
    merge have
          have (firstobs=2 rename=(num=_num id=_id));
    if id=_id then sum=num+_num;
    else sum=num;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;id sum 
A  3 
A  5 
A  7 
A  9 
A  5 
B  4 
B  8 
B  12
B  16
B  20
B  11 &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2020 07:31:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-calculate-sum-of-two-consecutive-numbers-in-a-column/m-p/631461#M20928</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-03-12T07:31:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate sum of two consecutive numbers in a column using loops in sas?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-calculate-sum-of-two-consecutive-numbers-in-a-column/m-p/631463#M20929</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/316224"&gt;@Saurabh_Rana&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	Input v1 $ v2;
	datalines;
A 1
A 2
A 3
A 4
A 5
B 1
B 3
B 5
B 7
B 9
B 11
;
run;

data want;
	merge have have(firstobs=2 rename=(V1=_V1 V2=_V2));
	if V1=_V1 then v2_sum = V2+_V2;
	else v2_sum = V2;
	drop _: V2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture d’écran 2020-03-12 à 08.30.57.png" style="width: 77px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/36784i5A456DA60E35A57E/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture d’écran 2020-03-12 à 08.30.57.png" alt="Capture d’écran 2020-03-12 à 08.30.57.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2020 07:31:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-calculate-sum-of-two-consecutive-numbers-in-a-column/m-p/631463#M20929</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-03-12T07:31:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate sum of two consecutive numbers in a column using loops in sas?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-calculate-sum-of-two-consecutive-numbers-in-a-column/m-p/631467#M20930</link>
      <description>&lt;P&gt;How can I do this using loops&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2020 07:53:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-calculate-sum-of-two-consecutive-numbers-in-a-column/m-p/631467#M20930</guid>
      <dc:creator>Saurabh_Rana</dc:creator>
      <dc:date>2020-03-12T07:53:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate sum of two consecutive numbers in a column using loops in sas?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-calculate-sum-of-two-consecutive-numbers-in-a-column/m-p/631468#M20931</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/316224"&gt;@Saurabh_Rana&lt;/a&gt;&amp;nbsp;why do you want to use a loop to do this?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2020 07:54:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-calculate-sum-of-two-consecutive-numbers-in-a-column/m-p/631468#M20931</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-03-12T07:54:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate sum of two consecutive numbers in a column using loops in sas?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-calculate-sum-of-two-consecutive-numbers-in-a-column/m-p/631470#M20932</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/316224"&gt;@Saurabh_Rana&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;How can I do this using loops&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You already use a loop: the implicit loop that the data step does over the observations in the input dataset(s). Your question therefore makes no sense.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please mark one of the answers (both do what you want) as the solution.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2020 08:06:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-calculate-sum-of-two-consecutive-numbers-in-a-column/m-p/631470#M20932</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-03-12T08:06:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate sum of two consecutive numbers in a column using loops in sas?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-calculate-sum-of-two-consecutive-numbers-in-a-column/m-p/631473#M20933</link>
      <description>&lt;P&gt;How can I do the same process for sum of 3 consecutive numbers?&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2020 08:22:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-calculate-sum-of-two-consecutive-numbers-in-a-column/m-p/631473#M20933</guid>
      <dc:creator>Saurabh_Rana</dc:creator>
      <dc:date>2020-03-12T08:22:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate sum of two consecutive numbers in a column using loops in sas?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-calculate-sum-of-two-consecutive-numbers-in-a-column/m-p/631475#M20934</link>
      <description>&lt;P&gt;Do you want a general solution that handles n consecutive numbers or is three your actual goal?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2020 08:26:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-calculate-sum-of-two-consecutive-numbers-in-a-column/m-p/631475#M20934</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-03-12T08:26:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate sum of two consecutive numbers in a column using loops in sas?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-calculate-sum-of-two-consecutive-numbers-in-a-column/m-p/631476#M20935</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/316224"&gt;@Saurabh_Rana&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;How can I do the same process for sum of 3 consecutive numbers?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You just need to use a second lookahead with firstobs=3, and expand the logic in the data step accordingly. Try your hand at the code you already got, and if you run into problems, post the code and the log, so we can help you fixing it. Learn by doing.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2020 08:29:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-calculate-sum-of-two-consecutive-numbers-in-a-column/m-p/631476#M20935</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-03-12T08:29:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate sum of two consecutive numbers in a column using loops in sas?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-calculate-sum-of-two-consecutive-numbers-in-a-column/m-p/631478#M20936</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Yes I want a general solution that handles n consecutive numbers.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2020 08:37:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-calculate-sum-of-two-consecutive-numbers-in-a-column/m-p/631478#M20936</guid>
      <dc:creator>Saurabh_Rana</dc:creator>
      <dc:date>2020-03-12T08:37:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate sum of two consecutive numbers in a column using loops in sas?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-calculate-sum-of-two-consecutive-numbers-in-a-column/m-p/631483#M20937</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/316224"&gt;@Saurabh_Rana&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;SPAN&gt;Yes I want a general solution that handles n consecutive numbers.&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then let's start with code that does it (Rule #1 of macro development) and see what needs to be dynamic:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
merge
  have
  have (firstobs=2 rename=(V1=_V1_1 V2=_V2_1))
  have (firstobs=3 rename=(V1=_V1_2 V2=_V2_2))
;
v2_sum = V2;
if V1 = _V1_1
then v2_sum = v2_sum +_V2_1;
if V1 = _V1_2
then v2_sum = v2_sum +_V2_2;
drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This code solves the "sum for three/double look-ahead" issue. Since I already wrote it in a fashion that makes repeating code parts visible (that's one reason why proper code formatting is an absolute MUST for anyone wanting to be called a "coder"!!!), it is not that hard to create a macro loop that repeats that code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro repeat(count=0);
%local i;
data want&amp;amp;count;
merge
  have
%do i = 1 %to &amp;amp;count;
  have (firstobs=%eval(&amp;amp;i + 1) rename=(V1=_V1_&amp;amp;i V2=_V2_&amp;amp;i))
%end;
;
v2_sum = V2;
%do i = 1 %to &amp;amp;count;
if V1 = _V1_&amp;amp;i
then v2_sum = v2_sum +_V2_&amp;amp;i;
%end;
drop _:;
run;
%mend;
%repeat(count=1)
%repeat(count=2)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 12 Mar 2020 09:03:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-calculate-sum-of-two-consecutive-numbers-in-a-column/m-p/631483#M20937</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-03-12T09:03:10Z</dc:date>
    </item>
  </channel>
</rss>

