<?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: Total count of a variable based on another variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Total-count-of-a-variable-based-on-another-variable/m-p/789848#M252806</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select ID, count(distinct group) as Number_Groups
from have
group by ID
union 
select "Total" as ID, count(distinct group) as Number_Groups
from have;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If ID was character something like this would work&lt;/P&gt;</description>
    <pubDate>Wed, 12 Jan 2022 22:31:51 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2022-01-12T22:31:51Z</dc:date>
    <item>
      <title>Total count of a variable based on another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-count-of-a-variable-based-on-another-variable/m-p/789839#M252799</link>
      <description>&lt;P&gt;Hi,&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;I have a dataset. It looks something like this:&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;
input ID group $;
datalines;
123 Marketing
123 HR&lt;BR /&gt;123 HR
789 Marketing
789 Council
123 Marketing&lt;BR /&gt;555 HR
123 Artist
555 Director&lt;BR /&gt;789 Artist&lt;BR /&gt;789 Doctor
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want the output to give this:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Number of groups ID belongs in&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;555&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;789&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note that for a given ID, they may have duplicate Group values. ID 123 has "Marketing" and "HR" twice, but they only contribute 1 to the "number" total. Can someone show how this can be generated?&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jan 2022 22:14:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-count-of-a-variable-based-on-another-variable/m-p/789839#M252799</guid>
      <dc:creator>yellowyellowred</dc:creator>
      <dc:date>2022-01-12T22:14:05Z</dc:date>
    </item>
    <item>
      <title>Re: Total count of a variable based on another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-count-of-a-variable-based-on-another-variable/m-p/789841#M252801</link>
      <description>&lt;P&gt;SQL is the easiest here:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select ID, count(distinct group) as Number_Groups
from have
group by ID;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could also use a double proc freq or data step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/count_distinct_by_group.sas" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/count_distinct_by_group.sas&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/412505"&gt;@yellowyellowred&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;I have a dataset. It looks something like this:&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;data have;
input ID group $;
datalines;
123 Marketing
123 HR&lt;BR /&gt;123 HR
789 Marketing
789 Council
123 Marketing&lt;BR /&gt;555 HR
123 Artist
555 Director&lt;BR /&gt;789 Artist&lt;BR /&gt;789 Doctor
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want the output to give this:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;TABLE border="1"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;ID&lt;/TD&gt;
&lt;TD&gt;Number of groups ID belongs in&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;123&lt;/TD&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;555&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;789&lt;/TD&gt;
&lt;TD&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that for a given ID, they may have duplicate Group values. ID 123 has "Marketing" and "HR" twice, but they only contribute 1 to the "number" total. Can someone show how this can be generated?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jan 2022 22:17:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-count-of-a-variable-based-on-another-variable/m-p/789841#M252801</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-01-12T22:17:37Z</dc:date>
    </item>
    <item>
      <title>Re: Total count of a variable based on another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-count-of-a-variable-based-on-another-variable/m-p/789844#M252802</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;thanks. how would I also add one to show the total number of distinct groups? In this case, it is 6. Edit: never mind I figured it out. It's just&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sql;
create table total_groups as
select count(distinct RgstrtnGrpNm) as total_count
from have
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Jan 2022 22:28:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-count-of-a-variable-based-on-another-variable/m-p/789844#M252802</guid>
      <dc:creator>yellowyellowred</dc:creator>
      <dc:date>2022-01-12T22:28:12Z</dc:date>
    </item>
    <item>
      <title>Re: Total count of a variable based on another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-count-of-a-variable-based-on-another-variable/m-p/789848#M252806</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select ID, count(distinct group) as Number_Groups
from have
group by ID
union 
select "Total" as ID, count(distinct group) as Number_Groups
from have;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If ID was character something like this would work&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jan 2022 22:31:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-count-of-a-variable-based-on-another-variable/m-p/789848#M252806</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-01-12T22:31:51Z</dc:date>
    </item>
  </channel>
</rss>

