<?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: Including dates/rows with no frequency counts in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Including-dates-rows-with-no-frequency-counts/m-p/331027#M74350</link>
    <description>mkeintz - thank you very much, and for providing the alternate methods to specify the date range. It works perfectly!</description>
    <pubDate>Wed, 08 Feb 2017 23:11:07 GMT</pubDate>
    <dc:creator>ucdcrush</dc:creator>
    <dc:date>2017-02-08T23:11:07Z</dc:date>
    <item>
      <title>Including dates/rows with no frequency counts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Including-dates-rows-with-no-frequency-counts/m-p/330945#M74325</link>
      <description>&lt;P&gt;Hello all. I am working with data similar to the following&lt;/P&gt;
&lt;P&gt;(this is the individual row level data with many other columns for a given lab test received):&lt;/P&gt;
&lt;P&gt;labname &amp;nbsp; &amp;nbsp; received_date &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;Quest &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2/1/17 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Quest &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2/4/17 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;Quest &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2/4/17&lt;/P&gt;
&lt;P&gt;Labcorp &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2/1/17&lt;/P&gt;
&lt;P&gt;Labcorp &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2/2/17&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and I would like to produce a final summary table that looks like this, which includes rows for all days in a range, not only&lt;/P&gt;
&lt;P&gt;the days that had data associated with them for that lab, i.e.,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;labname &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;date &amp;nbsp; &amp;nbsp; count&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Quest &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2/1/17 &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;Quest &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2/2/17 &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Quest &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2/3/17 &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Quest &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2/4/17 &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;2&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Labcorp &amp;nbsp; &amp;nbsp; &amp;nbsp; 2/1/17 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Labcorp &amp;nbsp; &amp;nbsp; &amp;nbsp; 2/2/17 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Labcorp &amp;nbsp; &amp;nbsp; &amp;nbsp; 2/3/17 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Labcorp &amp;nbsp; &amp;nbsp; &amp;nbsp; 2/4/17 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;...&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I am able to get the counts in a table from proc freq where there were records for a given lab on a particular day. But I would like to produce the final table/report to include all days within a range, so that we can look at the report and see when there were no records sent on particular days.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I am not sure how to approach this. Something in my brain says I could create a separate table of all dates of interest, then somehow join or merge to have them included in the data, but I don't know how to do that nor am I sure that's the path to go down.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Any ideas are appreciated, thank you.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Feb 2017 18:53:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Including-dates-rows-with-no-frequency-counts/m-p/330945#M74325</guid>
      <dc:creator>ucdcrush</dc:creator>
      <dc:date>2017-02-08T18:53:40Z</dc:date>
    </item>
    <item>
      <title>Re: Including dates/rows with no frequency counts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Including-dates-rows-with-no-frequency-counts/m-p/330955#M74328</link>
      <description>&lt;P&gt;Hi UCDCRush,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So many firsts today! Had my first question and my first (hopefully helpful) answer.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've quickly written some code you may find useful. In general, proc sql works great for these kinds of issues. The only downside is I don't really know how to generate 0's for days where you have no data for a particular lab.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*Input data;
data test;
	input lab: $ 
		  date: mmddyy10.;
		format date mmddyy10.;
	datalines;
Quest	2/1/17
Quest	2/4/17
Quest	2/4/17
Labcorp	2/1/17
Labcorp	2/2/17
;

run;

*Print to make sure results are as expected;
proc print data=test;
	attrib _all_ format=;
run;

*Use SQL to create a table. You want the count of total entries for each day grouped by both the date and the lab;
proc sql;
	create table sums as
	select lab as lab,  date as date, count(lab) as count
	from test
	group by date, lab;
quit;

