<?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: Counter field that can interrupt and resume count based on a condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Counter-field-that-can-interrupt-and-resume-count-based-on-a/m-p/700060#M214206</link>
    <description>&lt;P&gt;Store the count in a retained variable (_n below).&amp;nbsp; If the observation in hand is a success, assign that value to COUNTER, otherwise set COUNTER to 0:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1 (drop=_n:);
  set masterdata;
  by acctno;
  if first.acctno then _n=0;
  _n+result='Success';
  counter=ifn(result='Success',_n,0);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 19 Nov 2020 00:14:23 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2020-11-19T00:14:23Z</dc:date>
    <item>
      <title>Counter field that can interrupt and resume count based on a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counter-field-that-can-interrupt-and-resume-count-based-on-a/m-p/700017#M214182</link>
      <description>&lt;P&gt;In my code, I have created a counter field that tracks the number of iterations for an event within a given account. Now I am trying to create a second counter field that can stop and restart the iteration count if a given event fails to occur in a given observation.&amp;nbsp; Let me spell out the data set and the code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;AcctNo &amp;nbsp;&amp;nbsp; Date &amp;nbsp;&amp;nbsp; Result &amp;nbsp;&amp;nbsp; Counter&lt;/P&gt;
&lt;P&gt;2111 &amp;nbsp; &amp;nbsp;&amp;nbsp; 5/7/20 &amp;nbsp;&amp;nbsp; Success &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: Arial,Helvetica,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 27.42px; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;2111 &amp;nbsp; &amp;nbsp;&amp;nbsp; 5/14/20&amp;nbsp; Success &amp;nbsp; 2&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: Arial,Helvetica,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 27.42px; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;2111 &amp;nbsp; &amp;nbsp;&amp;nbsp; 5/21/20&amp;nbsp; Success &amp;nbsp; 3&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: Arial,Helvetica,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 27.42px; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;2111 &amp;nbsp; &amp;nbsp;&amp;nbsp; 5/28/20&amp;nbsp; Success &amp;nbsp; 4&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: Arial,Helvetica,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 27.42px; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;2111 &amp;nbsp; &amp;nbsp;&amp;nbsp; 6/4/20 &amp;nbsp;&amp;nbsp; Failure &amp;nbsp; &amp;nbsp;&amp;nbsp; 0&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: Arial,Helvetica,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 27.42px; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;2111 &amp;nbsp; &amp;nbsp;&amp;nbsp; 6/11/20&amp;nbsp; Success &amp;nbsp; 5&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="background-color: transparent; box-sizing: border-box; color: #333333; font-family: Arial,Helvetica,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 27.42px; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px; margin: 0px;"&gt;2112 &amp;nbsp; &amp;nbsp;&amp;nbsp; 5/7/20 &amp;nbsp;&amp;nbsp; Success &amp;nbsp; 1&lt;/P&gt;
&lt;P style="background-color: transparent; box-sizing: border-box; color: #333333; font-family: Arial,Helvetica,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 27.42px; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px; margin: 0px;"&gt;&lt;SPAN style="background-color: transparent; box-sizing: border-box; color: #333333; cursor: text; display: inline; float: none; font-family: Arial,Helvetica,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 27.42px; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;2112 &amp;nbsp; &amp;nbsp;&amp;nbsp; 5/14/20&amp;nbsp; Failure &amp;nbsp; &amp;nbsp;&amp;nbsp; 0&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="background-color: transparent; box-sizing: border-box; color: #333333; font-family: Arial,Helvetica,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 27.42px; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px; margin: 0px;"&gt;&lt;SPAN style="background-color: transparent; box-sizing: border-box; color: #333333; cursor: text; display: inline; float: none; font-family: Arial,Helvetica,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 27.42px; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;2112 &amp;nbsp; &amp;nbsp;&amp;nbsp; 5/21/20&amp;nbsp; Success &amp;nbsp; 2&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: Arial,Helvetica,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 27.42px; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;When the value "Failure" is in the result field, I want the counter to stop and the field filled with a zero value.&amp;nbsp; If the next observation shows "Success" in the result, the counter should resume its count where it left off before the "Failure" instance.&amp;nbsp; The process should repeat itself for each account number value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I wrote the following code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test1;&lt;/P&gt;
&lt;P&gt;set masterdata:&lt;/P&gt;
&lt;P&gt;by AcctNo;&lt;/P&gt;
&lt;P&gt;if first.AcctNo&lt;/P&gt;
&lt;P&gt;then event_int = 1;&lt;/P&gt;
&lt;P&gt;retain event_int;&lt;/P&gt;
&lt;P&gt;if Result = 'Failure' then event_int = 0&lt;BR /&gt;else event_int + 1;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unfortunately, it is not storing the previous counter value before the interruption so when I resume the counter, it starts back at 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any feedback would be greatly appreciated.&amp;nbsp; Thanks.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Nov 2020 22:28:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counter-field-that-can-interrupt-and-resume-count-based-on-a/m-p/700017#M214182</guid>
      <dc:creator>greg6363</dc:creator>
      <dc:date>2020-11-18T22:28:04Z</dc:date>
    </item>
    <item>
      <title>Re: Counter field that can interrupt and resume count based on a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counter-field-that-can-interrupt-and-resume-count-based-on-a/m-p/700023#M214188</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21164"&gt;@greg6363&lt;/a&gt;&amp;nbsp;-&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
 input (AcctNo    Date    Result) ($);*    Counter;
 cards;
2111      5/7/20    Success   1

2111      5/14/20  Success   2

2111      5/21/20  Success   3

2111      5/28/20  Success   4

2111      6/4/20    Failure      0

2111      6/11/20  Success   5

2112      5/7/20    Success   1

2112      5/14/20  Failure      0

2112      5/21/20  Success   2
;

data want;
 if _n_=1 then do;
  dcl hash h(suminc:'_iorc_');
  h.definekey('Result');
  h.definedone();
 end;
 retain _iorc_ 1;
 do until(last.acctno);
  set have;
  by acctno;
  h.ref();
  h.sum(sum:counter);
  if Result='Failure' then counter=0;
  output;
 end;
 h.clear();
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 18 Nov 2020 22:38:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counter-field-that-can-interrupt-and-resume-count-based-on-a/m-p/700023#M214188</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-11-18T22:38:19Z</dc:date>
    </item>
    <item>
      <title>Re: Counter field that can interrupt and resume count based on a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counter-field-that-can-interrupt-and-resume-count-based-on-a/m-p/700060#M214206</link>
      <description>&lt;P&gt;Store the count in a retained variable (_n below).&amp;nbsp; If the observation in hand is a success, assign that value to COUNTER, otherwise set COUNTER to 0:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1 (drop=_n:);
  set masterdata;
  by acctno;
  if first.acctno then _n=0;
  _n+result='Success';
  counter=ifn(result='Success',_n,0);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Nov 2020 00:14:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counter-field-that-can-interrupt-and-resume-count-based-on-a/m-p/700060#M214206</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-11-19T00:14:23Z</dc:date>
    </item>
    <item>
      <title>Re: Counter field that can interrupt and resume count based on a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counter-field-that-can-interrupt-and-resume-count-based-on-a/m-p/700101#M214226</link>
      <description>The code works to a T.  All set.  Thanks.</description>
      <pubDate>Thu, 19 Nov 2020 05:42:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counter-field-that-can-interrupt-and-resume-count-based-on-a/m-p/700101#M214226</guid>
      <dc:creator>greg6363</dc:creator>
      <dc:date>2020-11-19T05:42:14Z</dc:date>
    </item>
  </channel>
</rss>

