<?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: Count the records with reset flag in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-the-records-with-reset-flag/m-p/908731#M358562</link>
    <description>&lt;P&gt;If I understand correctly, here's one way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   by col1;
   col4 + 1;
   if first.col1 or col3=1 then col4=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As a newbie, learning about the effects of a BY statement in a DATA step is an important topic.&amp;nbsp; There are many online resources available.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 18 Dec 2023 22:25:21 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2023-12-18T22:25:21Z</dc:date>
    <item>
      <title>Count the records with reset flag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-records-with-reset-flag/m-p/908728#M358561</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have the following dataset,&lt;/P&gt;&lt;P&gt;Col1/Col2/Co3:&lt;/P&gt;&lt;P&gt;A/1/0;&lt;/P&gt;&lt;P&gt;A/3/1;&lt;/P&gt;&lt;P&gt;A/5/0;&lt;/P&gt;&lt;P&gt;A/8/0;&lt;/P&gt;&lt;P&gt;B/1/0;&lt;/P&gt;&lt;P&gt;B/2/1;&lt;/P&gt;&lt;P&gt;B/3/0;&lt;/P&gt;&lt;P&gt;B/4/1;&lt;/P&gt;&lt;P&gt;B/5/0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Col1/2/3 are strings, numeric and boolean(1 for true and 0 for false).&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data are ordered by Col1 and 2.&lt;/P&gt;&lt;P&gt;Now I would like to add an extra column (Col4), beginning with 1, based on the following rules:&lt;/P&gt;&lt;P&gt;If Col3=1 then Col4=1 else Col4 is set to the Col3 value of the previous Col2 for the same Col1 plus+1.&lt;/P&gt;&lt;P&gt;Hence, Col4 is a counter and Col3 serves as an indicator to reset the counter.&lt;/P&gt;&lt;P&gt;The new dataset looks like this:&lt;/P&gt;&lt;P&gt;Col1/Col2/Col3/Col4:&lt;/P&gt;&lt;P&gt;A/1/0/1;&lt;/P&gt;&lt;P&gt;A/3/1/1;&lt;/P&gt;&lt;P&gt;A/5/0/2;&lt;/P&gt;&lt;P&gt;A/8/0/3;&lt;/P&gt;&lt;P&gt;B/1/0/1;&lt;/P&gt;&lt;P&gt;B/2/1/1;&lt;/P&gt;&lt;P&gt;B/3/0/2;&lt;/P&gt;&lt;P&gt;B/4/1/1;&lt;/P&gt;&lt;P&gt;B/5/0/2;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am newbie to SAS so appreciate if anyone can help me out wth this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thank in advance.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Dec 2023 21:52:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-records-with-reset-flag/m-p/908728#M358561</guid>
      <dc:creator>fwu811</dc:creator>
      <dc:date>2023-12-18T21:52:07Z</dc:date>
    </item>
    <item>
      <title>Re: Count the records with reset flag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-records-with-reset-flag/m-p/908731#M358562</link>
      <description>&lt;P&gt;If I understand correctly, here's one way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   by col1;
   col4 + 1;
   if first.col1 or col3=1 then col4=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As a newbie, learning about the effects of a BY statement in a DATA step is an important topic.&amp;nbsp; There are many online resources available.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Dec 2023 22:25:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-records-with-reset-flag/m-p/908731#M358562</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-12-18T22:25:21Z</dc:date>
    </item>
    <item>
      <title>Re: Count the records with reset flag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-records-with-reset-flag/m-p/908733#M358563</link>
      <description>&lt;PRE&gt;The new dataset looks like this:
Col1/Col2/Col3/Col4:
A/1/0/1;
A/3/1/1;
A/5/0/2;
A/8/0/3;&lt;/PRE&gt;
&lt;P&gt;Does not match your description. You state "If Col3=1 then Col4=1 else&amp;nbsp;Col4 is set to the Col3 value of the previous Col2". There is no "previous Col2 for the first observation. So where does the 1 for Col4 come from?&lt;/P&gt;
&lt;P&gt;Same for the first Col1 = B. There is no previous Col1=B so where does that 1 come from?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please provide data in the form of working data step code pasted into a text box opened with the &amp;lt;/&amp;gt; icon such as:&lt;/P&gt;
&lt;PRE&gt;data have;
   input Col1 $ Col2 Col3;
datalines;
A 1 0
A 3 1
A 5 0
A 8 0
B 1 0
B 2 1
B 3 0
B 4 1
B 5 0
;&lt;/PRE&gt;
&lt;P&gt;This seems to do what you show for the given example:&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   by col1;
   retain col4;
   if first.col1 then col4=1;
   else col4+1;
   if col3=1 then col4=1;
run;
&lt;/PRE&gt;
&lt;P&gt;The By statement creates automatic variables that are not added to the data set that indicate whether an observation is the first or last of the by group. These values are boolean. So we can reset based on the First of a group.&lt;/P&gt;
&lt;P&gt;Retain establishes a new variable to keep values across the data step boundary so is the most common tool for counting like this.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Dec 2023 22:28:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-records-with-reset-flag/m-p/908733#M358563</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-12-18T22:28:48Z</dc:date>
    </item>
    <item>
      <title>Re: Count the records with reset flag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-records-with-reset-flag/m-p/908775#M358572</link>
      <description>Hi Both,&lt;BR /&gt;&lt;BR /&gt;Many thanks for your quick response. Col4 simply counts the occurrence of values in Col1 , but will recount when Col3 is flagged.&lt;BR /&gt;I will study the BY statement, which is perhaps unique to SAS, and try to figure out myself using your codes provided.</description>
      <pubDate>Tue, 19 Dec 2023 09:27:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-records-with-reset-flag/m-p/908775#M358572</guid>
      <dc:creator>fwu811</dc:creator>
      <dc:date>2023-12-19T09:27:03Z</dc:date>
    </item>
  </channel>
</rss>

