<?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: Counting unique records based on two variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Counting-unique-records-based-on-two-variables/m-p/612180#M178559</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/139341"&gt;@kmardinian&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input subject site;
	cards;
10021 644
10021 644
10256 644
10256 644
56985 733
56985 733
45698 733
12659 733
;
run;
proc sql;
	create table want as
	select site, count(distinct subject) as count
	from have
	group by site;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 16 Dec 2019 19:34:10 GMT</pubDate>
    <dc:creator>ed_sas_member</dc:creator>
    <dc:date>2019-12-16T19:34:10Z</dc:date>
    <item>
      <title>Counting unique records based on two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-unique-records-based-on-two-variables/m-p/612176#M178556</link>
      <description>&lt;P&gt;Hi, I have a dataset where I have subject ID and site ID as two separate variables and I would like to count the unique subject IDs for each site id.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;subject&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; site&lt;/P&gt;&lt;P&gt;10021&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 644&lt;/P&gt;&lt;P&gt;10021&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 644&lt;/P&gt;&lt;P&gt;10256&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 644&lt;/P&gt;&lt;P&gt;10256&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 644&lt;/P&gt;&lt;P&gt;56985&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 733&lt;/P&gt;&lt;P&gt;56985&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 733&lt;/P&gt;&lt;P&gt;45698&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 733&lt;/P&gt;&lt;P&gt;12659&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 733&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And I would like the output dataset to look like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;site&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; count&lt;/P&gt;&lt;P&gt;644&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;/P&gt;&lt;P&gt;733&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is much appreciated! Thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 16 Dec 2019 19:29:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-unique-records-based-on-two-variables/m-p/612176#M178556</guid>
      <dc:creator>kmardinian</dc:creator>
      <dc:date>2019-12-16T19:29:11Z</dc:date>
    </item>
    <item>
      <title>Re: Counting unique records based on two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-unique-records-based-on-two-variables/m-p/612179#M178558</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select site, count(unique subject) as count
from have
group by site;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Dec 2019 19:33:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-unique-records-based-on-two-variables/m-p/612179#M178558</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-12-16T19:33:46Z</dc:date>
    </item>
    <item>
      <title>Re: Counting unique records based on two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-unique-records-based-on-two-variables/m-p/612180#M178559</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/139341"&gt;@kmardinian&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input subject site;
	cards;
10021 644
10021 644
10256 644
10256 644
56985 733
56985 733
45698 733
12659 733
;
run;
proc sql;
	create table want as
	select site, count(distinct subject) as count
	from have
	group by site;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Dec 2019 19:34:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-unique-records-based-on-two-variables/m-p/612180#M178559</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-12-16T19:34:10Z</dc:date>
    </item>
    <item>
      <title>Re: Counting unique records based on two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-unique-records-based-on-two-variables/m-p/612183#M178562</link>
      <description>A double proc freq is the other option. &lt;BR /&gt;&lt;BR /&gt;proc freq data=have noprint;&lt;BR /&gt;table site*subject / out=temp;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc freq data=temp;&lt;BR /&gt;table site / out=want;&lt;BR /&gt;run;</description>
      <pubDate>Mon, 16 Dec 2019 19:39:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-unique-records-based-on-two-variables/m-p/612183#M178562</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-12-16T19:39:01Z</dc:date>
    </item>
    <item>
      <title>Re: Counting unique records based on two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-unique-records-based-on-two-variables/m-p/612184#M178563</link>
      <description>&lt;P&gt;if the data is grouped like your sample suggests, a datastep is convenient too&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input subject $            site $;
cards;
10021             644
10021             644
10256             644
10256             644
56985             733
56985             733
45698             733
12659             733
;

data want;
set have;
by site subject notsorted;
if first.site then count=1;
else if first.subject then count+1;
if last.site;
drop subject;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Dec 2019 19:41:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-unique-records-based-on-two-variables/m-p/612184#M178563</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-12-16T19:41:24Z</dc:date>
    </item>
    <item>
      <title>Re: Counting unique records based on two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-unique-records-based-on-two-variables/m-p/612189#M178566</link>
      <description>&lt;P&gt;Some HASH teaser &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input subject $            site $;
cards;
10021             644
10256             644
10021             644
10256             644
56985             733
56985             733
45698             733
12659             733
;


data want;
if _n_=1 then do;
   dcl hash H () ;
   h.definekey  ("subject") ;
   h.definedata ("subject") ;
   h.definedone () ;
 end;
 do until(last.site);
  set have;
  by site;
  h.ref();
 end;
 count=h.num_items;
 h.clear();
 drop subject;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Dec 2019 19:53:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-unique-records-based-on-two-variables/m-p/612189#M178566</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-12-16T19:53:32Z</dc:date>
    </item>
    <item>
      <title>Re: Counting unique records based on two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-unique-records-based-on-two-variables/m-p/612193#M178570</link>
      <description>&lt;P&gt;Key indexing paint brush . This assumes subjectid is numeric or char with only digit characters&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input subject $            site $;
cards;
10021             644
10256             644
10021             644
10256             644
56985             733
56985             733
45698             733
12659             733
;

data want;
array t(-1000000:1000000)_temporary_;
do until(last.site);
  set have;
  by site;
  t(input(subject,32.))=1;
end;
count=n(of t(*));
call missing(of t(*));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Dec 2019 19:52:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-unique-records-based-on-two-variables/m-p/612193#M178570</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-12-16T19:52:03Z</dc:date>
    </item>
    <item>
      <title>Re: Counting unique records based on two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-unique-records-based-on-two-variables/m-p/612207#M178579</link>
      <description>&lt;P&gt;Thank you!! This is exactly what I needed!&lt;/P&gt;</description>
      <pubDate>Mon, 16 Dec 2019 20:39:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-unique-records-based-on-two-variables/m-p/612207#M178579</guid>
      <dc:creator>kmardinian</dc:creator>
      <dc:date>2019-12-16T20:39:35Z</dc:date>
    </item>
    <item>
      <title>Re: Counting unique records based on two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-unique-records-based-on-two-variables/m-p/612367#M178679</link>
      <description>&lt;P&gt;Reeza ,&lt;/P&gt;
&lt;P&gt;no need double, just one shot.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input subject site;
	cards;
10021 644
10021 644
10256 644
10256 644
56985 733
56985 733
45698 733
12659 733
;

ods output nlevels=want;
proc freq data=have nlevels;
by site;
table subject;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Dec 2019 12:03:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-unique-records-based-on-two-variables/m-p/612367#M178679</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-12-17T12:03:07Z</dc:date>
    </item>
  </channel>
</rss>

