<?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: Starting a counter when value goes to zero in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580226#M164791</link>
    <description>&lt;P&gt;Same idea as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;except this resets your counter to missing if mw isn't 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;
	retain counter . ;
	mw_lag= lag(mw);
	if mw_lag &amp;gt; 0 and mw= 0 then counter = 1;
	else if mw_lag =0 and mw = 0 then counter +1;
	else call missing(counter);
	drop mw_lag;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 09 Aug 2019 18:13:08 GMT</pubDate>
    <dc:creator>noling</dc:creator>
    <dc:date>2019-08-09T18:13:08Z</dc:date>
    <item>
      <title>Starting a counter when value goes to zero</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580221#M164787</link>
      <description>&lt;P&gt;Here is an output I'm after:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 224px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/31637iC83BD51E9580F605/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Anytime the MW value changes from &amp;gt;0 to 0 I want to start a counter and reset the counter when MW &amp;gt;0, as shown above.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have achieved this with the code below.&amp;nbsp; I'm looking for a way to do this more efficiently, as there are times when I may need to count to up to 72+.&lt;/P&gt;&lt;PRE&gt;data test;&lt;BR /&gt;set data;&lt;BR /&gt;if MW = 0 and lag(MW) &amp;gt; 0 &lt;BR /&gt;then counter = 1;&lt;BR /&gt;if lag(counter)=1 and MW=0&lt;BR /&gt;then counter=2;&lt;BR /&gt;if lag(counter)=2 and MW=0&lt;BR /&gt;then counter=3;&lt;BR /&gt;if lag(counter)=3 and MW=0&lt;BR /&gt;then counter=4;&lt;BR /&gt;if lag(counter)=4 and MW=0&lt;BR /&gt;then counter=5;&lt;BR /&gt;if lag(counter)=5 and MW=0&lt;BR /&gt;then counter=6;&lt;BR /&gt;if lag(counter)=6 and MW=0&lt;BR /&gt;then counter=7;&lt;BR /&gt;if lag(counter)=7 and MW=0&lt;BR /&gt;then counter=8;&lt;BR /&gt;if lag(counter)=8 and MW=0&lt;BR /&gt;then counter=9;&lt;BR /&gt;if lag(counter)=9 and MW=0&lt;BR /&gt;then counter=10;&lt;BR /&gt;if lag(counter)=10 and MW=0&lt;BR /&gt;then counter=11;&lt;BR /&gt;run;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Aug 2019 17:59:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580221#M164787</guid>
      <dc:creator>Chad_OSU</dc:creator>
      <dc:date>2019-08-09T17:59:30Z</dc:date>
    </item>
    <item>
      <title>Re: Starting a counter when value goes to zero</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580224#M164789</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    prev_mw=lag(mw);
    if prev_mw&amp;gt;=0 and mw=0 then counter+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Aug 2019 18:09:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580224#M164789</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-08-09T18:09:03Z</dc:date>
    </item>
    <item>
      <title>Re: Starting a counter when value goes to zero</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580225#M164790</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input mw;
cards;
5
5
5
0
0
0
5
0
0
0
0
0
;

data want;
set have;
by mw notsorted;
if first.mw and mw=0  then count=1;
else if  mw=0 then count+1;
else count=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Aug 2019 18:11:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580225#M164790</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-09T18:11:59Z</dc:date>
    </item>
    <item>
      <title>Re: Starting a counter when value goes to zero</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580226#M164791</link>
      <description>&lt;P&gt;Same idea as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;except this resets your counter to missing if mw isn't 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;
	retain counter . ;
	mw_lag= lag(mw);
	if mw_lag &amp;gt; 0 and mw= 0 then counter = 1;
	else if mw_lag =0 and mw = 0 then counter +1;
	else call missing(counter);
	drop mw_lag;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Aug 2019 18:13:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580226#M164791</guid>
      <dc:creator>noling</dc:creator>
      <dc:date>2019-08-09T18:13:08Z</dc:date>
    </item>
    <item>
      <title>Re: Starting a counter when value goes to zero</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580227#M164792</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input mw;
