<?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: How do I count unique observations for date by ID? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-unique-observations-for-date-by-ID/m-p/708313#M217668</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input ID	SVCDATE;
cards;
1	22016
1	22016
1	22016
1	32016
1	82016
2	72016
2	72016
2	82016
2	102016
;

data want;
 do until(last.svcdate);
  set have;
  by id svcdate notsorted;
  count=sum(count,1);
 end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 26 Dec 2020 23:39:03 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2020-12-26T23:39:03Z</dc:date>
    <item>
      <title>How do I count unique observations for date by ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-unique-observations-for-date-by-ID/m-p/708306#M217661</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please see the dataset below:&lt;/P&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;SVCDATE&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;22016&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;22016&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;22016&lt;/TD&gt;&lt;/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t;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;102016&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default dgrid-selected ui-state-active"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default dgrid-selected ui-state-active"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default dgrid-selected ui-state-active"&gt;Can someone please help me code this?&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default dgrid-selected ui-state-active"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default dgrid-selected ui-state-active"&gt;Thanks!&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-odd ui-state-default"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-odd ui-state-default"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-odd ui-state-default"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-odd ui-state-default"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-odd ui-state-default"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Sat, 26 Dec 2020 23:03:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-unique-observations-for-date-by-ID/m-p/708306#M217661</guid>
      <dc:creator>raajdesaii</dc:creator>
      <dc:date>2020-12-26T23:03:56Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count unique observations for date by ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-unique-observations-for-date-by-ID/m-p/708308#M217663</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=have;
    tables id*svdate/list;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Please, in the future do not provide data as screen captures. Provide the data according to &lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;these instructions&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Sat, 26 Dec 2020 23:06:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-unique-observations-for-date-by-ID/m-p/708308#M217663</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-12-26T23:06:11Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count unique observations for date by ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-unique-observations-for-date-by-ID/m-p/708309#M217664</link>
      <description>This does not get me what I have in the output.&lt;BR /&gt;&lt;BR /&gt;I want to know how many unique date counts are by each id. For e.g. ID1 has 3 counts for 22016, ID1 has 1 count for 32016. I want to do this in a datastep.</description>
      <pubDate>Sat, 26 Dec 2020 23:10:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-unique-observations-for-date-by-ID/m-p/708309#M217664</guid>
      <dc:creator>raajdesaii</dc:creator>
      <dc:date>2020-12-26T23:10:40Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count unique observations for date by ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-unique-observations-for-date-by-ID/m-p/708312#M217667</link>
      <description>&lt;P&gt;This is best done in SQL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID $ 1 SVCDATE;
cards;
1	22016
1	22016
1	22016
1	32016
1	82016
2	72016
2	72016
2	82016
2	102016
;
run;

proc sql;
  create table want as 
  select ID
         ,SVCDATE
         ,count(*) as Count
  from have
  group by ID
          ,SVCDATE
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 26 Dec 2020 23:35:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-unique-observations-for-date-by-ID/m-p/708312#M217667</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2020-12-26T23:35:40Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count unique observations for date by ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-unique-observations-for-date-by-ID/m-p/708313#M217668</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input ID	SVCDATE;
cards;
1	22016
1	22016
1	22016
1	32016
1	82016
2	72016
2	72016
2	82016
2	102016
;

data want;
 do until(last.svcdate);
  set have;
  by id svcdate notsorted;
  count=sum(count,1);
 end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 26 Dec 2020 23:39:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-unique-observations-for-date-by-ID/m-p/708313#M217668</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-12-26T23:39:03Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count unique observations for date by ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-unique-observations-for-date-by-ID/m-p/708314#M217669</link>
      <description>&lt;P&gt;Are your data already arranged such that each id/svcdate appears in consecutive records?&amp;nbsp; If so, then&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID	SVCDATE;
datalines;
1	22016
1	22016
1	22016
1	32016
1	82016
2	72016
2	72016
2	82016
2	102016
run;

data want;
  set have;
  by id svcdate notsorted;
  if last.svcdate;
  count=dif(_n_);
  if count=. then count=_n_;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The automatic variable _N_ is the "iteration number" of the data step - in this case equivalent to the observation number.&amp;nbsp; The DIF function is defined as&amp;nbsp; DIF(_N_)=_N_-lag(_N_).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since the dif function is only executed at the end of each id/svcdate group, due to the "subsetting IF" statement&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;U&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;if last.svcdate;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;the lagged value of _N_ will always be the _N_ at the end of the prior group - i.e.&amp;nbsp; &amp;nbsp;you are calculating&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; COUNT=&amp;nbsp;&lt;FONT face="courier new,courier"&gt;_N_(at end of current group) - _N_(of end of prior group).&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the first group COUNT will be missing, so it has to be corrected.&lt;/P&gt;</description>
      <pubDate>Sat, 26 Dec 2020 23:39:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-unique-observations-for-date-by-ID/m-p/708314#M217669</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-12-26T23:39:08Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count unique observations for date by ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-unique-observations-for-date-by-ID/m-p/708315#M217670</link>
      <description>&lt;P&gt;Thanks! that worked.&lt;/P&gt;</description>
      <pubDate>Sun, 27 Dec 2020 00:00:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-unique-observations-for-date-by-ID/m-p/708315#M217670</guid>
      <dc:creator>raajdesaii</dc:creator>
      <dc:date>2020-12-27T00:00:31Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count unique observations for date by ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-unique-observations-for-date-by-ID/m-p/708316#M217671</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/196010"&gt;@raajdesaii&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;This does not get me what I have in the output.&lt;BR /&gt;&lt;BR /&gt;I want to know how many unique date counts are by each id. For e.g. ID1 has 3 counts for 22016, ID1 has 1 count for 32016. I want to do this in a datastep.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It works for me.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Doing it in a DATA step is the hard way to do it. You have to write your own code, debug it, and so on, whereas using PROC FREQ,&amp;nbsp; SAS has already done the hard work of programming it, testing it, and debugging it.&lt;/P&gt;</description>
      <pubDate>Sun, 27 Dec 2020 00:07:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-unique-observations-for-date-by-ID/m-p/708316#M217671</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-12-27T00:07:05Z</dc:date>
    </item>
  </channel>
</rss>

