<?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: Difference between the dates depending on the conditions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-the-dates-depending-on-the-conditions/m-p/542767#M149993</link>
    <description>&lt;P&gt;I have changed the date format. But the contents are same. I am looking for the conditions to apply the days between calaculations.&lt;/P&gt;</description>
    <pubDate>Wed, 13 Mar 2019 13:05:31 GMT</pubDate>
    <dc:creator>Vk_2</dc:creator>
    <dc:date>2019-03-13T13:05:31Z</dc:date>
    <item>
      <title>Difference between the dates depending on the conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-the-dates-depending-on-the-conditions/m-p/542607#M149930</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I want to calculate days between like this calculation&lt;/SPAN&gt;&amp;nbsp;shown picture&lt;/P&gt;&lt;P&gt;I wrote the code like this&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA table2;
infile datalines DELIMITER=',';
INFORMAT cust_id 4.  visit_dt  Purchase_dt MMDDYY10. item $10.  first_purchase_dt MMDDYY10. ; 
INPUT cust_id visit_dt Purchase_dt item FIRST_Purchase_dt;  
format visit_dt Purchase_dt FIRST_Purchase_dt date9.;
DATALINES;
1234,01/11/2017,01/12/2017,Big,01/01/2015
1234,01/18/2018,01/19/2018,Big,01/01/2015
1567,01/11/2008,01/12/2008,Big,03/27/2007
1345,01/03/2006,.,Small,01/02/2006
1345,01/24/2008,01/24/2008,Big,01/02/2006
1579,01/24/2009,.,Medium,05/06/2006
;
run;
 
proc sort data=table2;

by cust_id visit_dt ;
run;
data want;
set table2;
by cust_id;
prev_date=lag(COALESCE(Purchase_dt,visit_dt));
format prev_date date9.;
DAYS_BTW = intck('day', first_purchase_dt, visit_dt); 
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Days between should be calculated like this-&lt;/P&gt;&lt;P&gt;a) if there is no duplicate Cust_id then it is visit_dt-First_Purchase dt or the first duplicate cust_id when sorted by visit_dt.&lt;/P&gt;&lt;P&gt;b) if there is duplicate then visit_dt-Previous(purchase_dt) if it exists else visit_dt-previous(visit_dt). This happens as when the item is small or medium then there is no purchase dt.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think I am using right functions but not according to the conditions that I want. I am using sas eg version-7.12 (7.100.2.3350) (64-bit)&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture_days_btw.JPG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/27886i00213E4E28D9EDA3/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture_days_btw.JPG" alt="Capture_days_btw.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Mar 2019 23:24:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-the-dates-depending-on-the-conditions/m-p/542607#M149930</guid>
      <dc:creator>Vk_2</dc:creator>
      <dc:date>2019-03-12T23:24:05Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between the dates depending on the conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-the-dates-depending-on-the-conditions/m-p/542620#M149932</link>
      <description>&lt;P&gt;your screen shot example doesn't match what your output is based on your code sample.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV&gt;&lt;FONT style="background-color: #ffffff;"&gt;The SAS System &lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV&gt;
&lt;P&gt;Obs cust_id visit_dt Purchase_dt item first_purchase_dt prev_date DAYS_BTW &lt;BR /&gt;1 1234 11JAN2017 12JAN2017 Big 01JAN2015 . 741 &lt;BR /&gt;2 1234 18JAN2018 19JAN2018 Big 01JAN2015 12JAN2017 1113 &lt;BR /&gt;3 1345 03JAN2006 . Small 02JAN2006 19JAN2018 1 &lt;BR /&gt;4 1345 24JAN2008 24JAN2008 Big 02JAN2006 03JAN2006 752 &lt;BR /&gt;5 1567 11JAN2008 12JAN2008 Big 27MAR2007 24JAN2008 290 &lt;BR /&gt;6 1579 24JAN2009 . Medium 06MAY2006 12JAN2008 994&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Mar 2019 01:29:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-the-dates-depending-on-the-conditions/m-p/542620#M149932</guid>
      <dc:creator>VDD</dc:creator>
      <dc:date>2019-03-13T01:29:33Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between the dates depending on the conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-the-dates-depending-on-the-conditions/m-p/542767#M149993</link>
      <description>&lt;P&gt;I have changed the date format. But the contents are same. I am looking for the conditions to apply the days between calaculations.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Mar 2019 13:05:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-the-dates-depending-on-the-conditions/m-p/542767#M149993</guid>
      <dc:creator>Vk_2</dc:creator>
      <dc:date>2019-03-13T13:05:31Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between the dates depending on the conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-the-dates-depending-on-the-conditions/m-p/542799#M150006</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA table2;
infile datalines DELIMITER=',';
INFORMAT cust_id 4.  visit_dt  Purchase_dt MMDDYY10. item $10.  first_purchase_dt MMDDYY10. ; 
INPUT cust_id visit_dt Purchase_dt item FIRST_Purchase_dt;  
format visit_dt Purchase_dt FIRST_Purchase_dt date9.;
DATALINES;
1234,01/11/2017,01/12/2017,Big,01/01/2015
1234,01/18/2018,01/19/2018,Big,01/01/2015
1567,01/11/2008,01/12/2008,Big,03/27/2007
1345,01/03/2006,.,Small,01/02/2006
1345,01/24/2008,01/24/2008,Big,01/02/2006
1579,01/24/2009,.,Medium,05/06/2006
;
run;
 
proc sort data=table2;

by cust_id visit_dt ;
run;
data want;
set table2;
by cust_id;
prev_cust_id=lag(cust_id);
prev_date=lag(COALESCE(Purchase_dt,visit_dt));
format prev_date date9.;
if cust_id=prev_cust_id then DAYS_BTW = intck('day', prev_date,visit_dt);
else DAYS_BTW = intck('day', FIRST_Purchase_dt,visit_dt);&lt;BR /&gt;drop prev_cust_id prev_date;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;got the logic right.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Mar 2019 14:27:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-the-dates-depending-on-the-conditions/m-p/542799#M150006</guid>
      <dc:creator>Vk_2</dc:creator>
      <dc:date>2019-03-13T14:27:25Z</dc:date>
    </item>
  </channel>
</rss>

