<?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: Gap of at least 30 days in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Gap-of-at-least-30-days/m-p/326975#M72916</link>
    <description>&lt;P&gt;please try the sql code,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards missover;
input Person	Chocolate$	Date_Received:date9.	Date_Finished:date9.;
format date: date9.;
cards;
1	Snickers	7-May-15	11-May-15	
1	MM	        8-May-15	13-May-15	
1	Twix	    10-May-15	18-May-15	
1	Bounty	    12-May-15	16-May-15	
1	Hersheys	17-Jun-15	21-Jun-15	
;

proc sql;
create table test as select a.*, b.Date_Received as other_rec, b.Date_Finished as other_fini,a.Date_Received-b.Date_Finished as day, b.Chocolate as choc from have as a, 
(select Chocolate,person, Date_Received, Date_Finished from have) as b where a.person=b.person;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 24 Jan 2017 08:39:51 GMT</pubDate>
    <dc:creator>Jagadishkatam</dc:creator>
    <dc:date>2017-01-24T08:39:51Z</dc:date>
    <item>
      <title>Gap of at least 30 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Gap-of-at-least-30-days/m-p/326952#M72909</link>
      <description>&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Person&lt;/TD&gt;&lt;TD&gt;Chocolate&lt;/TD&gt;&lt;TD&gt;Date Received&lt;/TD&gt;&lt;TD&gt;Date Finished&lt;/TD&gt;&lt;TD&gt;No chocolate&lt;/TD&gt;&lt;TD&gt;Actual No chocolate&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;Snickers&lt;/TD&gt;&lt;TD&gt;7-May-15&lt;/TD&gt;&lt;TD&gt;11-May-15&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;MM&lt;/TD&gt;&lt;TD&gt;8-May-15&lt;/TD&gt;&lt;TD&gt;13-May-15&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;Twix&lt;/TD&gt;&lt;TD&gt;10-May-15&lt;/TD&gt;&lt;TD&gt;18-May-15&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;Bounty&lt;/TD&gt;&lt;TD&gt;12-May-15&lt;/TD&gt;&lt;TD&gt;16-May-15&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;Hersheys&lt;/TD&gt;&lt;TD&gt;17-Jun-15&lt;/TD&gt;&lt;TD&gt;21-Jun-15&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to find when there were at least 30 days without a chocolate for person 1. My current code sorts the data by date received, and gets me the solution to "No chocolate". This is wrong, as it marks the last row as 1, suggesting a difference between (date finsihed - date received for the next chocolate) is 16May15 - 17jun15 = 31 days, however the code does not account for the Bounty choclate date finished which is 18May15, and hence the difference should have been 18May15 - 17Jun15 = 29days and hence does not meet the criteria of at least 30 days as suggested by the Actual No Chocolate column.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sorting the data by date finshed descending does not help either&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be appreciated thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set&amp;nbsp;have&lt;/P&gt;&lt;P&gt;by person date_received;&lt;/P&gt;&lt;P&gt;gap=&lt;SPAN&gt;date_&lt;/SPAN&gt;&lt;SPAN&gt;received&lt;/SPAN&gt;-lag(&lt;SPAN&gt;date_finished&lt;/SPAN&gt;);&lt;/P&gt;&lt;P&gt;if first.person=1 then no_chocolate=1;&lt;/P&gt;&lt;P&gt;else if gap&amp;gt;=30 then &lt;SPAN&gt;no_chocolate&lt;/SPAN&gt;+1;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 24 Jan 2017 06:01:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Gap-of-at-least-30-days/m-p/326952#M72909</guid>
      <dc:creator>div44</dc:creator>
      <dc:date>2017-01-24T06:01:55Z</dc:date>
    </item>
    <item>
      <title>Re: Gap of at least 30 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Gap-of-at-least-30-days/m-p/326964#M72912</link>
      <description>&lt;P&gt;I dont see that the Date finished for the Bounty is&amp;nbsp;&lt;SPAN&gt;18May15? Isn't that the date finished for the Twix?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 24 Jan 2017 07:51:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Gap-of-at-least-30-days/m-p/326964#M72912</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2017-01-24T07:51:02Z</dc:date>
    </item>
    <item>
      <title>Re: Gap of at least 30 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Gap-of-at-least-30-days/m-p/326973#M72915</link>
      <description>&lt;P&gt;Hi.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your code should work,&amp;nbsp;Date Finished of Bounty is 16-May-2015.&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;data want;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;set&amp;nbsp;have&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;by person date_received;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;gap=&lt;SPAN&gt;date_&lt;/SPAN&gt;&lt;SPAN&gt;received&lt;/SPAN&gt;-lag(&lt;SPAN&gt;date_finished&lt;/SPAN&gt;);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;if first.person=1 then no_chocolate=1;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;else if gap&amp;gt;=30 then &lt;SPAN&gt;no_chocolate&lt;/SPAN&gt;+1;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bear in mind that if you want to count exclusively the days between to dates you need to adjust by subtracting one day:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, if date_received=17-may-2015 and date_finished=16-may-2015, former minus the latter would return 1 day, is that the expected result? maybe you are looking for something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;gap=&lt;/SPAN&gt;&lt;SPAN&gt;date_&lt;/SPAN&gt;&lt;SPAN&gt;received&lt;/SPAN&gt;&lt;SPAN&gt;-lag(&lt;/SPAN&gt;&lt;SPAN&gt;date_finished&lt;/SPAN&gt;&lt;SPAN&gt;)-1;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;&lt;SPAN&gt;Hope it helps.&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;&lt;SPAN&gt;Daniel Santos&amp;nbsp;@ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 24 Jan 2017 08:29:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Gap-of-at-least-30-days/m-p/326973#M72915</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2017-01-24T08:29:07Z</dc:date>
    </item>
    <item>
      <title>Re: Gap of at least 30 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Gap-of-at-least-30-days/m-p/326975#M72916</link>
      <description>&lt;P&gt;please try the sql code,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards missover;
