<?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 to flag a customer based on previous 5 yr rolling sales in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-a-customer-based-on-previous-5-yr-rolling-sales/m-p/645836#M193103</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input CUSTOMER_ID  $    Sale_Date : mmddyy10.;
format sale_date mmddyy10.;
cards;
A                              02/03/2010      .         2010
A                              03/05/2013      .         2013
A                              01/11/2019     1          2019
B                              06/08/2017     .           2017
B                              04/08/2019     .           2019
C                              05/09/2019    1           2019
;
data want;
 set have;
 by CUSTOMER_ID;
 dif=intck('year',lag(sale_date),sale_date,'c');
 if first.customer_id then dif=9999;
 if last.customer_id and dif&amp;gt;=5 then flag=1;
drop dif;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 07 May 2020 12:04:28 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2020-05-07T12:04:28Z</dc:date>
    <item>
      <title>How to flag a customer based on previous 5 yr rolling sales</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-a-customer-based-on-previous-5-yr-rolling-sales/m-p/645826#M193098</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I am trying to find for the year 2019 sales, if a customer has made a purchase in previous rolling 5 years by creating a flag variable (1 = No purchase in past 5 yrs 0 = Purchase in past 5 yrs).&amp;nbsp;&lt;/P&gt;&lt;P&gt;Year 2019&lt;/P&gt;&lt;P&gt;Cust A, time gap between last 2 transactions is more than 5 years , so flag is 1 in 2019.&lt;/P&gt;&lt;P&gt;Cust B&amp;nbsp;time gap between last 2 transactions is less than 5 years, so flag is zero&lt;/P&gt;&lt;P&gt;Cust C has made first and only transaction in 2019 so flag is 1&lt;/P&gt;&lt;P&gt;I have tried by using lag however it doesn't consider rolling five years&lt;/P&gt;&lt;P&gt;Input&lt;/P&gt;&lt;P&gt;CUSTOMER_ID&amp;nbsp; &amp;nbsp; &amp;nbsp; Sale_Date&amp;nbsp; &amp;nbsp; Flag&amp;nbsp; &amp;nbsp; &amp;nbsp; Year&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 02/03/2010&amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2010&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 03/05/2013&amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2013&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 01/11/2019&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2019&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 06/08/2017&amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2017&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 04/08/2019&amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2019&lt;/P&gt;&lt;P&gt;C&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 05/09/2019&amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2019&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SALE_DT1= INTNX('MONTH',Sale_Date,0,"BEGINNING");&lt;BR /&gt;MONTHS_SINCE = INTCK('MONTH', LAG(SALE_DT1),SALE_DT1);&lt;BR /&gt;IF CUSTOMER_ID ^= LAG(CUSTOMER_ID) THEN MONTHS_SINCE = .;&lt;BR /&gt;IF MISSING(MONTHS_SINCE) THEN FLAG = 1;&lt;BR /&gt;ELSE IF MONTHS_SINCE &amp;gt; 60 THEN FLAG =1;&lt;/P&gt;&lt;P&gt;ELSE FLAG =0;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 May 2020 11:40:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-a-customer-based-on-previous-5-yr-rolling-sales/m-p/645826#M193098</guid>
      <dc:creator>deep3</dc:creator>
      <dc:date>2020-05-07T11:40:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag a customer based on previous 5 yr rolling sales</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-a-customer-based-on-previous-5-yr-rolling-sales/m-p/645836#M193103</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input CUSTOMER_ID  $    Sale_Date : mmddyy10.;
format sale_date mmddyy10.;
cards;
A                              02/03/2010      .         2010
A                              03/05/2013      .         2013
A                              01/11/2019     1          2019
B                              06/08/2017     .           2017
B                              04/08/2019     .           2019
C                              05/09/2019    1           2019
;
data want;
 set have;
 by CUSTOMER_ID;
 dif=intck('year',lag(sale_date),sale_date,'c');
 if first.customer_id then dif=9999;
 if last.customer_id and dif&amp;gt;=5 then flag=1;
drop dif;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 May 2020 12:04:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-a-customer-based-on-previous-5-yr-rolling-sales/m-p/645836#M193103</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-05-07T12:04:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag a customer based on previous 5 yr rolling sales</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-a-customer-based-on-previous-5-yr-rolling-sales/m-p/645871#M193119</link>
      <description>&lt;P&gt;You can use a SQL self join to select, within ID, the row with the nearest prior date.&lt;/P&gt;
&lt;P&gt;I use the aliases EACH and SELF when doing self joins and further call it a 'triangular' join because the query contains an inequality operator (&amp;gt;) in the join criteria.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;data have;
input ID $ Date: mmddyy10.; format date mmddyy10.;
datalines;
A  02/03/2010
A  03/05/2013
A  01/11/2019
B  06/08/2017
B  04/08/2019
C  05/09/2019
;

proc sql;
  create table want as
  select 
    *
    , intck('month', first_prior, date) as months_apart
    , year(date) = 2019 and calculated months_apart not between 0 and 60 as y2019_5yr_flag
  from 
    ( 
        select 
          each.id
        , each.date
        , max(self.date) as first_prior format=mmddyy10.
        from 
          have as each
        left join 
          have as self
        on
          each.id = self.id
        &amp;amp; each.date &amp;gt; self.date
        group by 
          each.id, each.date
    ) as subselect 
  order by 
    id, date
  ;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="RichardADeVenezia_0-1588857944629.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/39161i04DCFD0FAF40DC7C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="RichardADeVenezia_0-1588857944629.png" alt="RichardADeVenezia_0-1588857944629.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 May 2020 13:26:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-a-customer-based-on-previous-5-yr-rolling-sales/m-p/645871#M193119</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-05-07T13:26:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag a customer based on previous 5 yr rolling sales</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-a-customer-based-on-previous-5-yr-rolling-sales/m-p/645872#M193120</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input CUSTOMER_ID $ Sale_Date :ddmmyy10.;
format sale_date yymmddd10.;
datalines;
A 02/03/2010
A 03/05/2013
A 01/11/2019
B 06/08/2017
B 04/08/2019
C 05/09/2019
;

data want;
set have;
by customer_id;
dif = intck('year',lag(sale_date),sale_date,'c');
if first.customer_id
then do;
  if last.customer_id then flag = 1;
end;
else if dif &amp;gt; 5 then flag = 1;
drop dif;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note how I presented the input dataset in a data step with datalines; this leaves no ambiguities about attributes and contents, and allows everybody else to recreate the dataset as-is with a simple copy/paste and submit.&lt;/P&gt;</description>
      <pubDate>Thu, 07 May 2020 13:27:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-a-customer-based-on-previous-5-yr-rolling-sales/m-p/645872#M193120</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-05-07T13:27:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to flag a customer based on previous 5 yr rolling sales</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flag-a-customer-based-on-previous-5-yr-rolling-sales/m-p/646120#M193241</link>
      <description>&lt;P&gt;Thank you sir. This works and the run time is very optimal&lt;/P&gt;</description>
      <pubDate>Fri, 08 May 2020 07:31:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flag-a-customer-based-on-previous-5-yr-rolling-sales/m-p/646120#M193241</guid>
      <dc:creator>deep3</dc:creator>
      <dc:date>2020-05-08T07:31:24Z</dc:date>
    </item>
  </channel>
</rss>

