<?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 Finding if customer has already products in last 2 months in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Finding-if-customer-has-already-products-in-last-2-months/m-p/765618#M30568</link>
    <description>&lt;P&gt;Hi everyone, hope to find you well!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a table called "policies" that has the following data:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Date_begin_policy  Customer_id   Policy_id
01-09-2019             A            111
02-10-2019             A            123
09-07-2019             A            126&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;and i want to build 2 columns "has_policy_month_before" and "has_policy_month_less2" that for each date_begin_policy checks if customer has already other policies in&amp;nbsp;date_begin_policy -1month and&amp;nbsp;date_begin_policy - 2 month:&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;Date_begin_policy  Customer_id   Policy_id  has_policy_month_before   has_policy_month_less2
01-09-2019             A             111           no                         yes
02-10-2019             A             123           yes                        no
09-07-2019             A             126           no                         no&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;I would do it like this in Teradata sql:&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;select t1.customer_id
         , t1.policy_id
         , t1.date_begin_policy
         , case
             when trunc(t2.date_begin_policy, 'mm') &amp;gt;= add_months(trunc(t1.date_begin_policy, 'mm'), -1)
             then 'yes'
             else 'no'
           end as has_policy_month_before
         , case
             when trunc(t2.date_begin_policy, 'mm') &amp;lt;  add_months(trunc(t1.date_begin_policy, 'mm'), -1)
             then 'yes'
             else 'no'
           end as has_policy_month_less2
      from mvt_data as t1
 left join mvt_data as t2  on t2.customer_id                     = t1.customer_id
                          and trunc(t2.date_begin_policy, 'mm') &amp;gt;= add_months(trunc(t1.date_begin_policy, 'mm'), -2)
                          and trunc(t2.date_begin_policy, 'mm') &amp;lt;  trunc(t1.date_begin_policy, 'mm')
      where t1.customer_id = 'A'
   order by t1.policy_id asc;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;but the problem is the add_months function and the trunc function are not like this in SAS. Could anyone please help me?&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please note i really would like to do it in sql and not sas syntax preferably.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 02 Sep 2021 12:37:07 GMT</pubDate>
    <dc:creator>msf2021</dc:creator>
    <dc:date>2021-09-02T12:37:07Z</dc:date>
    <item>
      <title>Finding if customer has already products in last 2 months</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Finding-if-customer-has-already-products-in-last-2-months/m-p/765618#M30568</link>
      <description>&lt;P&gt;Hi everyone, hope to find you well!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a table called "policies" that has the following data:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Date_begin_policy  Customer_id   Policy_id
01-09-2019             A            111
02-10-2019             A            123
09-07-2019             A            126&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;and i want to build 2 columns "has_policy_month_before" and "has_policy_month_less2" that for each date_begin_policy checks if customer has already other policies in&amp;nbsp;date_begin_policy -1month and&amp;nbsp;date_begin_policy - 2 month:&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;Date_begin_policy  Customer_id   Policy_id  has_policy_month_before   has_policy_month_less2
01-09-2019             A             111           no                         yes
02-10-2019             A             123           yes                        no
09-07-2019             A             126           no                         no&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;I would do it like this in Teradata sql:&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;select t1.customer_id
         , t1.policy_id
         , t1.date_begin_policy
         , case
             when trunc(t2.date_begin_policy, 'mm') &amp;gt;= add_months(trunc(t1.date_begin_policy, 'mm'), -1)
             then 'yes'
             else 'no'
           end as has_policy_month_before
         , case
             when trunc(t2.date_begin_policy, 'mm') &amp;lt;  add_months(trunc(t1.date_begin_policy, 'mm'), -1)
             then 'yes'
             else 'no'
           end as has_policy_month_less2
      from mvt_data as t1
 left join mvt_data as t2  on t2.customer_id                     = t1.customer_id
                          and trunc(t2.date_begin_policy, 'mm') &amp;gt;= add_months(trunc(t1.date_begin_policy, 'mm'), -2)
                          and trunc(t2.date_begin_policy, 'mm') &amp;lt;  trunc(t1.date_begin_policy, 'mm')
      where t1.customer_id = 'A'
   order by t1.policy_id asc;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;but the problem is the add_months function and the trunc function are not like this in SAS. Could anyone please help me?&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please note i really would like to do it in sql and not sas syntax preferably.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Sep 2021 12:37:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Finding-if-customer-has-already-products-in-last-2-months/m-p/765618#M30568</guid>
      <dc:creator>msf2021</dc:creator>
      <dc:date>2021-09-02T12:37:07Z</dc:date>
    </item>
    <item>
      <title>Re: Finding if customer has already products in last 2 months</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Finding-if-customer-has-already-products-in-last-2-months/m-p/765630#M30569</link>
      <description>&lt;PRE&gt;data have;
input Date_begin_policy : ddmmyy12.  Customer_id  $ Policy_id;
format Date_begin_policy ddmmyy10.;
cards;
01-09-2019             A            111
02-10-2019             A            123
09-07-2019             A            126
;
run;

proc sql;
create table want as
select *,
exists(select * from have where Customer_id=a.Customer_id  and 
Date_begin_policy between intnx('month',a.Date_begin_policy,-1) and a.Date_begin_policy-1) as has_policy_month_before,
exists(select * from have where Customer_id=a.Customer_id  and 
Date_begin_policy between intnx('month',a.Date_begin_policy,-2) and intnx('month',a.Date_begin_policy,-1)-1) as has_policy_month_less2

 from have as a;
quit;&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 Sep 2021 13:34:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Finding-if-customer-has-already-products-in-last-2-months/m-p/765630#M30569</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-09-02T13:34:59Z</dc:date>
    </item>
    <item>
      <title>Re: Finding if customer has already products in last 2 months</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Finding-if-customer-has-already-products-in-last-2-months/m-p/765643#M30572</link>
      <description>&lt;P&gt;Thank you!! What if i want also less 3 months?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Sep 2021 14:01:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Finding-if-customer-has-already-products-in-last-2-months/m-p/765643#M30572</guid>
      <dc:creator>msf2021</dc:creator>
      <dc:date>2021-09-02T14:01:52Z</dc:date>
    </item>
    <item>
      <title>Re: Finding if customer has already products in last 2 months</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Finding-if-customer-has-already-products-in-last-2-months/m-p/765831#M30588</link>
      <description>&lt;P&gt;OK. Here is .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
input Date_begin_policy : ddmmyy12.  Customer_id  $ Policy_id;
format Date_begin_policy ddmmyy10.;
cards;
01-09-2019             A            111
02-10-2019             A            123
09-07-2019             A            126
;
run;

proc sql;
create table want as
select *,
exists(select * from have where Customer_id=a.Customer_id  and 
Date_begin_policy between intnx('month',a.Date_begin_policy,-1) and a.Date_begin_policy-1) as has_policy_month_before,
exists(select * from have where Customer_id=a.Customer_id  and 
Date_begin_policy between intnx('month',a.Date_begin_policy,-2) and intnx('month',a.Date_begin_policy,-1)-1) as has_policy_month_less2,
exists(select * from have where Customer_id=a.Customer_id  and 
Date_begin_policy between intnx('month',a.Date_begin_policy,-3) and intnx('month',a.Date_begin_policy,-2)-1) as has_policy_month_less3


 from have as a;
quit;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Sep 2021 11:44:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Finding-if-customer-has-already-products-in-last-2-months/m-p/765831#M30588</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-09-03T11:44:22Z</dc:date>
    </item>
  </channel>
</rss>