cards;
5
5
5
0
0
0
5
0
0
0
0
0
;

data want;
if 0 then set have;/*get the variable order as-is*/
do count=1 by 1 until(last.mw);
set have;
by mw notsorted;
if mw ne 0 then count=.;
output;
end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Aug 2019 18:16:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580227#M164792</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-09T18:16:25Z</dc:date>
    </item>
    <item>
      <title>Re: Starting a counter when value goes to zero</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580230#M164793</link>
      <description>&lt;P&gt;Thank you for the responses!&amp;nbsp; How would I stop the counter based on a parameter?&amp;nbsp; For example, once it hit 10, stop counting until the condition of &amp;gt;0 to 0 happens again?&lt;/P&gt;</description>
      <pubDate>Fri, 09 Aug 2019 18:20:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580230#M164793</guid>
      <dc:creator>Chad_OSU</dc:creator>
      <dc:date>2019-08-09T18:20:35Z</dc:date>
    </item>
    <item>
      <title>Re: Starting a counter when value goes to zero</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580232#M164794</link>
      <description>&lt;P&gt;Good point,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/115150"&gt;@noling&lt;/a&gt;, I seem to have missed that.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Aug 2019 18:23:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580232#M164794</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-08-09T18:23:41Z</dc:date>
    </item>
    <item>
      <title>Re: Starting a counter when value goes to zero</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580234#M164795</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/284699"&gt;@Chad_OSU&lt;/a&gt;&amp;nbsp; Do you mean this by any chance?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input mw;
cards;
5
5
5
0
0
0
0
0
0
0
0
0
0
0
0
0
5
0
0
0
0
0
;

data want;
if 0 then set have;/*get the variable order as-is*/
retain limit 10;
do count=1 by 1 until(last.mw);
set have;
by mw notsorted;
if mw ne 0 then count=.;
if count&amp;gt;limit then count=limit;
output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Aug 2019 18:30:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580234#M164795</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-09T18:30:01Z</dc:date>
    </item>
    <item>
      <title>Re: Starting a counter when value goes to zero</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580238#M164798</link>
      <description>Yes, that should work, thanks!</description>
      <pubDate>Fri, 09 Aug 2019 18:45:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580238#M164798</guid>
      <dc:creator>Chad_OSU</dc:creator>
      <dc:date>2019-08-09T18:45:12Z</dc:date>
    </item>
    <item>
      <title>Re: Starting a counter when value goes to zero</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580241#M164799</link>
      <description>&lt;P&gt;How can I make the duplicated 10 values null without messing up the counter?&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture2.PNG" style="width: 191px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/31644iEEA0D211839177A1/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Capture2.PNG" alt="Capture2.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Aug 2019 18:56:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580241#M164799</guid>
      <dc:creator>Chad_OSU</dc:creator>
      <dc:date>2019-08-09T18:56:21Z</dc:date>
    </item>
    <item>
      <title>Re: Starting a counter when value goes to zero</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580243#M164801</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input mw;
cards;
5
5
5
0
0
0
0
0
0
0
0
0
0
0
0
0
5
0
0
0
0
0
;

data want;
retain _limit 10;
do _c=1 by 1 until(last.mw);
set have;
by mw notsorted;
count=_c;
if mw ne 0 or _c&amp;gt;_limit then count=.;
output;
end;
drop _:;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Aug 2019 19:01:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580243#M164801</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-09T19:01:29Z</dc:date>
    </item>
    <item>
      <title>Re: Starting a counter when value goes to zero</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580245#M164802</link>
      <description>Perfect, thank you!!</description>
      <pubDate>Fri, 09 Aug 2019 19:04:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580245#M164802</guid>
      <dc:creator>Chad_OSU</dc:creator>
      <dc:date>2019-08-09T19:04:44Z</dc:date>
    </item>
    <item>
      <title>Re: Starting a counter when value goes to zero</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580267#M164808</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/284699"&gt;@Chad_OSU&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;I like the way&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;thinks - and the DoW loop he uses to achieve the result. However, methinks that in this case, it can be achieved simpler because:&lt;/P&gt;
