<?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 Calculating Intervals between Purchases in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Calculating-Intervals-between-Purchases/m-p/589103#M18023</link>
    <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Problem.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/32506iDF9710E4C45D37A9/image-size/large?v=v2&amp;amp;px=999" role="button" title="Problem.PNG" alt="Problem.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Hi SAS community, i would like to create a column to calculate the interval in days between each Purchase_Flg = 1 for each USER_ID_Hash. I've racked my brains but it seems like i've arrived at a standstill.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Methods i've tried would be to use it in a by statement with User_ID_Hash, I_Date, Purchase_Flg (of course after sorting them) to try to obtain first.XXX and last.XXX variables to see if i could define some where statements based on them but it does not seem to work. I'm not sure how to approach this in the right way and it'll be great if someone can somehow point me in the right direction. Appreciate the help.&lt;/P&gt;</description>
    <pubDate>Mon, 16 Sep 2019 15:49:51 GMT</pubDate>
    <dc:creator>dartlee</dc:creator>
    <dc:date>2019-09-16T15:49:51Z</dc:date>
    <item>
      <title>Calculating Intervals between Purchases</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Calculating-Intervals-between-Purchases/m-p/589103#M18023</link>
      <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Problem.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/32506iDF9710E4C45D37A9/image-size/large?v=v2&amp;amp;px=999" role="button" title="Problem.PNG" alt="Problem.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Hi SAS community, i would like to create a column to calculate the interval in days between each Purchase_Flg = 1 for each USER_ID_Hash. I've racked my brains but it seems like i've arrived at a standstill.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Methods i've tried would be to use it in a by statement with User_ID_Hash, I_Date, Purchase_Flg (of course after sorting them) to try to obtain first.XXX and last.XXX variables to see if i could define some where statements based on them but it does not seem to work. I'm not sure how to approach this in the right way and it'll be great if someone can somehow point me in the right direction. Appreciate the help.&lt;/P&gt;</description>
      <pubDate>Mon, 16 Sep 2019 15:49:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Calculating-Intervals-between-Purchases/m-p/589103#M18023</guid>
      <dc:creator>dartlee</dc:creator>
      <dc:date>2019-09-16T15:49:51Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Intervals between Purchases</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Calculating-Intervals-between-Purchases/m-p/589110#M18024</link>
      <description>&lt;P&gt;Please show what you've tried and what didn't work.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by user_id;

retain last_purchase; 

*reset to missing at first ID;
if first.user_id then last_purchase = .;


if purchase_flag = 1 then do;
if not missing(last_purchase) then diff = i_date - last_purchase; 
last_purchase = i_date;
end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also, don't post pictures, please post actual sample data. This is untested because I will not type out your data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have issues with the code, please post the full log.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here are instructions on how to provide sample data as a data step:&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/290599"&gt;@dartlee&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Problem.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/32506iDF9710E4C45D37A9/image-size/large?v=v2&amp;amp;px=999" role="button" title="Problem.PNG" alt="Problem.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Hi SAS community, i would like to create a column to calculate the interval in days between each Purchase_Flg = 1 for each USER_ID_Hash. I've racked my brains but it seems like i've arrived at a standstill.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Methods i've tried would be to use it in a by statement with User_ID_Hash, I_Date, Purchase_Flg (of course after sorting them) to try to obtain first.XXX and last.XXX variables to see if i could define some where statements based on them but it does not seem to work. I'm not sure how to approach this in the right way and it'll be great if someone can somehow point me in the right direction. Appreciate the help.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Sep 2019 16:11:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Calculating-Intervals-between-Purchases/m-p/589110#M18024</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-09-16T16:11:17Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Intervals between Purchases</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Calculating-Intervals-between-Purchases/m-p/589152#M18028</link>
      <description>&lt;P&gt;Since your I_date variable appears to be datetime values you may need DATEPART with it to extract the dates. You don't specify what units you want your interval in.&lt;/P&gt;
&lt;P&gt;The following would return days between.&lt;/P&gt;
&lt;PRE&gt;proc sort data=have out=temp (where=(purchase_flg=1));
   by user_id_hash idate;
run;

data want;
   set temp;
   by user_id;
   last_date= datepart(lag(idate));
   if first.id then interval=.;
   else interval = datepart(idate) - lastdate;
   drop last_date; 
run;&lt;/PRE&gt;
&lt;P&gt;Merge back onto your original data by&amp;nbsp; user_id idate.&lt;/P&gt;</description>
      <pubDate>Mon, 16 Sep 2019 17:59:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Calculating-Intervals-between-Purchases/m-p/589152#M18028</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-09-16T17:59:37Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Intervals between Purchases</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Calculating-Intervals-between-Purchases/m-p/589752#M18039</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't need to extract date values from datetimes to generate number of days (i.e. number of midnights crossed) between two datetimes&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set temp;
   by user_id;
   interval=intck('hour24',lag(date),date);
   if first.id then interval=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;regards,&lt;/P&gt;
&lt;P&gt;Mark&lt;/P&gt;</description>
      <pubDate>Wed, 18 Sep 2019 17:44:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Calculating-Intervals-between-Purchases/m-p/589752#M18039</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-09-18T17:44:08Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Intervals between Purchases</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Calculating-Intervals-between-Purchases/m-p/589846#M18041</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't need to extract date values from datetimes to generate number of days (i.e. number of midnights crossed) between two datetimes&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set temp;
   by user_id;
   interval=intck('hour24',lag(date),date);
   if first.id then interval=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;regards,&lt;/P&gt;
&lt;P&gt;Mark&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Or use 'DTDAY' I believe.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Sep 2019 21:43:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Calculating-Intervals-between-Purchases/m-p/589846#M18041</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-09-18T21:43:18Z</dc:date>
    </item>
  </channel>
</rss>

