<?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 count for in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unique-count-for/m-p/397680#M25564</link>
    <description>Thanks for you help, your code worked like a charm</description>
    <pubDate>Thu, 21 Sep 2017 03:17:27 GMT</pubDate>
    <dc:creator>Haydn</dc:creator>
    <dc:date>2017-09-21T03:17:27Z</dc:date>
    <item>
      <title>Unique count for</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unique-count-for/m-p/397307#M25556</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sample data below&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;Ref&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;InvoiceNumber&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;InvoiceType&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Reason&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;A1&lt;/TD&gt;&lt;TD&gt;NAA&lt;/TD&gt;&lt;TD&gt;Income&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;A2&lt;/TD&gt;&lt;TD&gt;NAA&lt;/TD&gt;&lt;TD&gt;Income&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;A3&lt;/TD&gt;&lt;TD&gt;DPP&lt;/TD&gt;&lt;TD&gt;Income&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;A4&lt;/TD&gt;&lt;TD&gt;PPA&lt;/TD&gt;&lt;TD&gt;Care&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to do a unique count combing Ref and InvoiceType and a unique count combing Ref and Reason. Output should be as below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;Ref&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;InvoiceNumber&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;InvoiceType&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Reason&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;UnqiueRef&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;UniqueReason&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;UniqueInvoiceType&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;A1&lt;/TD&gt;&lt;TD&gt;NAA&lt;/TD&gt;&lt;TD&gt;Income&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;A2&lt;/TD&gt;&lt;TD&gt;NAA&lt;/TD&gt;&lt;TD&gt;Income&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;A3&lt;/TD&gt;&lt;TD&gt;DPP&lt;/TD&gt;&lt;TD&gt;Income&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;A4&lt;/TD&gt;&lt;TD&gt;PPA&lt;/TD&gt;&lt;TD&gt;Care&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I had thought of concatenating Ref and InvoiceType (new field UniqueInvoiceType)&amp;nbsp;and also Ref and Reason (new field UniqueReason) and doing an if first on both thos fields.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is this the best/most efficient way of achiveing this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers&lt;/P&gt;&lt;P&gt;Haydn&lt;/P&gt;</description>
      <pubDate>Wed, 20 Sep 2017 03:12:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unique-count-for/m-p/397307#M25556</guid>
      <dc:creator>Haydn</dc:creator>
      <dc:date>2017-09-20T03:12:26Z</dc:date>
    </item>
    <item>
      <title>Re: Unique count for</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unique-count-for/m-p/397330#M25557</link>
      <description>&lt;P&gt;I am very sure, there will be much easier and cleaner solution then what I have written. if your combination of ref and invoicenumber are unique then you can do sperately each first.group and then join them back as shown below.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Ref $ 1-3 InvoiceNumber $ 5-6	InvoiceType $ 8-10	Reason $ 12-18;
datalines;
123	A1	NAA	Income
123	A2	NAA	Income
123	A3	DPP	Income
123	A4	PPA	Care
223	A1	NAA	Income
223	A2	NAA	Income
223	A3	DPP	Income
223	A4	PPA	Care
;

proc sort data = have out=have1;
by Ref invoicenumber;
run;

data have1a;
set have1;
by Ref invoicenumber;
if first.ref then UnqiueRef = 1; else UnqiueRef =.;
 run;
 
 proc sort data = have out=have2;
by Ref  InvoiceType;
run;
 
 data have2a;
set have2;
by Ref  InvoiceType;
if first.invoicetype = 1 then UniqueInvoiceType =1 ; 
 else UniqueInvoiceType =.;
 run;
 
  proc sort data = have out=have3;
by Ref reason;
run;
 
 data have3a;
set have3;
by Ref  reason;
if first.reason = 1 then Uniquereason =1 ; 
 else Uniquereason =.;
 run;
 
 
 proc sql;
 create table want as
 select a.Ref,
        a.invoicenumber,
        a.invoicetype,
        a.reason,
        a.UnqiueRef,
        b.UniqueInvoiceType,
        c.Uniquereason
 from have1a a, have2a b, have3a c
 where  a.Ref = b.ref
 and    a.ref=c.ref
 and   a.InvoiceNumber =b.InvoiceNumber
 and    a.InvoiceNumber =c.InvoiceNumber
 order by a.Ref, a.InvoiceNumber;
 quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 20 Sep 2017 05:50:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unique-count-for/m-p/397330#M25557</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2017-09-20T05:50:47Z</dc:date>
    </item>
    <item>
      <title>Re: Unique count for</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unique-count-for/m-p/397440#M25561</link>
      <description>&lt;PRE&gt;
I love this question. What if there are more than one Ref ?


data have;
input Ref $ 1-3 InvoiceNumber $ 5-6	InvoiceType $ 8-10	Reason $ 12-18;
datalines;
123	A1	NAA	Income
123	A2	NAA	Income
123	A3	DPP	Income
123	A4	PPA	Care
223	A1	NAA	Income
223	A2	NAA	Income
223	A3	DPP	Income
223	A4	PPA	Care
;

data want;
 if _n_=1 then do;
  if 0 then set have;
  declare hash h1(dataset:'have');
  h1.definekey('ref');
  h1.definedone();

  declare hash h2(dataset:'have');
  h2.definekey('ref','InvoiceType');
  h2.definedone();

  declare hash h3(dataset:'have');
  h3.definekey('ref','Reason');
  h3.definedone();
 end;
set have;
if h1.check()=0 then do;UnqiueRef=1;h1.remove();end;
if h2.check()=0 then do;UniqueReason=1;h2.remove();end;
if h3.check()=0 then do;UniqueInvoiceType=1;h3.remove();end;
run;

&lt;/PRE&gt;</description>
      <pubDate>Wed, 20 Sep 2017 13:37:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unique-count-for/m-p/397440#M25561</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-09-20T13:37:06Z</dc:date>
    </item>
    <item>
      <title>Re: Unique count for</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unique-count-for/m-p/397649#M25563</link>
      <description>&lt;P&gt;Thanks for your help on this.&lt;/P&gt;&lt;P&gt;There will definitely be more than 1 ref in the dataset (it has 2 million rows). I'm running this now and will post back here on how it went.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers&lt;/P&gt;&lt;P&gt;Haydn&lt;/P&gt;</description>
      <pubDate>Wed, 20 Sep 2017 23:39:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unique-count-for/m-p/397649#M25563</guid>
      <dc:creator>Haydn</dc:creator>
      <dc:date>2017-09-20T23:39:01Z</dc:date>
    </item>
    <item>
      <title>Re: Unique count for</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unique-count-for/m-p/397680#M25564</link>
      <description>Thanks for you help, your code worked like a charm</description>
      <pubDate>Thu, 21 Sep 2017 03:17:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Unique-count-for/m-p/397680#M25564</guid>
      <dc:creator>Haydn</dc:creator>
      <dc:date>2017-09-21T03:17:27Z</dc:date>
    </item>
  </channel>
</rss>

