<?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: SAS counter reset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-counter-reset/m-p/674931#M203281</link>
    <description>&lt;P&gt;Easily done with a retained variable that is set or incremented on conditions:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID Mon $ Flag Need;
datalines;
1 Jan-06 0 0
1 Feb-06 0 0
1 Mar-06 0 0
1 Apr-06 0 0
1 May-06 0 0
1 Jun-06 0 0
1 Jul-06 0.7 1
1 Aug-06 0 2
1 Sep-06 1 1
1 Oct-06 1.3 1
1 Nov-06 0 2
1 Dec-06 0 3
2 Feb-14 0 0
2 Mar-14 0 0
2 Apr-14 0 0
2 May-14 0 0
2 Jun-14 0.6 1
2 Jul-14 0 2
2 Aug-14 0 3
2 Sep-14 0 4
2 Oct-14 0 5
2 Nov-14 2 1
3 Aug-18 0 0
3 Sep-18 0 0
3 Oct-18 0 0
3 Nov-18 0 0
3 Dec-18 0 0
;

data want;
set have (rename=(need=want_need));
by id;
if first.id then need = 0;
if flag ne 0 then need = 1;
else if need ne 0 then need + 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The retain is done automatically because of the use of the SUM Statement (need + 1).&lt;/P&gt;</description>
    <pubDate>Thu, 06 Aug 2020 09:21:18 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-08-06T09:21:18Z</dc:date>
    <item>
      <title>SAS counter reset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-counter-reset/m-p/674924#M203276</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-SPOILER&gt;&amp;nbsp;&lt;/LI-SPOILER&gt;&lt;P&gt;Hi SAS Community.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset with three variables and I am trying to create the fourth one 'need'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;'Need' is months since Flag is last&amp;gt;0 resetting to 1 once flag&amp;gt;0. I hope I explained this correctly, but essentially you can see how the variable should be created from the datastep below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The dataset is already sorted by ID and Date. How can I code this efficiently?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Datastep is below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input ID Mon $ Flag Need;&lt;BR /&gt;datalines;&lt;BR /&gt;1 Jan-06 0 0&lt;BR /&gt;1 Feb-06 0 0&lt;BR /&gt;1 Mar-06 0 0&lt;BR /&gt;1 Apr-06 0 0&lt;BR /&gt;1 May-06 0 0&lt;BR /&gt;1 Jun-06 0 0&lt;BR /&gt;1 Jul-06 0.7 1&lt;BR /&gt;1 Aug-06 0 2&lt;BR /&gt;1 Sep-06 1 1&lt;BR /&gt;1 Oct-06 1.3 1&lt;BR /&gt;1 Nov-06 0 2&lt;BR /&gt;1 Dec-06 0 3&lt;BR /&gt;2 Feb-14 0 0&lt;BR /&gt;2 Mar-14 0 0&lt;BR /&gt;2 Apr-14 0 0&lt;BR /&gt;2 May-14 0 0&lt;BR /&gt;2 Jun-14 0.6 1&lt;BR /&gt;2 Jul-14 0 2&lt;BR /&gt;2 Aug-14 0 3&lt;BR /&gt;2 Sep-14 0 4&lt;BR /&gt;2 Oct-14 0 5&lt;BR /&gt;2 Nov-14 2 1&lt;BR /&gt;3 Aug-18 0 0&lt;BR /&gt;3 Sep-18 0 0&lt;BR /&gt;3 Oct-18 0 0&lt;BR /&gt;3 Nov-18 0 0&lt;BR /&gt;3 Dec-18 0 0&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Aug 2020 08:53:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-counter-reset/m-p/674924#M203276</guid>
      <dc:creator>PetePatel</dc:creator>
      <dc:date>2020-08-06T08:53:23Z</dc:date>
    </item>
    <item>
      <title>Re: SAS counter reset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-counter-reset/m-p/674931#M203281</link>
      <description>&lt;P&gt;Easily done with a retained variable that is set or incremented on conditions:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID Mon $ Flag Need;
datalines;
1 Jan-06 0 0
1 Feb-06 0 0
1 Mar-06 0 0
1 Apr-06 0 0
1 May-06 0 0
1 Jun-06 0 0
1 Jul-06 0.7 1
1 Aug-06 0 2
1 Sep-06 1 1
1 Oct-06 1.3 1
1 Nov-06 0 2
1 Dec-06 0 3
2 Feb-14 0 0
2 Mar-14 0 0
2 Apr-14 0 0
2 May-14 0 0
2 Jun-14 0.6 1
2 Jul-14 0 2
2 Aug-14 0 3
2 Sep-14 0 4
2 Oct-14 0 5
2 Nov-14 2 1
3 Aug-18 0 0
3 Sep-18 0 0
3 Oct-18 0 0
3 Nov-18 0 0
3 Dec-18 0 0
;

data want;
set have (rename=(need=want_need));
by id;
if first.id then need = 0;
if flag ne 0 then need = 1;
else if need ne 0 then need + 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The retain is done automatically because of the use of the SUM Statement (need + 1).&lt;/P&gt;</description>
      <pubDate>Thu, 06 Aug 2020 09:21:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-counter-reset/m-p/674931#M203281</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-08-06T09:21:18Z</dc:date>
    </item>
  </channel>
</rss>

