<?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: Unique prescriber count in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Unique-prescriber-count/m-p/287906#M59285</link>
    <description>SQL: Select count(distinct pres_id)</description>
    <pubDate>Thu, 28 Jul 2016 18:31:12 GMT</pubDate>
    <dc:creator>LinusH</dc:creator>
    <dc:date>2016-07-28T18:31:12Z</dc:date>
    <item>
      <title>Unique prescriber count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unique-prescriber-count/m-p/287896#M59283</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i have following data, where i need to count unique prescribers for each member. A prescriber could have been associated with multiple facilites. If a prescriber counted already for a member and there exists a different prescriber for the member from same facility it should still be counted as 1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Mem_id&lt;/TD&gt;&lt;TD&gt;pres_id&lt;/TD&gt;&lt;TD&gt;fac_id&lt;/TD&gt;&lt;TD&gt;remarks&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;M001&lt;/TD&gt;&lt;TD&gt;P001&lt;/TD&gt;&lt;TD&gt;F001&lt;/TD&gt;&lt;TD&gt;Add 1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;M001&lt;/TD&gt;&lt;TD&gt;P001&lt;/TD&gt;&lt;TD&gt;F002&lt;/TD&gt;&lt;TD&gt;Add 0 P001 is already counted above&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;M001&lt;/TD&gt;&lt;TD&gt;P002&lt;/TD&gt;&lt;TD&gt;F003&lt;/TD&gt;&lt;TD&gt;Add 1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;M001&lt;/TD&gt;&lt;TD&gt;P003&lt;/TD&gt;&lt;TD&gt;F003&lt;/TD&gt;&lt;TD&gt;Add 0 P003 is from same facility of P002&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;M001&lt;/TD&gt;&lt;TD&gt;P003&lt;/TD&gt;&lt;TD&gt;F004&lt;/TD&gt;&lt;TD&gt;Add 0 P003 is already counted above&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;M001&lt;/TD&gt;&lt;TD&gt;P003&lt;/TD&gt;&lt;TD&gt;F005&lt;/TD&gt;&lt;TD&gt;Add 0 P003 is already counted above&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;M001&lt;/TD&gt;&lt;TD&gt;P004&lt;/TD&gt;&lt;TD&gt;F002&lt;/TD&gt;&lt;TD&gt;Add 0 P004 is from same facility&amp;nbsp; of P002. Final count is 2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;M002&lt;/TD&gt;&lt;TD&gt;P002&lt;/TD&gt;&lt;TD&gt;F002&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;add 1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;M002&lt;/TD&gt;&lt;TD&gt;P002&lt;/TD&gt;&lt;TD&gt;F003&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;add 0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;M002&lt;/TD&gt;&lt;TD&gt;P003&lt;/TD&gt;&lt;TD&gt;F002&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;add 0. final count 1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Desired output&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Mem_ID&lt;/TD&gt;&lt;TD&gt;Unique prescriber count&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;M001&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;M002&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Thu, 28 Jul 2016 18:12:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unique-prescriber-count/m-p/287896#M59283</guid>
      <dc:creator>sasuser77</dc:creator>
      <dc:date>2016-07-28T18:12:14Z</dc:date>
    </item>
    <item>
      <title>Re: Unique prescriber count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unique-prescriber-count/m-p/287906#M59285</link>
      <description>SQL: Select count(distinct pres_id)</description>
      <pubDate>Thu, 28 Jul 2016 18:31:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unique-prescriber-count/m-p/287906#M59285</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-07-28T18:31:12Z</dc:date>
    </item>
    <item>
      <title>Re: Unique prescriber count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unique-prescriber-count/m-p/287936#M59296</link>
      <description>&lt;P&gt;The simplest way I can think of is the Hash, dumping everything into Hash object, then check and count:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input 
		Mem_id$ pres_id$ fac_id$;
	cards;
M001 P001 F001 
M001 P001 F002 
M001 P002 F003 
M001 P003 F003 
M001 P003 F004 
M001 P003 F005 
M001 P004 F002 
M002 P002 F002 
M002 P002 F003 
M002 P003 F002 
;