input Person	Chocolate$	Date_Received:date9.	Date_Finished:date9.;
format date: date9.;
cards;
1	Snickers	7-May-15	11-May-15	
1	MM	        8-May-15	13-May-15	
1	Twix	    10-May-15	18-May-15	
1	Bounty	    12-May-15	16-May-15	
1	Hersheys	17-Jun-15	21-Jun-15	
;

proc sql;
create table test as select a.*, b.Date_Received as other_rec, b.Date_Finished as other_fini,a.Date_Received-b.Date_Finished as day, b.Chocolate as choc from have as a, 
(select Chocolate,person, Date_Received, Date_Finished from have) as b where a.person=b.person;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 24 Jan 2017 08:39:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Gap-of-at-least-30-days/m-p/326975#M72916</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2017-01-24T08:39:51Z</dc:date>
    </item>
    <item>
      <title>Re: Gap of at least 30 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Gap-of-at-least-30-days/m-p/326999#M72918</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards missover;
input Person	Chocolate$	Date_Received:date9.	Date_Finished:date9.;
format date: date9.;
cards;
1 Snickers 7-May-15 11-May-15
1 MM 8-May-15 13-May-15
1 Twix 10-May-15 18-May-15
1 Bounty 12-May-15 16-May-15
1 Hersheys 17-Jun-15 21-Jun-15
1 Milka 31-aug-15 05-sep-15
;
run;

data want;
set have;
by person;
retain max_finish;
no_chocolate = 0;
if not first.person
then do;
  if date_received - max_finish &amp;gt; 30
  then no_chocolate = 1;
end;
max_finish = max(max_finish,date_finished);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I added an additional data line to make sure that a value of 1 for no_chocolate is triggered.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Jan 2017 10:04:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Gap-of-at-least-30-days/m-p/326999#M72918</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-01-24T10:04:17Z</dc:date>
    </item>
    <item>
      <title>Re: Gap of at least 30 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Gap-of-at-least-30-days/m-p/327121#M72948</link>
      <description>&lt;P&gt;Yes, it should have been the finished date for Twix.&lt;/P&gt;&lt;P&gt;Thanks for pointing that out.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Jan 2017 17:44:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Gap-of-at-least-30-days/m-p/327121#M72948</guid>
      <dc:creator>div44</dc:creator>
      <dc:date>2017-01-24T17:44:40Z</dc:date>
    </item>
    <item>
      <title>Re: Gap of at least 30 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Gap-of-at-least-30-days/m-p/327122#M72949</link>
      <description>The finished date for Twix is what I was looking for which is 18May, and it is here that the code collapses</description>
      <pubDate>Tue, 24 Jan 2017 17:45:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Gap-of-at-least-30-days/m-p/327122#M72949</guid>
      <dc:creator>div44</dc:creator>
      <dc:date>2017-01-24T17:45:58Z</dc:date>
    </item>
    <item>
      <title>Re: Gap of at least 30 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Gap-of-at-least-30-days/m-p/327159#M72962</link>
      <description>&lt;P&gt;Run my code. If it doesn't do what you want, post the modified example data step, and where the result differs from your expectations.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Jan 2017 19:45:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Gap-of-at-least-30-days/m-p/327159#M72962</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-01-24T19:45:03Z</dc:date>
    </item>
  </channel>
</rss>

