<?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: Conditional summation and enumeration in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditional-summation-and-enumeration/m-p/512765#M138125</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID Diff Duration;
datalines;
1 . 10
1 2 13
1 25 14
1 5 5
1 2 7
1 2 9
2 . 3
2 2 5
2 10 7
3 . 4
;
run;


data want;


set have;
	by id;

retain Sequence Cummulative_Duration;

if first.id then do;
	Sequence =1;
	Cummulative_Duration=Duration;
end;

else if Diff &amp;lt; 3 then do;
	Sequence =Sequence;
	Cummulative_Duration+Duration;
end;
	else do;
		Sequence+1;
		Cummulative_Duration=Duration;
	end;


run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 13 Nov 2018 22:00:47 GMT</pubDate>
    <dc:creator>r_behata</dc:creator>
    <dc:date>2018-11-13T22:00:47Z</dc:date>
    <item>
      <title>Conditional summation and enumeration</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-summation-and-enumeration/m-p/512758#M138121</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I am new to SAS (using SAS Enterprise 7.1) and I struggle to solve the following problem:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example Dataset:&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input ID Diff Duration;&lt;BR /&gt;datalines;&lt;BR /&gt;1 . 10&lt;BR /&gt;1 2 13&lt;BR /&gt;1 25 14&lt;BR /&gt;1 5 5&lt;BR /&gt;1 2 7&lt;BR /&gt;1 2 9&lt;BR /&gt;2 . 3&lt;BR /&gt;2 2 5&lt;BR /&gt;2 10 7&lt;BR /&gt;3 . 4&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Task: I would like to sum up Duration and give an enumeration variable for a Sequence but only if IDs are similar and Diff is not . and &lt;span class="lia-unicode-emoji" title=":red_heart:"&gt;❤️&lt;/span&gt; (so if Diff&amp;lt;3 I call it a Sequence). What makes the task difficult is that when comparing to subsequent rows the enumeration variable '&lt;SPAN&gt;Sequence'&amp;nbsp;&lt;/SPAN&gt;should only continue counting if the next row does not belong to the current sequence (so if Diff is not .&amp;nbsp;and &amp;lt;3) whereas the summation '&lt;SPAN&gt;Cummulative_Duration'&amp;nbsp;&lt;/SPAN&gt;should start form the beginning in that case.&lt;/P&gt;&lt;P&gt;So the result should look like the following table:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID Diff Duration Sequence Cummulative_Duration ;&lt;BR /&gt;1 . 10 1 10&lt;BR /&gt;1 2 13 1 23&lt;BR /&gt;1 25 14 2 14&lt;BR /&gt;1 5 5 3 5&lt;BR /&gt;1 2 7 3 12&lt;BR /&gt;1 2 9 3 21&lt;BR /&gt;2 . 3 1 3&lt;BR /&gt;2 2 5 1 8&lt;BR /&gt;2 10 7 2 7&lt;BR /&gt;3 . 4 1 4&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;Thanks in advance for taking your time to help me solving this (I tried already half a day ...)!&lt;/P&gt;</description>
      <pubDate>Tue, 13 Nov 2018 21:31:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-summation-and-enumeration/m-p/512758#M138121</guid>
      <dc:creator>haukewiegand</dc:creator>
      <dc:date>2018-11-13T21:31:12Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional summation and enumeration</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-summation-and-enumeration/m-p/512765#M138125</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID Diff Duration;
datalines;
1 . 10
1 2 13
1 25 14
1 5 5
1 2 7
1 2 9
2 . 3
2 2 5
2 10 7
3 . 4
;
run;


data want;


set have;
	by id;

retain Sequence Cummulative_Duration;

if first.id then do;
	Sequence =1;
	Cummulative_Duration=Duration;
end;

else if Diff &amp;lt; 3 then do;
	Sequence =Sequence;
	Cummulative_Duration+Duration;
end;
	else do;
		Sequence+1;
		Cummulative_Duration=Duration;
	end;


run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 13 Nov 2018 22:00:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-summation-and-enumeration/m-p/512765#M138125</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2018-11-13T22:00:47Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional summation and enumeration</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-summation-and-enumeration/m-p/512774#M138129</link>
      <description>&lt;P&gt;Assuming i understand your req:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID Diff Duration;
datalines;
1 . 10
1 2 13
1 25 14
1 5 5
1 2 7
1 2 9
2 . 3
2 2 5
2 10 7
3 . 4
;
run;

data want;
if 0 then set have;
sequence=1;
cumulative=0;
do until(last.id);
set have;
by id;
if diff&amp;gt;=3 then do; sequence+1;cumulative=0;end;
cumulative+Duration;
output;
end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 13 Nov 2018 22:38:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-summation-and-enumeration/m-p/512774#M138129</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-11-13T22:38:02Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional summation and enumeration</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-summation-and-enumeration/m-p/512776#M138130</link>
      <description>&lt;P&gt;x&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID Diff Duration;
datalines;
1 . 10
1 2 13
1 25 14
1 5 5
1 2 7
1 2 9
2 . 3
2 2 5
2 10 7
3 . 4
;
data want;
  set have;
  by id;
  if diff&amp;lt;=3 then cumulative_duration+duration;
  else cumulative_duration=duration;
  sequence+(diff&amp;gt;3);
  if first.id then do;
    sequence=1;
    cumulative_duration=duration;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Notice that the "if diff&amp;lt;=3 then cumulative_duration+duration;" statement doesn't have an equal sign, typically needed to assign a value.&amp;nbsp;&amp;nbsp; This means the expression "cumulative_duration+duration" is a summing expression, not an assignment.&amp;nbsp; The summing expression means (1) perform the summation, (2) using a RETAINED value of cumulative_duration, and (3) if cumulative_duration is missing, treat it as a zero.&lt;/LI&gt;
&lt;LI&gt;The "sequence+(diff&amp;gt;3)" is a summing statement, adding 1 to sequence whenever diff&amp;gt;3).&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Tue, 13 Nov 2018 22:33:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-summation-and-enumeration/m-p/512776#M138130</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-11-13T22:33:49Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional summation and enumeration</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-summation-and-enumeration/m-p/512789#M138136</link>
      <description>Thanks for the explanations! Probably that was where my trials failed ...</description>
      <pubDate>Tue, 13 Nov 2018 23:43:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-summation-and-enumeration/m-p/512789#M138136</guid>
      <dc:creator>haukewiegand</dc:creator>
      <dc:date>2018-11-13T23:43:26Z</dc:date>
    </item>
  </channel>
</rss>