data want;

	if _n_=1 then
		do;
			dcl hash h();
			h.definekey('pool');

			h.definedone();
		end;
	length pool $ 10;

	set have;
	by mem_id notsorted;

	if first.mem_id then
		do;
			call missing (count);
			rc=h.clear();
		end;

	if h.check(key:pres_id) ne 0 and h.check(key:fac_id) ne 0 then
		count+1;
	pool=pres_id;
	rc=h.add();
	pool=fac_id;
	rc=h.add();

	if last.mem_id then
		output;
	keep mem_id count;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And please be aware:&amp;nbsp;the&amp;nbsp;entrance order&amp;nbsp;of the data MATTERS. So this is why SQL will not work for you in this case.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2016 19:49:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unique-prescriber-count/m-p/287936#M59296</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2016-07-28T19:49:56Z</dc:date>
    </item>
    <item>
      <title>Re: Unique prescriber count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unique-prescriber-count/m-p/287957#M59304</link>
      <description>&lt;P&gt;Thank you Haikuo. &amp;nbsp;It appears to be working for the above example. I should have been more clear with my requirement.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am actually looking different count for below example&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;M003 P116 F116 &amp;nbsp; + 1&lt;BR /&gt;M003 P448 F081 &amp;nbsp;+ 1&lt;BR /&gt;M003 P448 F123 &amp;nbsp;- pres already counted&lt;BR /&gt;&lt;STRONG&gt;M003 P031 F387&amp;nbsp;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;M003 P031 F081 &amp;nbsp;- pres is part of F081 facitlity which is already counted&amp;nbsp;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;M003 P031 F123&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;M003 P031 F133&lt;/STRONG&gt;&lt;BR /&gt;&lt;EM&gt;M003 P165 F081 &amp;nbsp;&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;M003 P165 F123 -&amp;nbsp;pres is part of F123/F081 , already counted&amp;nbsp;&lt;/EM&gt;&lt;BR /&gt;M003 P662 F080&lt;BR /&gt;M003 P662 F081 -&amp;nbsp;&lt;STRONG&gt;pres is part of F081 facitlity which is already counted&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the code shows count as 4 where as i am expecting only 2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;only first 2 obs are unique,&amp;nbsp;others either prescriber is repeating or one of the facility of new prescriber is already part of counted prescriber&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2016 20:55:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unique-prescriber-count/m-p/287957#M59304</guid>
      <dc:creator>sasuser77</dc:creator>
      <dc:date>2016-07-28T20:55:02Z</dc:date>
    </item>
    <item>
      <title>Re: Unique prescriber count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unique-prescriber-count/m-p/287966#M59310</link>
      <description>&lt;P&gt;What is counted when a prescriber is in multiple facilities, is that 2 or 1?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And same facility is counted only once?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2016 21:27:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unique-prescriber-count/m-p/287966#M59310</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-07-28T21:27:16Z</dc:date>
    </item>
    <item>
      <title>Re: Unique prescriber count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unique-prescriber-count/m-p/287973#M59315</link>
      <description>&lt;P&gt;when prescriber in muti facilitis , is counted as 1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and the facility which is already belong to a counted prescriber facility is not counted again.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2016 21:43:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unique-prescriber-count/m-p/287973#M59315</guid>
      <dc:creator>sasuser77</dc:creator>
      <dc:date>2016-07-28T21:43:18Z</dc:date>
    </item>
    <item>
      <title>Re: Unique prescriber count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unique-prescriber-count/m-p/287977#M59318</link>
      <description>&lt;P&gt;That's not an&amp;nbsp;easy problem, at least in any solution I've seen.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's the pretty much identical problem that was posed last week on SO&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://stackoverflow.com/questions/38494568/data-step-manipulation-based-on-two-fields-conditioning" target="_blank"&gt;http://stackoverflow.com/questions/38494568/data-step-manipulation-based-on-two-fields-conditioning&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2016 22:13:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unique-prescriber-count/m-p/287977#M59318</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-07-28T22:13:42Z</dc:date>
    </item>
    <item>
      <title>Re: Unique prescriber count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unique-prescriber-count/m-p/288046#M59351</link>
      <description>&lt;PRE&gt;
Yes. It is really not easy question. I love this question.
and Hash Table come to rescue .

