<?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 How to cut the contract period if two contracts overlap in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-cut-the-contract-period-if-two-contracts-overlap/m-p/726738#M225871</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I have data with the duration of the contract.&amp;nbsp;However, some data is unstructured and date contracts overlap.&amp;nbsp;In these cases, I would like the term of the contract to be in line with other contracts.&amp;nbsp;Below is an example of what the data looks like:&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;ID&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;START_DATE&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;STOP_DATE&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;1&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2019-01-01&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2019-12-31&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;1&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2020-01-01&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2020-12-31&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;2&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2020-01-01&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2020-10-31&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;2&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2020-01-07&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2021-12-31&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;2&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2020-09-01&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2022-12-31&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;ID 1 is shown as valid. I would like to transform ID 2 into the following form:&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;ID&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;START_DATE&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;STOP_DATE&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;1&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2019-01-01&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2019-12-31&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;1&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2020-01-01&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2020-12-31&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;2&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2020-01-01&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2020-01-06&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;2&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2020-01-07&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2020-08-31&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;2&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2020-09-01&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2022-12-31&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;I know the basics of SAS and I don't know how to solve this task. Can You help me?&lt;/P&gt;</description>
    <pubDate>Tue, 16 Mar 2021 13:18:10 GMT</pubDate>
    <dc:creator>PatrykSAS</dc:creator>
    <dc:date>2021-03-16T13:18:10Z</dc:date>
    <item>
      <title>How to cut the contract period if two contracts overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-cut-the-contract-period-if-two-contracts-overlap/m-p/726738#M225871</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I have data with the duration of the contract.&amp;nbsp;However, some data is unstructured and date contracts overlap.&amp;nbsp;In these cases, I would like the term of the contract to be in line with other contracts.&amp;nbsp;Below is an example of what the data looks like:&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;ID&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;START_DATE&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;STOP_DATE&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;1&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2019-01-01&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2019-12-31&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;1&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2020-01-01&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2020-12-31&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;2&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2020-01-01&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2020-10-31&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;2&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2020-01-07&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2021-12-31&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;2&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2020-09-01&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2022-12-31&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;ID 1 is shown as valid. I would like to transform ID 2 into the following form:&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;ID&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;START_DATE&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;STOP_DATE&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;1&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2019-01-01&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2019-12-31&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;1&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2020-01-01&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2020-12-31&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;2&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2020-01-01&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2020-01-06&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;2&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2020-01-07&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2020-08-31&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;2&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2020-09-01&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;2022-12-31&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;I know the basics of SAS and I don't know how to solve this task. Can You help me?&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 13:18:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-cut-the-contract-period-if-two-contracts-overlap/m-p/726738#M225871</guid>
      <dc:creator>PatrykSAS</dc:creator>
      <dc:date>2021-03-16T13:18:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to cut the contract period if two contracts overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-cut-the-contract-period-if-two-contracts-overlap/m-p/726765#M225882</link>
      <description>&lt;P&gt;Can you please explain your business logic in more detail?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 14:02:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-cut-the-contract-period-if-two-contracts-overlap/m-p/726765#M225882</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2021-03-16T14:02:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to cut the contract period if two contracts overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-cut-the-contract-period-if-two-contracts-overlap/m-p/726767#M225884</link>
      <description>&lt;P&gt;You want to modify the current stop_date if the following start_date overlaps:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=nxt_:) ;
  set have (keep=id);
  by id;
  merge have  have (firstobs=2 keep=start_date rename=(start_date=nxt_start));
  if last.id=0 then stop_date=min(stop_date,nxt_start-1);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Mar 2021 14:05:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-cut-the-contract-period-if-two-contracts-overlap/m-p/726767#M225884</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-03-16T14:05:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to cut the contract period if two contracts overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-cut-the-contract-period-if-two-contracts-overlap/m-p/726768#M225885</link>
      <description>&lt;P&gt;Here is a method for looking ahead to see the value of START_DATE on the next observation.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have ;
  by id start_date stop_date ;
  set have(keep=start_date rename=(start_date=next_date) firstobs=2) 
      have(obs=1 drop=_all_)
  ;
  if last.id then next_date=.;
  new_stop = min(stop_date,next_date-1);
  format new_stop yymmdd10.;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs    ID    START_DATE     STOP_DATE     next_date      new_stop

 1      1    2019-01-01    2019-12-31    2020-01-01    2019-12-31
 2      1    2020-01-01    2020-12-31             .    2020-12-31
 3      2    2020-01-01    2020-10-31    2020-01-07    2020-01-06
 4      2    2020-01-07    2021-12-31    2020-09-01    2020-08-31
 5      2    2020-09-01    2022-12-31             .    2022-12-31

&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Mar 2021 14:09:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-cut-the-contract-period-if-two-contracts-overlap/m-p/726768#M225885</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-03-16T14:09:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to cut the contract period if two contracts overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-cut-the-contract-period-if-two-contracts-overlap/m-p/726770#M225886</link>
      <description>The client with the id 2 has signed 3 annexes. The first one signed until 2020.10, another one by 2021.12 and another by 2022.12. Each annex has different contract terms. Now, for example, I would like to calculate this client's margin for 2020. If I used table number 1, I would count his margins three times, which is not true. The new annex should shorten the period of validity of the previous one. In this way, I will count several months of 2020 according to one contract, and the remaining months according to the terms of another annex.</description>
      <pubDate>Tue, 16 Mar 2021 14:09:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-cut-the-contract-period-if-two-contracts-overlap/m-p/726770#M225886</guid>
      <dc:creator>PatrykSAS</dc:creator>
      <dc:date>2021-03-16T14:09:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to cut the contract period if two contracts overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-cut-the-contract-period-if-two-contracts-overlap/m-p/726775#M225890</link>
      <description>&lt;P&gt;Next&amp;nbsp;code is tested:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile cards dlm = '09'x truncover;
  input ID Start_date yymmdd10. @+1 Stop_date yymmdd10.;
  format start_date stop_date yymmdd10.;
cards;
1	2019-01-01	2019-12-31
1	2020-01-01	2020-12-31
2	2020-01-01	2020-10-31
2	2020-01-07	2021-12-31
2	2020-09-01	2022-12-31
; run;

proc sort data=have; 
    by ID descending start_date descending stop_date; 
run;
data want;
 set have;
   by ID;
        prev_start = lag(start_date);
        if not first.ID and
           stop_date &amp;gt; prev_start
        then  stop_date = prev_start -1;
     drop prev_start;
run; 
proc sort data=want; 
    by ID start_date stop_date; 
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 14:20:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-cut-the-contract-period-if-two-contracts-overlap/m-p/726775#M225890</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-03-16T14:20:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to cut the contract period if two contracts overlap</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-cut-the-contract-period-if-two-contracts-overlap/m-p/726777#M225892</link>
      <description>It works! Thank you a lot &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Tue, 16 Mar 2021 14:25:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-cut-the-contract-period-if-two-contracts-overlap/m-p/726777#M225892</guid>
      <dc:creator>PatrykSAS</dc:creator>
      <dc:date>2021-03-16T14:25:55Z</dc:date>
    </item>
  </channel>
</rss>