*Print new table, check for accuracy;
proc print data=sums;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;You can use "where" statements after you generate your count data to limit to the dates you want. So, if you wanted to limit to dates between 2/1/17 and 2/4/17, you could use this code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*Print new table, check for accuracy;
proc print data=sums;
	where 20851&amp;lt;=date&amp;lt;=20854;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;You just have to be familiar with how SAS codes dates. using "attrib _all_ format=.;" is a statement you can add to almost any proc to see the unformatted values of variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this helps!&lt;/P&gt;</description>
      <pubDate>Wed, 08 Feb 2017 19:18:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Including-dates-rows-with-no-frequency-counts/m-p/330955#M74328</guid>
      <dc:creator>jondowns</dc:creator>
      <dc:date>2017-02-08T19:18:33Z</dc:date>
    </item>
    <item>
      <title>Re: Including dates/rows with no frequency counts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Including-dates-rows-with-no-frequency-counts/m-p/330959#M74330</link>
      <description>&lt;P&gt;Sorry for the double post. Here is the table you get when you print the "sums" dataset:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="5"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P align="center"&gt;&lt;STRONG&gt;Obs&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P align="center"&gt;&lt;STRONG&gt;lab&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P align="center"&gt;&lt;STRONG&gt;date&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P align="center"&gt;&lt;STRONG&gt;count&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P align="center"&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Labcorp&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;02/01/2017&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P align="center"&gt;&lt;STRONG&gt;2&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Quest&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;02/01/2017&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P align="center"&gt;&lt;STRONG&gt;3&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Labcorp&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;02/02/2017&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P align="center"&gt;&lt;STRONG&gt;4&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Quest&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;02/04/2017&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Wed, 08 Feb 2017 19:22:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Including-dates-rows-with-no-frequency-counts/m-p/330959#M74330</guid>
      <dc:creator>jondowns</dc:creator>
      <dc:date>2017-02-08T19:22:10Z</dc:date>
    </item>
    <item>
      <title>Re: Including dates/rows with no frequency counts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Including-dates-rows-with-no-frequency-counts/m-p/330963#M74332</link>
      <description>&lt;P&gt;Assuming:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;data are grouped (not&amp;nbsp;necessarily sorted) by labname&lt;/LI&gt;
&lt;LI&gt;data are sorted by received_date within labname&lt;/LI&gt;
&lt;LI&gt;every labname begins with are received_date=2/1/17&lt;/LI&gt;
&lt;LI&gt;the maximum date is known to be 2/4/17&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do count=1 by 1 until (last.received_date);
    set have ;
    by labname received_date notsorted;
    if eod=0 then set have (firstobs=2 keep=received_date rename=(received_date=nxtdate)) end=eod;
  end;
  output;

  lastdate=ifn(last.labname,'04feb2017'd,nxtdate-1);
  count=0;
  if lastdate&amp;gt;received_date then do received_date=received_date+1 to lastdate;
    output;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If not all lab's begin on 2/1/2017, then you could modify to:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let begdate=01feb2017;
%let enddate=04feb2017;

data want;
  do count=1 by 1 until (last.received_date);
    set have ;
    by labname received_date notsorted;
    if eod=0 then set have (firstobs=2 keep=received_date rename=(received_date=nxtdate)) end=eod;
  end;

  /* Fill any gaps at the beginning of a lab */
  if lag(labname)^=labname and received_date&amp;gt; "&amp;amp;begdate"d then do;
    store_count=count; store_date=received_date;
    count=0;
    do received_date="&amp;amp;begdate"d to store_date-1;
      output;
    end;
    count=store_count; received_date=store_date;
  end;

  /* Output current count */
  output;

  /* Take care of any gaps following this date */
  lastdate=ifn(last.labname,"&amp;amp;enddate"d,nxtdate-1);
  count=0;
  if lastdate&amp;gt;received_date then do received_date=received_date+1 to lastdate;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this program only the expected date range is known in advance (see the %let begdate=&amp;nbsp; and %let enddate= statements).&amp;nbsp; You could also run a PROC SQL with "select into :" to gets those dates, as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;sql&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="2"&gt;noprint&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;&amp;nbsp; select&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; min(received_date) format=&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;date9.&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;, max(received_date) format=&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;date9.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;&amp;nbsp; into&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; :begdate , :enddate&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;&amp;nbsp; from&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; have;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Feb 2017 19:36:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Including-dates-rows-with-no-frequency-counts/m-p/330963#M74332</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-02-08T19:36:28Z</dc:date>
    </item>
    <item>
      <title>Re: Including dates/rows with no frequency counts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Including-dates-rows-with-no-frequency-counts/m-p/331027#M74350</link>
      <description>mkeintz - thank you very much, and for providing the alternate methods to specify the date range. It works perfectly!</description>
      <pubDate>Wed, 08 Feb 2017 23:11:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Including-dates-rows-with-no-frequency-counts/m-p/331027#M74350</guid>
      <dc:creator>ucdcrush</dc:creator>
      <dc:date>2017-02-08T23:11:07Z</dc:date>
    </item>
  </channel>
</rss>