data have;
	input 
		Mem_id$ pres_id$ fac_id$;
	cards;
M001 P001 F001 
M001 P001 F002 
M001 P002 F003 
M001 P003 F003 
M001 P003 F004 
M001 P003 F005 
M001 P004 F002 
M002 P002 F002 
M002 P002 F003 
M002 P003 F002 
M003 P116 F116  
M003 P448 F081 
M003 P448 F123
M003 P031 F387 
M003 P031 F081
M003 P031 F123
M003 P031 F133
M003 P165 F081  
M003 P165 F123
M003 P662 F080
M003 P662 F081
;
run;

data want;
 if _n_=1 then do;
  if 0 then set have;
  declare hash h(dataset:'have');
  h.definekey('Mem_id','pres_id','fac_id');
  h.definedone();
  
  declare hash h1(dataset:'have',multidata:'y');
  h1.definekey('Mem_id','fac_id');
  h1.definedata('pres_id');
  h1.definedone();
  
  declare hash h2(dataset:'have',multidata:'y');
  h2.definekey('Mem_id','pres_id');
  h2.definedata('fac_id');
  h2.definedone();
 end;
set have;
by Mem_id;
if first.Mem_id then count=0;
if h.check()=0 then count+1;

rc=h1.find();
do while(rc=0);
 rr=h.remove();
 
 rx=h2.find();
 do while(rx=0);
  rr=h.remove();
  rx=h2.find_next();
 end;
 
 rc=h1.find_next();
end;

if last.Mem_id;

keep Mem_id count;
run;

&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Jul 2016 06:42:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unique-prescriber-count/m-p/288046#M59351</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-07-29T06:42:32Z</dc:date>
    </item>
    <item>
      <title>Re: Unique prescriber count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unique-prescriber-count/m-p/288049#M59352</link>
      <description>&lt;PRE&gt;
Opps. I found a problem in my last code(I don't know why I can't delete last post ). 
Try this one :




data have;
	input 
		Mem_id$ pres_id$ fac_id$;
	cards;
M001 P001 F001 
M001 P001 F002 
M001 P002 F003 
M001 P003 F003 
M001 P003 F004 
M001 P003 F005 
M001 P004 F002 
M002 P002 F002 
M002 P002 F003 
M002 P003 F002 
M003 P116 F116  
M003 P448 F081 
M003 P448 F123
M003 P031 F387 
M003 P031 F081
M003 P031 F123
M003 P031 F133
M003 P165 F081  
M003 P165 F123
M003 P662 F080
M003 P662 F081
;
run;

data want;
 if _n_=1 then do;
  if 0 then set have;
  declare hash h(dataset:'have');
  h.definekey('Mem_id','pres_id','fac_id');
  h.definedone();
  
  declare hash h1(dataset:'have',multidata:'y');
  h1.definekey('Mem_id','fac_id');
  h1.definedata('pres_id');
  h1.definedone();
  
  declare hash h2(dataset:'have',multidata:'y');
  h2.definekey('Mem_id','pres_id');
  h2.definedata('fac_id');
  h2.definedone();
 end;
set have;
by Mem_id;
if first.Mem_id then count=0;
if h.check()=0 then count+1;

_fac_id=fac_id;
rc=h1.find();
do while(rc=0);
 rr=h.remove();
 
 rx=h2.find();
 do while(rx=0);
  rr=h.remove();
  rx=h2.find_next();
 end;
 
 fac_id=_fac_id;
 rc=h1.find_next();
end;

if last.Mem_id;

keep Mem_id count;
run;


&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Jul 2016 06:52:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unique-prescriber-count/m-p/288049#M59352</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-07-29T06:52:31Z</dc:date>
    </item>
    <item>
      <title>Re: Unique prescriber count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unique-prescriber-count/m-p/288113#M59361</link>
      <description>&lt;P&gt;Thank you xia. This is so perfect and you are a great life saver.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jul 2016 13:47:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unique-prescriber-count/m-p/288113#M59361</guid>
      <dc:creator>sasuser77</dc:creator>
      <dc:date>2016-07-29T13:47:53Z</dc:date>
    </item>
  </channel>
</rss>