&lt;P&gt;- by the nature of the task, it doesn't require BY&lt;/P&gt;
&lt;P&gt;- there's no need to impose any artificial limit on the counter&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                 
  input mw @@ ;                             
  cards ;                                   
5 5 5 0 0 0 0 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 
;                                           
run ;                                       
                                            
data want ;                                 
  set have ;                                
  counter + mw = 0 ;                          
  if mw then counter = . ;                  
run ;                                       
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Aug 2019 20:34:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580267#M164808</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-08-09T20:34:44Z</dc:date>
    </item>
    <item>
      <title>Re: Starting a counter when value goes to zero</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580277#M164811</link>
      <description>&lt;P&gt;Any advice on how to apply this same logic with 2 different groupings?&amp;nbsp;If the group x limit was say 5, and the group y limit was say 3?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture3.PNG" style="width: 120px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/31650iC899EE48E97A8555/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Capture3.PNG" alt="Capture3.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Aug 2019 21:35:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580277#M164811</guid>
      <dc:creator>Chad_OSU</dc:creator>
      <dc:date>2019-08-09T21:35:16Z</dc:date>
    </item>
    <item>
      <title>Re: Starting a counter when value goes to zero</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580279#M164812</link>
      <description>&lt;P&gt;Thanks for the reply. In a later response I added that I do actually need to apply a limit to the counter.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Aug 2019 21:38:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580279#M164812</guid>
      <dc:creator>Chad_OSU</dc:creator>
      <dc:date>2019-08-09T21:38:23Z</dc:date>
    </item>
    <item>
      <title>Re: Starting a counter when value goes to zero</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580294#M164822</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/284699"&gt;@Chad_OSU&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;Frankly, I'm at a loss as to what you mean by "limit". If it's a specific MW threshold value, I don't see 3 in your sample input data set.&lt;/P&gt;
&lt;P&gt;Please clarify; or, better yet, show how your output data set is supposed to look like according to your logic.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Aug 2019 22:49:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580294#M164822</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-08-09T22:49:13Z</dc:date>
    </item>
    <item>
      <title>Re: Starting a counter when value goes to zero</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580302#M164826</link>
      <description>&lt;P&gt;Sorry for the confusion, by "limit" I am referring to the counter limit.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's what I have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data limits;
input group$ limit;
datalines;
x 3
y 5
run;

data have;
input group$ MW;
datalines;
x 0
x 0
x 5
x 5
x 0
x 0
x 0
x 0
x 0
x 0
x 0
y 0
y 0
y 5
y 0
y 0
y 0
y 0
y 0
y 0
y 0
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's the expected output:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="counter.PNG" style="width: 224px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/31655iA73B6336C91A1139/image-size/large?v=v2&amp;amp;px=999" role="button" title="counter.PNG" alt="counter.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Aug 2019 23:01:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580302#M164826</guid>
      <dc:creator>Chad_OSU</dc:creator>
      <dc:date>2019-08-09T23:01:20Z</dc:date>
    </item>
    <item>
      <title>Re: Starting a counter when value goes to zero</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580308#M164829</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data limits;
input group$ limit;
datalines;
x 3
y 5
;
run;

data have;
input group$ MW;
datalines;
x 0
x 0
x 5
x 5
x 0
x 0
x 0
x 0
x 0
x 0
x 0
y 0
y 0
y 5
y 0
y 0
y 0
y 0
y 0
y 0
y 0
run;

data want;
do until(last.group);
 merge have limits;
 by group;
 if mw then _f=1;
 if _f then _c=sum(_c,mw=0);
 counter=_c;
 if mw or  _c&amp;gt;limit then counter=.;
 output;
end;
drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Aug 2019 23:41:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580308#M164829</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-09T23:41:09Z</dc:date>
    </item>
    <item>
      <title>Re: Starting a counter when value goes to zero</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580318#M164835</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/284699"&gt;@Chad_OSU&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;Ahhh ... that's a different story: A waaaay better presentation of the task and expected results - but also quite different an arrangement.&lt;/P&gt;
