<?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: Summarise Events with Intersecting Dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Summarise-Events-with-Intersecting-Dates/m-p/795601#M255220</link>
    <description>&lt;P&gt;What if a pair of date ranges don't intersect, but are contiguos?&amp;nbsp; I.e. what if you have these two start/finish pairs?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="50%"&gt;Start&lt;/TD&gt;
&lt;TD width="50%"&gt;Finish&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%"&gt;05jan2022&lt;/TD&gt;
&lt;TD width="50%"&gt;12jan2022&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%"&gt;13jan2022&lt;/TD&gt;
&lt;TD width="50%"&gt;20jan2022&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Should these remain as two distinct events, or should they be joined as a single event from 05jan2022 through 20jan2022?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is your naming convention for merged events?&lt;/P&gt;</description>
    <pubDate>Fri, 11 Feb 2022 05:43:00 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2022-02-11T05:43:00Z</dc:date>
    <item>
      <title>Summarise Events with Intersecting Dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summarise-Events-with-Intersecting-Dates/m-p/795575#M255203</link>
      <description>&lt;P&gt;I have a dataset which contains many events that contains clients records with start and end dates.&lt;/P&gt;&lt;P&gt;Whole events should be those from clients where there are intersecting dates from the same suppliers.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;
    input event:$ client:$ supplier:$ start:date9.  finish:date9. spend @@@@@;
	format start date9. finish date9.;
    datalines;
    001 x1 SU1 01JAN2022 10JAN2022 10
	002 y2 SU3 21DEC2021 22DEC2021 50
	003 x1 SU1 05JAN2022 10JAN2022 5
	004 x1 SU1 31DEC2021 11JAN2022 10
	005 z3 SU2 01JAN2022 05JAN2022 15
	006 x1 SU1 13JAN2022 15JAN2022 6
    ;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This is the data I want.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 420px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/68382i845733DCE0771394/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;For example client x1 has 3 events that intersect dates and one that does not.&lt;/P&gt;&lt;P&gt;I also need to sum the spend and also make a note in the event ID that I've combined multiple events into one.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Feb 2022 23:50:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summarise-Events-with-Intersecting-Dates/m-p/795575#M255203</guid>
      <dc:creator>cobba</dc:creator>
      <dc:date>2022-02-10T23:50:32Z</dc:date>
    </item>
    <item>
      <title>Re: Summarise Events with Intersecting Dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summarise-Events-with-Intersecting-Dates/m-p/795595#M255216</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input event:$ client:$ supplier:$ start:date9.  finish:date9. spend @@@@@;
	format start date9. finish date9.;
    datalines;
    001 x1 SU1 01JAN2022 10JAN2022 10
	002 y2 SU3 21DEC2021 22DEC2021 50
	003 x1 SU1 05JAN2022 10JAN2022 5
	004 x1 SU1 31DEC2021 11JAN2022 10
	005 z3 SU2 01JAN2022 05JAN2022 15
	006 x1 SU1 13JAN2022 15JAN2022 6
    ;
run;

data temp;
set have;
do date=start to finish;
 output;
end;
format date date9.;
drop start finish;
run;

proc sort data=temp;by client supplier date;run;
data temp2;
sum=0;
do until(last.date);
 set temp;
 by client supplier date;
 length events $ 200;
 events=catx(',',events,event);
 sum+spend;
end;
drop event spend;
run;
data temp3;
 set temp2;
 by client supplier;
 if first.supplier or dif(date) ne 1 then group+1;
run;
proc sql;
create table want as
select group,
 (select distinct events from temp3 where group=a.group having length(events)=max(length(events))) as event,
  max(client) as client,max(supplier) as supplier,
  min(date) as start format=date9.,max(date) as finish format=date9.,max(sum) as spend
 from temp3 as a
  group by group;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Feb 2022 04:55:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summarise-Events-with-Intersecting-Dates/m-p/795595#M255216</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-02-11T04:55:23Z</dc:date>
    </item>
    <item>
      <title>Re: Summarise Events with Intersecting Dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summarise-Events-with-Intersecting-Dates/m-p/795601#M255220</link>
      <description>&lt;P&gt;What if a pair of date ranges don't intersect, but are contiguos?&amp;nbsp; I.e. what if you have these two start/finish pairs?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="50%"&gt;Start&lt;/TD&gt;
&lt;TD width="50%"&gt;Finish&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%"&gt;05jan2022&lt;/TD&gt;
&lt;TD width="50%"&gt;12jan2022&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%"&gt;13jan2022&lt;/TD&gt;
&lt;TD width="50%"&gt;20jan2022&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Should these remain as two distinct events, or should they be joined as a single event from 05jan2022 through 20jan2022?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is your naming convention for merged events?&lt;/P&gt;</description>
      <pubDate>Fri, 11 Feb 2022 05:43:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summarise-Events-with-Intersecting-Dates/m-p/795601#M255220</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-02-11T05:43:00Z</dc:date>
    </item>
    <item>
      <title>Re: Summarise Events with Intersecting Dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summarise-Events-with-Intersecting-Dates/m-p/795620#M255225</link>
      <description>&lt;P&gt;Seems to do want you want, but needs tweaking depending on your answers to the questions asked by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have out=sorted;
   by client start;
run;


data want;
   set sorted;
   by client;
   
   length first_event $ 3;
   
   retain first_start first_finish first_event;    
   format first_start first_finish date9.;
   
   if first.client then do;
      sum_spend = 0;
      first_start = start;
      first_finish = finish;
      first_event = event;
   end;
   
   if event &amp;lt; first_event then do;
      first_event = event;
   end;

   if start &amp;lt; first_finish then do;
      sum_spend + spend;
   end;
   else do;
      substr(first_event, 1, 1) = 'M';   
      output;
      sum_spend = spend;
      first_start = start;
      first_event = event;
   end;
   
   if last.client then do;
      first_finish = finish;
      output;
   end;
   
   drop event start finish spend;
   rename 
      first_event = event
      first_start = start
      first_finish = finish
      sum_spend = spend
   ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Feb 2022 08:43:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summarise-Events-with-Intersecting-Dates/m-p/795620#M255225</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-02-11T08:43:49Z</dc:date>
    </item>
  </channel>
</rss>

