<?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 Sum a column only when consecutive rows have same entries in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sum-a-column-only-when-consecutive-rows-have-same-entries/m-p/443051#M282813</link>
    <description>Hi,&lt;BR /&gt;&lt;BR /&gt;I have a dataset with a by variable and a flag with 1 or 0 values. I want to sum only the first consecutive entries of 1 of the flag variable and output them.&lt;BR /&gt;Ex.&lt;BR /&gt;&lt;BR /&gt;ID flag&lt;BR /&gt;A 0&lt;BR /&gt;A 1&lt;BR /&gt;A 1&lt;BR /&gt;A 0&lt;BR /&gt;B 1&lt;BR /&gt;B 0&lt;BR /&gt;B 0&lt;BR /&gt;C 1&lt;BR /&gt;C 1&lt;BR /&gt;C 1&lt;BR /&gt;C 0&lt;BR /&gt;C 1&lt;BR /&gt;C 1&lt;BR /&gt;&lt;BR /&gt;The output dataset i want is:&lt;BR /&gt;ID. Count&lt;BR /&gt;A 2&lt;BR /&gt;B 1&lt;BR /&gt;C 3&lt;BR /&gt;&lt;BR /&gt;I have tried using retain statement and conditional sum of when flag =1 but the count is incorrect for ID C from the example above.&lt;BR /&gt;&lt;BR /&gt;Any help will be appreciated.&lt;BR /&gt;&lt;BR /&gt;Thanks</description>
    <pubDate>Tue, 06 Mar 2018 21:13:35 GMT</pubDate>
    <dc:creator>Rohit_R</dc:creator>
    <dc:date>2018-03-06T21:13:35Z</dc:date>
    <item>
      <title>Sum a column only when consecutive rows have same entries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-a-column-only-when-consecutive-rows-have-same-entries/m-p/443051#M282813</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;I have a dataset with a by variable and a flag with 1 or 0 values. I want to sum only the first consecutive entries of 1 of the flag variable and output them.&lt;BR /&gt;Ex.&lt;BR /&gt;&lt;BR /&gt;ID flag&lt;BR /&gt;A 0&lt;BR /&gt;A 1&lt;BR /&gt;A 1&lt;BR /&gt;A 0&lt;BR /&gt;B 1&lt;BR /&gt;B 0&lt;BR /&gt;B 0&lt;BR /&gt;C 1&lt;BR /&gt;C 1&lt;BR /&gt;C 1&lt;BR /&gt;C 0&lt;BR /&gt;C 1&lt;BR /&gt;C 1&lt;BR /&gt;&lt;BR /&gt;The output dataset i want is:&lt;BR /&gt;ID. Count&lt;BR /&gt;A 2&lt;BR /&gt;B 1&lt;BR /&gt;C 3&lt;BR /&gt;&lt;BR /&gt;I have tried using retain statement and conditional sum of when flag =1 but the count is incorrect for ID C from the example above.&lt;BR /&gt;&lt;BR /&gt;Any help will be appreciated.&lt;BR /&gt;&lt;BR /&gt;Thanks</description>
      <pubDate>Tue, 06 Mar 2018 21:13:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-a-column-only-when-consecutive-rows-have-same-entries/m-p/443051#M282813</guid>
      <dc:creator>Rohit_R</dc:creator>
      <dc:date>2018-03-06T21:13:35Z</dc:date>
    </item>
    <item>
      <title>Re: Sum a column only when consecutive rows have same entries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-a-column-only-when-consecutive-rows-have-same-entries/m-p/443058#M282814</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/85933"&gt;@Rohit_R&lt;/a&gt; wrote:&lt;BR /&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;I have a dataset with a by variable and a flag with 1 or 0 values. I want to sum only the first consecutive entries of 1 of the flag variable and output them.&lt;BR /&gt;Ex.&lt;BR /&gt;&lt;BR /&gt;ID flag&lt;BR /&gt;A 0&lt;BR /&gt;A 1&lt;BR /&gt;A 1&lt;BR /&gt;A 0&lt;BR /&gt;B 1&lt;BR /&gt;B 0&lt;BR /&gt;B 0&lt;BR /&gt;C 1&lt;BR /&gt;C 1&lt;BR /&gt;C 1&lt;BR /&gt;C 0&lt;BR /&gt;C 1&lt;BR /&gt;C 1&lt;BR /&gt;&lt;BR /&gt;The output dataset i want is:&lt;BR /&gt;ID. Count&lt;BR /&gt;A 2&lt;BR /&gt;B 1&lt;BR /&gt;C 3&lt;BR /&gt;&lt;BR /&gt;I have tried using retain statement and conditional sum of when flag =1 but the count is incorrect for ID C from the example above.&lt;BR /&gt;&lt;BR /&gt;Any help will be appreciated.&lt;BR /&gt;&lt;BR /&gt;Thanks&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Since retain and conditional summing is the likely approach then show the code you used.&lt;/P&gt;
