<?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: Calculate the number of new clients in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Calculate-the-number-of-new-clients/m-p/842703#M82214</link>
    <description>&lt;P&gt;Code like below should work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover;
  input ClientID $ ContactDate :ddmmyy10.;
  format ContactDate date9.;
datalines;
A001 01/11/2010
A001 02/11/2010
A001 04/11/2010
B001 05/06/2010
B001 05/06/2016
B001 07/06/2016
B001 08/06/2016
C001 08/02/2017
D001 01/03/2014
A001 03/11/2021
A001 04/11/2021
E001 02/04/2021
G001 
C001 02/06/2021
;

%let ref_p_start_dt=01Jan2021;
proc sql;
  select
    ClientID,
    min(ContactDate) as ref_p_first_ContactDate format=date9.
  from have o
  where 
    ContactDate&amp;gt;="&amp;amp;ref_p_start_dt"d 
    and not exists
    (
      /* customers with contact within 5 years prior to reference period start date */
      select * 
      from have i
      where 
        intnx('year',"&amp;amp;ref_p_start_dt"d,-5,'s')&amp;lt; ContactDate &amp;lt; "&amp;amp;ref_p_start_dt"d
        and o.clientID=i.ClientID
    )
  group by
    ClientID
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1667694230633.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/76987i9DF11E23EE29429B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1667694230633.png" alt="Patrick_0-1667694230633.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 06 Nov 2022 00:23:59 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2022-11-06T00:23:59Z</dc:date>
    <item>
      <title>Calculate the number of new clients</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculate-the-number-of-new-clients/m-p/842699#M82213</link>
      <description>Hi&lt;BR /&gt;&lt;BR /&gt;Below is a sample of the data.&lt;BR /&gt;&lt;BR /&gt;I am trying to get the number of new clients that is defined as clients who has not been seen in the 5 years preceding the first service contact during the reference period.&lt;BR /&gt;&lt;BR /&gt;For example, in 2021 there were two new clients A001 and E001 (within five years).&lt;BR /&gt;&lt;BR /&gt;Thank you for your help.&lt;BR /&gt;&lt;BR /&gt;ClientD ContactDate&lt;BR /&gt;A001 01/11/2010&lt;BR /&gt;A001 02/11/2010&lt;BR /&gt;A001 04/11/2010&lt;BR /&gt;B001 05/06/2016&lt;BR /&gt;B001 07/06/2016&lt;BR /&gt;B001 08/06/2016&lt;BR /&gt;C001 08/02/2017&lt;BR /&gt;D001 01/03/2014&lt;BR /&gt;A001 03/11/2021&lt;BR /&gt;A001 04/11/2021&lt;BR /&gt;E001 02/04/2021&lt;BR /&gt;G001 …… (missing)&lt;BR /&gt;C001 02/06/2021&lt;BR /&gt;</description>
      <pubDate>Sat, 05 Nov 2022 22:54:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculate-the-number-of-new-clients/m-p/842699#M82213</guid>
      <dc:creator>Sofia2022</dc:creator>
      <dc:date>2022-11-05T22:54:13Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate the number of new clients</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculate-the-number-of-new-clients/m-p/842703#M82214</link>
      <description>&lt;P&gt;Code like below should work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover;
  input ClientID $ ContactDate :ddmmyy10.;
  format ContactDate date9.;
datalines;
A001 01/11/2010
A001 02/11/2010
A001 04/11/2010
B001 05/06/2010
B001 05/06/2016
B001 07/06/2016
B001 08/06/2016
C001 08/02/2017
D001 01/03/2014
A001 03/11/2021
A001 04/11/2021
E001 02/04/2021
G001 
C001 02/06/2021
;

%let ref_p_start_dt=01Jan2021;
proc sql;
  select
    ClientID,
    min(ContactDate) as ref_p_first_ContactDate format=date9.
  from have o
  where 
    ContactDate&amp;gt;="&amp;amp;ref_p_start_dt"d 
    and not exists
    (
      /* customers with contact within 5 years prior to reference period start date */
      select * 
      from have i
      where 
        intnx('year',"&amp;amp;ref_p_start_dt"d,-5,'s')&amp;lt; ContactDate &amp;lt; "&amp;amp;ref_p_start_dt"d
        and o.clientID=i.ClientID
    )
  group by
    ClientID
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1667694230633.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/76987i9DF11E23EE29429B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1667694230633.png" alt="Patrick_0-1667694230633.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 06 Nov 2022 00:23:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculate-the-number-of-new-clients/m-p/842703#M82214</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-11-06T00:23:59Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate the number of new clients</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculate-the-number-of-new-clients/m-p/842705#M82215</link>
      <description>&lt;P&gt;Try next code -&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have out=temp;
   by ClientID Contact_date;
run;
data want;
  set temp;
   by ClientID;
      retain first_contact;
        if first.ClientID and last.ClientID then output;
        else do;
             if first.ClientID then first_contact = Contact_Date; else
             if intnx('year',first_contact,5, 'same') &amp;lt; Contact_date
                then output;
run;    &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;What if somone makes a contact &lt;STRONG&gt;once a year&lt;/STRONG&gt; for more then 5 years. Should he be considered as a new contact ? I think that the requirement since&amp;nbsp;&lt;STRONG&gt;first service &lt;/STRONG&gt;should be reconsidered.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 06 Nov 2022 01:24:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculate-the-number-of-new-clients/m-p/842705#M82215</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2022-11-06T01:24:02Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate the number of new clients</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculate-the-number-of-new-clients/m-p/842710#M82216</link>
      <description>&lt;P&gt;Your reference period is what exactly? I don't see a clear definition of any value for that.&lt;/P&gt;</description>
      <pubDate>Sun, 06 Nov 2022 07:06:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculate-the-number-of-new-clients/m-p/842710#M82216</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-11-06T07:06:50Z</dc:date>
    </item>
  </channel>
</rss>