&lt;P&gt;The simple approach I've posted before won't cut here for two reasons:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;you need to determine the size of each successive collection of records with the same (group, mv) key-value before deciding whether to mark its records as counted or missing&amp;nbsp; &amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;hence, it cannot be done in a &lt;EM&gt;purely&lt;/EM&gt; single pass through the data&amp;nbsp;&amp;nbsp;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Below, is one way of cracking this - pretty general if the limits and the driver data are stored in separate files, as you showed - which, by the way, is a sensible way to structure data processing. Note that I've added a bunch of successive 5's in HAVE, as otherwise we cannot have a proper test case accounting for all eventualities:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data limits ;                                                
  input group $ limit ;                                      
  cards ;                                                    
x 3                                                          
y 5                                                          
run ;                                                        
                                                             
data have ;                                                  
  input group $ MW ;                                         
  cards ;                                                    
x 0                                                          
x 0                                                          
x 5                                                          
x 5                                                          
x 0                                                          
x 0                                                          
x 0                                                          
x 0                                                          
x 0                                                          
x 0                                                          
x 0                                                          
y 0                                                          
y 0                                                          
y 5                                                          
y 0                                                          
y 0                                                          
y 0                                                          
y 0                                                          
y 0                                                          
y 0                                                          
y 0                                                          
y 5                                                          
y 5                                                          
y 5                                                          
y 5                                                          
y 5                                                          
y 5                                                          
y 5                                                          
run ;                                                        
                                                             
data want (drop = _:) ;                                      
  if _n_ = 1 then do ;                                       
    if 0 then set limits (keep = limit) ;                    
    dcl hash h (dataset:"limits") ;                          
    h.definekey  ("group") ;                                 
    h.definedata ("limit") ;                                 
    h.definedone () ;                                        
  end ;                                                      
  do _q = 1 by 1 until (last.mw) ;                           
    set have ;                                               
    by group mw notsorted ;                                  
  end ;                                                      
  h.find() ;                                                 
  do _n_ = 1 to _q ;                                         
    set have ;                                               
    if mw or (_q &amp;lt; limit) or (_n_ &amp;gt; limit) then counter = . ;
    else counter = _n_ ;                                     
    output ;                                                 
  end ;                                                      
run ;                                                        
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Above, a hash table (H) is used to store the correspondence between GROUP and LIMIT in memory, so that LIMIT could be easily lookup up by GROUP downstream. The first DoW-loop determines the size of each (group,mw) BY group. The second reads the same BY group and selects the proper value for COUNTER based on the specified conditions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternatively, the same can be done by using MERGE to pair the records from the driver with the appropriate LIMIT values (instead of using the hash table). However, it involves two steps since NOTSORTED cannot be coded with MERGE (or UPDATE) statements.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data havelimits ;                                            
  merge have limits ;                                        
  by group ;                                                 
run ;                                                        
                                                             
data want (drop = _:) ;                                      
  do _q = 1 by 1 until (last.mw) ;                           
    set havelimits ;                                         
    by group mw notsorted ;                                  
  end ;                                                      
  do _n_ = 1 to _q ;                                         
    set havelimits ;                                         
    if mw or (_q &amp;lt; limit) or (_n_ &amp;gt; limit) then counter = . ;
    else counter = _n_ ;                                     
    output ;                                                 
  end ;                                                      
run ;                                                        
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Sat, 10 Aug 2019 02:58:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580318#M164835</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-08-10T02:58:37Z</dc:date>
    </item>
    <item>
      <title>Re: Starting a counter when value goes to zero</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580321#M164837</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;Kudos! just what the doctor ordered.&lt;/P&gt;
&lt;P&gt;So, in fact it &lt;EM&gt;can&lt;/EM&gt; be done in a purely single pass.&lt;/P&gt;
&lt;P&gt;I retract my wrongheaded statement to the contrary.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 10 Aug 2019 03:05:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Starting-a-counter-when-value-goes-to-zero/m-p/580321#M164837</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-08-10T03:05:31Z</dc:date>
    </item>
  </channel>
</rss>

