<?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 Id once in a group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-Id-once-in-a-group/m-p/836869#M330892</link>
    <description>&lt;P&gt;So should Item_id ABC12 and ABC13 even be in the output since they do not have "a distinct" id? Or should the Id be blank/missing?&lt;/P&gt;</description>
    <pubDate>Wed, 05 Oct 2022 04:47:01 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2022-10-05T04:47:01Z</dc:date>
    <item>
      <title>Count Id once in a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-Id-once-in-a-group/m-p/836846#M330874</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I have a dataset as below;&lt;BR /&gt;data a;&lt;BR /&gt;input ID $ Item_id $5.;&lt;BR /&gt;datalines;&lt;BR /&gt;a1 ABC10&lt;BR /&gt;a2 ABC11&lt;BR /&gt;a2 ABC12&lt;BR /&gt;a2 ABC13&lt;BR /&gt;b1 ABC11&lt;BR /&gt;b2 ABC11&lt;BR /&gt;c1 ABC10&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;Uisng the link below , i can count distinct ID's for each item using below code:&lt;BR /&gt;proc sql;&lt;BR /&gt;select item_id,&lt;BR /&gt;count(distinct ID) as Distinct_Id from a&lt;BR /&gt;group by Item_id;&lt;BR /&gt;quit;&lt;BR /&gt;After seeing the output i am noticing in last two rows that&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ABC12 and ABC13 have 1 disctinct id.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, this ID is same (a2) , therefore , if it has been included for first item count than it should not be&amp;nbsp;&lt;/P&gt;
&lt;P&gt;counted in the second item group.&lt;/P&gt;
&lt;P&gt;I would appreciate any suggestion.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Oct 2022 02:42:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-Id-once-in-a-group/m-p/836846#M330874</guid>
      <dc:creator>sascode</dc:creator>
      <dc:date>2022-10-05T02:42:05Z</dc:date>
    </item>
    <item>
      <title>Re: Count Id once in a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-Id-once-in-a-group/m-p/836869#M330892</link>
      <description>&lt;P&gt;So should Item_id ABC12 and ABC13 even be in the output since they do not have "a distinct" id? Or should the Id be blank/missing?&lt;/P&gt;</description>
      <pubDate>Wed, 05 Oct 2022 04:47:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-Id-once-in-a-group/m-p/836869#M330892</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-10-05T04:47:01Z</dc:date>
    </item>
    <item>
      <title>Re: Count Id once in a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-Id-once-in-a-group/m-p/836871#M330893</link>
      <description>&lt;P&gt;a2 already exists for item_id ABC11.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1664945567054.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/75894iD4B006727DAA16E4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1664945567054.png" alt="Patrick_0-1664945567054.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;So what exactly is your rule for counting distinct IDs? Only count for their first appearance in any group? And if so what's the sort order for the groups (or you don't care)?&lt;/P&gt;</description>
      <pubDate>Wed, 05 Oct 2022 04:55:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-Id-once-in-a-group/m-p/836871#M330893</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-10-05T04:55:06Z</dc:date>
    </item>
    <item>
      <title>Re: Count Id once in a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-Id-once-in-a-group/m-p/836916#M330917</link>
      <description>yes, it is fine if it show 0 .&lt;BR /&gt;Thank you.</description>
      <pubDate>Wed, 05 Oct 2022 11:33:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-Id-once-in-a-group/m-p/836916#M330917</guid>
      <dc:creator>sascode</dc:creator>
      <dc:date>2022-10-05T11:33:06Z</dc:date>
    </item>
    <item>
      <title>Re: Count Id once in a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-Id-once-in-a-group/m-p/836917#M330918</link>
      <description>Hi,&lt;BR /&gt;Yes, if an id is counted once in a given group , it should not counted again in the next group .&lt;BR /&gt;Sorting is not a problem</description>
      <pubDate>Wed, 05 Oct 2022 11:34:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-Id-once-in-a-group/m-p/836917#M330918</guid>
      <dc:creator>sascode</dc:creator>
      <dc:date>2022-10-05T11:34:34Z</dc:date>
    </item>
    <item>
      <title>Re: Count Id once in a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-Id-once-in-a-group/m-p/836955#M330937</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/215702"&gt;@sascode&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;yes, it is fine if it show 0 .&lt;BR /&gt;Thank you.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then maybe something like:&lt;/P&gt;
&lt;PRE&gt;proc sort data=a;
  by id;
run;

data need;
   set a;
   by id;
   if first.id then do;
   end;
   else call missing(id);
run;

proc sql;
select item_id,
count(distinct ID) as Distinct_Id from need
group by Item_id;
quit;&lt;/PRE&gt;
&lt;P&gt;The sort groups all of the ID values together and then the data step sets all but one to missing. That way there is no "duplicate" Id to count.&lt;/P&gt;
&lt;P&gt;Caution: this makes no attempt to control which "item_id" gets the Id. You might sort by Id and Item_id, then the first Item_id by alphabetic value would be associated with the ID. More complex rules of which Item_id should have the Id may be a bit difficult to enforce without a lot of work.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Oct 2022 14:27:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-Id-once-in-a-group/m-p/836955#M330937</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-10-05T14:27:24Z</dc:date>
    </item>
  </channel>
</rss>

