<?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 Obtaining Distinct Count by groups in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Obtaining-Distinct-Count-by-groups/m-p/748747#M235176</link>
    <description>&lt;P&gt;Hello, I'm struggling to do an incremental count by distinct ID's. Below is an example of my expectation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basically, I want to know how many distinct ID's there are by customer (in order of date) between any results of -4. So after that -4 result we begin counting again.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have;
input NAME $ DATE ID RESULT;
format DATE yymmdd10.;
cards;
Joe 20190101 1 500
Joe 20190102 1 500
Joe 20190103 1 500
Joe 20190104 1  -4
Tom 20190104 1 500
Tom 20190101 1  -4
Tom 20190102 1 500
Tom 20190103 2 500
Tom 20190104 3 500
Tom 20190104 7  -4
;
run;

data want;
input NAME $ DATE ID RESULT want;
format DATE yymmdd10.;
cards;
Joe 20190101 1 500 1
Joe 20190102 1 500 1
Joe 20190103 1 500 1
Joe 20190104 1  -4 1
Tom 20190104 1 500 1
Tom 20190101 1  -4 1
Tom 20190102 1 500 1
Tom 20190103 2 500 2
Tom 20190104 3 500 3
Tom 20190104 7  -4 4
;
run;&lt;/PRE&gt;</description>
    <pubDate>Thu, 17 Jun 2021 19:37:30 GMT</pubDate>
    <dc:creator>Krueger1</dc:creator>
    <dc:date>2021-06-17T19:37:30Z</dc:date>
    <item>
      <title>Obtaining Distinct Count by groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtaining-Distinct-Count-by-groups/m-p/748747#M235176</link>
      <description>&lt;P&gt;Hello, I'm struggling to do an incremental count by distinct ID's. Below is an example of my expectation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basically, I want to know how many distinct ID's there are by customer (in order of date) between any results of -4. So after that -4 result we begin counting again.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have;
input NAME $ DATE ID RESULT;
format DATE yymmdd10.;
cards;
Joe 20190101 1 500
Joe 20190102 1 500
Joe 20190103 1 500
Joe 20190104 1  -4
Tom 20190104 1 500
Tom 20190101 1  -4
Tom 20190102 1 500
Tom 20190103 2 500
Tom 20190104 3 500
Tom 20190104 7  -4
;
run;

data want;
input NAME $ DATE ID RESULT want;
format DATE yymmdd10.;
cards;
Joe 20190101 1 500 1
Joe 20190102 1 500 1
Joe 20190103 1 500 1
Joe 20190104 1  -4 1
Tom 20190104 1 500 1
Tom 20190101 1  -4 1
Tom 20190102 1 500 1
Tom 20190103 2 500 2
Tom 20190104 3 500 3
Tom 20190104 7  -4 4
;
run;&lt;/PRE&gt;</description>
      <pubDate>Thu, 17 Jun 2021 19:37:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtaining-Distinct-Count-by-groups/m-p/748747#M235176</guid>
      <dc:creator>Krueger1</dc:creator>
      <dc:date>2021-06-17T19:37:30Z</dc:date>
    </item>
    <item>
      <title>Re: Obtaining Distinct Count by groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtaining-Distinct-Count-by-groups/m-p/748753#M235179</link>
      <description>&lt;P&gt;See if this works:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by name id /* notsorted if id is not ascending */;
retain want;
if first.name or lag(result) = -4 then want = 1;
else if first.id then want + 1;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested, posted from my tablet.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Jun 2021 19:56:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtaining-Distinct-Count-by-groups/m-p/748753#M235179</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-06-17T19:56:27Z</dc:date>
    </item>
    <item>
      <title>Re: Obtaining Distinct Count by groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtaining-Distinct-Count-by-groups/m-p/748773#M235187</link>
      <description>&lt;P&gt;This appears to work, still need to test in the larger scheme of things. I was missing the else if in my attempts!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks so much!&lt;/P&gt;</description>
      <pubDate>Thu, 17 Jun 2021 20:33:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtaining-Distinct-Count-by-groups/m-p/748773#M235187</guid>
      <dc:creator>Krueger1</dc:creator>
      <dc:date>2021-06-17T20:33:28Z</dc:date>
    </item>
    <item>
      <title>Re: Obtaining Distinct Count by groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtaining-Distinct-Count-by-groups/m-p/748972#M235288</link>
      <description>&lt;P&gt;Thank you for the response, so this was SUPER close however it's not catching DISTINCT ID's. Below is a better example of this. Currently, if ID 1 appears, then 2 then 1 again it'll increment to 3 when it should only be 2.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have;
input NAME $ Flag DATE ID RESULT;
format DATE yymmdd10.;
cards;
Joe 0 20190101 1 500
Joe 0 20190102 1 500
Joe 0 20190103 1 500
Joe 0 20190104 1  -4
Joe 1 20190121 1 500
Joe 0 20190122 1 500
Tom 0 20190104 1 500
Tom 0 20190101 1  -4
Tom 0 20190102 1 500
Tom 0 20190103 2 500
Tom 0 20190104 3 500
Tom 0 20190105 1 500
Tom 0 20190104 7  -4
;
run;

data want;
input NAME $ DATE ID RESULT want;
format DATE yymmdd10.;
cards;
Joe 0 20190101 1 500 1
Joe 0 20190102 1 500 1
Joe 0 20190103 1 500 1
Joe 0 20190104 1  -4 1
Joe 1 20190121 1 500 1
Joe 0 20190122 1 500 1
Tom 0 20190104 1 500 1
Tom 0 20190101 1  -4 1
Tom 0 20190102 1 500 1
Tom 0 20190103 2 500 2
Tom 0 20190104 3 500 3
Tom 0 20190105 1 500 3
Tom 0 20190106 2 500 3
Tom 0 20190104 7  -4 1
;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Jun 2021 18:24:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtaining-Distinct-Count-by-groups/m-p/748972#M235288</guid>
      <dc:creator>Krueger1</dc:creator>
      <dc:date>2021-06-18T18:24:59Z</dc:date>
    </item>
    <item>
      <title>Re: Obtaining Distinct Count by groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtaining-Distinct-Count-by-groups/m-p/749627#M235610</link>
      <description>&lt;P&gt;[A new thread has been started to discuss this follow-up question: &lt;A href="https://communities.sas.com/t5/SAS-Programming/Increment-on-out-of-order-but-distinct-values/m-p/749624" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Programming/Increment-on-out-of-order-but-distinct-values/m-p/749624&lt;/A&gt;]&lt;/P&gt;</description>
      <pubDate>Tue, 22 Jun 2021 16:42:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtaining-Distinct-Count-by-groups/m-p/749627#M235610</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-06-22T16:42:43Z</dc:date>
    </item>
  </channel>
</rss>