&lt;P&gt;Likely issues are not resetting the counter to 0 for the first value of the ID (hint) or&amp;nbsp;not resetting/ setting a flag when done within a group of ID values&lt;/P&gt;</description>
      <pubDate>Tue, 06 Mar 2018 21:22:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-a-column-only-when-consecutive-rows-have-same-entries/m-p/443058#M282814</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-03-06T21:22:48Z</dc:date>
    </item>
    <item>
      <title>Re: Sum a column only when consecutive rows have same entries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-a-column-only-when-consecutive-rows-have-same-entries/m-p/443073#M282815</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID $ flag;
datalines;
A 0
A 1
A 1
A 0
B 1
B 0
B 0
C 1
C 1
C 1
C 0
C 1
C 1
;

data want;
set have;
by  flag notsorted;
if first.flag and flag=1 then count=1;
else count+1;
if last.flag and flag=1 then do;output;count=0;end;
run;

proc sort data=want out=final_want(drop=flag) nodupkey;
by id flag;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 06 Mar 2018 21:45:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-a-column-only-when-consecutive-rows-have-same-entries/m-p/443073#M282815</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-03-06T21:45:23Z</dc:date>
    </item>
    <item>
      <title>Re: Sum a column only when consecutive rows have same entries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-a-column-only-when-consecutive-rows-have-same-entries/m-p/443081#M282816</link>
      <description>&lt;P&gt;It does present some logic challenges.&amp;nbsp; One way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;by ID flag notsorted;&lt;/P&gt;
&lt;P&gt;retain c count;&lt;/P&gt;
&lt;P&gt;if first.id then count=0;&lt;/P&gt;
&lt;P&gt;if first.flag then c=0;&lt;/P&gt;
&lt;P&gt;c + flag;&lt;/P&gt;
&lt;P&gt;if last.flag and count=0 then count=c;&lt;/P&gt;
&lt;P&gt;if last.id;&lt;/P&gt;
&lt;P&gt;keep id count;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Mar 2018 22:11:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-a-column-only-when-consecutive-rows-have-same-entries/m-p/443081#M282816</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-03-06T22:11:17Z</dc:date>
    </item>
    <item>
      <title>Re: Sum a column only when consecutive rows have same entries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-a-column-only-when-consecutive-rows-have-same-entries/m-p/443098#M282817</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID $ flag;
datalines;
A 0
A 1
A 1
A 0
B 1
B 0
B 0
C 1
C 1
C 1
C 0
C 1
C 1
;

data want;
f=0;
do until(last.id);
set have;
by id flag notsorted;
if first.id then count=0;
if flag then count+1;
if last.flag and flag and  not f then do;output;f=1;end;
end;
drop f:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 06 Mar 2018 23:51:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-a-column-only-when-consecutive-rows-have-same-entries/m-p/443098#M282817</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-03-06T23:51:05Z</dc:date>
    </item>
    <item>
      <title>Re: Sum a column only when consecutive rows have same entries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-a-column-only-when-consecutive-rows-have-same-entries/m-p/443228#M282818</link>
      <description>&lt;P&gt;Thank you, works like a charm!&lt;/P&gt;</description>
      <pubDate>Wed, 07 Mar 2018 09:56:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-a-column-only-when-consecutive-rows-have-same-entries/m-p/443228#M282818</guid>
      <dc:creator>Rohit_R</dc:creator>
      <dc:date>2018-03-07T09:56:02Z</dc:date>
    </item>
    <item>
      <title>Re: Sum a column only when consecutive rows have same entries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-a-column-only-when-consecutive-rows-have-same-entries/m-p/443238#M282819</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID $ flag;
datalines;
A 0
A 1
A 1
A 0
B 1
B 0
B 0
C 1
C 1
C 1
C 0
C 1
C 1
;
proc summary data=have;
by id flag notsorted;
output out=temp;
run;
data want;
 set temp(where=(flag=1));
 by id;
 if first.id;
 keep id _freq_;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Mar 2018 10:51:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-a-column-only-when-consecutive-rows-have-same-entries/m-p/443238#M282819</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-03-07T10:51:49Z</dc:date>
    </item>
  </channel>
</rss>

