<?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: Record distinct count in proc sql union in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Record-distinct-count-in-proc-sql-union/m-p/753245#M237378</link>
    <description>&lt;P&gt;This, maybe?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT;
  if _N_=1 then do;
   dcl hash H(dataset:'HAVE(obs=0)');
   H.definekey('ID');
   H.definedone();
  end;
  set HAVE;
  by VAR1 DATE;    
  if first.VAR1 then H.clear;
  if first.DATE then SUM=0;
  if H.check() then do;
    SUM+1;
    H.add();
  end;
  if last.DATE then output;
  keep VAR1 DATE SUM;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 09 Jul 2021 18:04:37 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2021-07-09T18:04:37Z</dc:date>
    <item>
      <title>Record distinct count in proc sql union</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Record-distinct-count-in-proc-sql-union/m-p/753025#M237277</link>
      <description>&lt;P&gt;Hello folks, I need to compare multiple list of IDs by dates and record distinct count of IDs in proc sql union function. Below are my codes:&lt;/P&gt;&lt;PRE&gt;proc sql;
select count(distinct ID) as cnt
from(
select ID
from test(where=(DATE="2019/10"))
UNION 
select ID
from test(where=(DATE="2019/11"))
UNION
select ID
from test(where=(DATE="2019/12"))
UNION
select ID
from test(where=(DATE="2020/01"))
.../*with more dates*/
);
quit;&lt;/PRE&gt;&lt;P&gt;Basically, I only need to count distinct IDs every time I "union" another date if that makes sense. Currently, my code only spits out the total distinct count for all the dates I am combining, for instance, after running the code above, SAS will have a single number in the output. But I want to have the distinct count each time I am adding in a date, for example, starting with the distinct count of IDs when date = 2019/09, I will have another count when date=2019/10 was added in, another count when date=2019/11 added in... which goes on until my last date value=2020/05.&amp;nbsp; Is there a solution for my question? Thanks so much!&lt;/P&gt;</description>
      <pubDate>Thu, 08 Jul 2021 22:10:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Record-distinct-count-in-proc-sql-union/m-p/753025#M237277</guid>
      <dc:creator>kevsma</dc:creator>
      <dc:date>2021-07-08T22:10:03Z</dc:date>
    </item>
    <item>
      <title>Re: Record distinct count in proc sql union</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Record-distinct-count-in-proc-sql-union/m-p/753030#M237279</link>
      <description>&lt;P&gt;I don't think it is quite clear how you expect the result to look.&lt;/P&gt;
&lt;P&gt;Can you show us an example of how the result looks? Best is a small example of the starting data and then the result.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Jul 2021 22:26:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Record-distinct-count-in-proc-sql-union/m-p/753030#M237279</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-07-08T22:26:04Z</dc:date>
    </item>
    <item>
      <title>Re: Record distinct count in proc sql union</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Record-distinct-count-in-proc-sql-union/m-p/753053#M237288</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;select DATE, count(distinct ID) as CNT
from test where DATE in("2019/10","2019/11","2019/12")
group by DATE&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jul 2021 00:48:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Record-distinct-count-in-proc-sql-union/m-p/753053#M237288</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-07-09T00:48:33Z</dc:date>
    </item>
    <item>
      <title>Re: Record distinct count in proc sql union</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Record-distinct-count-in-proc-sql-union/m-p/753210#M237366</link>
      <description>&lt;P&gt;Hi thank you and this's a great question, and I don't have a very clear sense as what SAS's output would look like. I am expecting to have SAS record the distinct count of IDs in a time-series fashion. One key information I forgot to mention is that the IDs by dates are accumulative, meaning at the starting point when date=2019/10, the IDs will all be included when the date moves onto the next month 2019/11. For example, if there are 20 distinct IDs when date=2019/10, SAS can create a variable that captures this number when date=2019/10; and when date=2019/11, there are 15 distinct IDs, but 13 of the 15 IDs are not distinct from the IDs when date=2019/10, only 2 IDs are newly added, so I want SAS to capture 17 as the total distinct count of IDs when date=2019/11. Hoping i am explaining this clearly...&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jul 2021 16:26:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Record-distinct-count-in-proc-sql-union/m-p/753210#M237366</guid>
      <dc:creator>kevsma</dc:creator>
      <dc:date>2021-07-09T16:26:59Z</dc:date>
    </item>
    <item>
      <title>Re: Record distinct count in proc sql union</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Record-distinct-count-in-proc-sql-union/m-p/753211#M237367</link>
      <description>&lt;P&gt;Thanks for your reply. Your method will give me the distinct count of IDs by date but this is not what I want exactly. What I want is the distinct count of IDs accumulatively if that makes sense. So whatever the IDs have been counted in a previous dates will be counted ONLY once and only the newly added IDs by dates will be added to the count. This requires a comparison of IDs between two adjacent dates I believe. Let me know whether I explain this clearly.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jul 2021 16:30:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Record-distinct-count-in-proc-sql-union/m-p/753211#M237367</guid>
      <dc:creator>kevsma</dc:creator>
      <dc:date>2021-07-09T16:30:04Z</dc:date>
    </item>
    <item>
      <title>Re: Record distinct count in proc sql union</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Record-distinct-count-in-proc-sql-union/m-p/753223#M237372</link>
      <description>&lt;P&gt;I found an old discussion thread&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;you was involved which answers my question perfectly!!!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/SAS-cumulative-count-by-unique-ID-and-date/td-p/653043" target="_self"&gt;https://communities.sas.com/t5/SAS-Programming/SAS-cumulative-count-by-unique-ID-and-date/td-p/653043&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if you don't mind, may I ask how to set up a looping macro based on following your code? Say if I have to run your following code based on another variable (VAR1) in the HAVE dataset. Basically I need to run your following code multiple times when VAR1=1,2,3,4,5...&lt;/P&gt;&lt;P&gt;Is there a way to do that?&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT;
  if _N_=1 then do;
   dcl hash H(dataset:'HAVE(obs=0)');
   H.definekey('ID');
   H.definedone();
  end;
  set HAVE;
  by DATE;    
  if first.DATE then SUM=0;
  if H.check() then do;
    SUM+1;
    H.add();
  end;
  if last.DATE then output;
  keep DATE SUM;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Jul 2021 17:13:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Record-distinct-count-in-proc-sql-union/m-p/753223#M237372</guid>
      <dc:creator>kevsma</dc:creator>
      <dc:date>2021-07-09T17:13:50Z</dc:date>
    </item>
    <item>
      <title>Re: Record distinct count in proc sql union</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Record-distinct-count-in-proc-sql-union/m-p/753245#M237378</link>
      <description>&lt;P&gt;This, maybe?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT;
  if _N_=1 then do;
   dcl hash H(dataset:'HAVE(obs=0)');
   H.definekey('ID');
   H.definedone();
  end;
  set HAVE;
  by VAR1 DATE;    
  if first.VAR1 then H.clear;
  if first.DATE then SUM=0;
  if H.check() then do;
    SUM+1;
    H.add();
  end;
  if last.DATE then output;
  keep VAR1 DATE SUM;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Jul 2021 18:04:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Record-distinct-count-in-proc-sql-union/m-p/753245#M237378</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2021-07-09T18:04:37Z</dc:date>
    </item>
    <item>
      <title>Re: Record distinct count in proc sql union</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Record-distinct-count-in-proc-sql-union/m-p/753250#M237380</link>
      <description>&lt;P&gt;Thank you for the reply! I ran the code but SAS spit out error messages, any ideas?&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;669    if first.VAR1 then H.clear;
                          --------
                          559    22
                                 76
ERROR 559-185: Invalid object attribute reference H.clear.

ERROR 22-322: Syntax error, expecting one of the following: (, +, =.

ERROR 76-322: Syntax error, statement will be ignored.

670    if first.DATE then sum=0;
671    if H.check() then do;
ERROR: DATA STEP Component Object failure.  Aborted during the COMPILATION phase.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jul 2021 18:19:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Record-distinct-count-in-proc-sql-union/m-p/753250#M237380</guid>
      <dc:creator>kevsma</dc:creator>
      <dc:date>2021-07-09T18:19:54Z</dc:date>
    </item>
    <item>
      <title>Re: Record distinct count in proc sql union</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Record-distinct-count-in-proc-sql-union/m-p/753252#M237381</link>
      <description>&lt;P&gt;I fixed the issue, there should have a parenthesis after clear. Now the following codes seem to work perfectly:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=want1; by VAR1 DATE; run;
data TEST; 
	if _N_=1 then do;
	dcl hash H(dataset:'want1(obs=0)');
	H.definekey('ID');
	H.definedone();
    end;
set want1;
  by VAR1 DATE;
  if first.VAR1 then H.clear();
  if first.DATE then sum=0;
  if H.check() then do;
  	sum + 1;
	H.add();
  end;
  if last.DATE then output;
  keep VAR1 DATE sum;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Jul 2021 18:25:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Record-distinct-count-in-proc-sql-union/m-p/753252#M237381</guid>
      <dc:creator>kevsma</dc:creator>
      <dc:date>2021-07-09T18:25:01Z</dc:date>
    </item>
  </channel>
</rss>

