<?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: Merge data by date range in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-by-date-range/m-p/897757#M354816</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/455700"&gt;@Alice_SAS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Yes, that situation exists. If the &lt;SPAN&gt;two separate date ranges&lt;/SPAN&gt; don't overlap, I prefer not to merge them and leave them as they are.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Please post data containing this case and post the data in usable form.&lt;/P&gt;</description>
    <pubDate>Mon, 09 Oct 2023 06:30:38 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2023-10-09T06:30:38Z</dc:date>
    <item>
      <title>Merge data by date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-by-date-range/m-p/896600#M354283</link>
      <description>&lt;P&gt;I have the table like:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;source&lt;/TD&gt;&lt;TD&gt;target&lt;/TD&gt;&lt;TD&gt;start&lt;/TD&gt;&lt;TD&gt;end&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;X&lt;/TD&gt;&lt;TD&gt;20050106&lt;/TD&gt;&lt;TD&gt;20070506&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;Y&lt;/TD&gt;&lt;TD&gt;20030601&lt;/TD&gt;&lt;TD&gt;20060704&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;Y&lt;/TD&gt;&lt;TD&gt;20060704&lt;/TD&gt;&lt;TD&gt;20080905&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;Y&lt;/TD&gt;&lt;TD&gt;20080905&lt;/TD&gt;&lt;TD&gt;20091211&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;P&lt;/TD&gt;&lt;TD&gt;20040704&lt;/TD&gt;&lt;TD&gt;20080905&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;P&lt;/TD&gt;&lt;TD&gt;20061001&lt;/TD&gt;&lt;TD&gt;20100806&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;TD&gt;L&lt;/TD&gt;&lt;TD&gt;20070911&lt;/TD&gt;&lt;TD&gt;20081120&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;I hope to merge the time range of the target corresponding to each source, and&amp;nbsp;get this table:&lt;/P&gt;&lt;P&gt;Can you help me? Thank you!&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;source&lt;/TD&gt;&lt;TD&gt;target&lt;/TD&gt;&lt;TD&gt;start&lt;/TD&gt;&lt;TD&gt;end&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;X&lt;/TD&gt;&lt;TD&gt;20050106&lt;/TD&gt;&lt;TD&gt;20070506&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;Y&lt;/TD&gt;&lt;TD&gt;20030601&lt;/TD&gt;&lt;TD&gt;20091211&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;P&lt;/TD&gt;&lt;TD&gt;20040704&lt;/TD&gt;&lt;TD&gt;20100806&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;TD&gt;L&lt;/TD&gt;&lt;TD&gt;20070911&lt;/TD&gt;&lt;TD&gt;20081120&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Mon, 02 Oct 2023 02:28:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-by-date-range/m-p/896600#M354283</guid>
      <dc:creator>Alice_SAS</dc:creator>
      <dc:date>2023-10-02T02:28:37Z</dc:date>
    </item>
    <item>
      <title>Re: Merge data by date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-by-date-range/m-p/896606#M354287</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
  select
    source,
    target,
    min(start) as start,
    max(end) as end
  from have
  group by source, target
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 02 Oct 2023 05:43:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-by-date-range/m-p/896606#M354287</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-10-02T05:43:51Z</dc:date>
    </item>
    <item>
      <title>Re: Merge data by date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-by-date-range/m-p/896607#M354288</link>
      <description>&lt;P&gt;Could be solved by a data step, too, but i don't think, that it is &lt;EM&gt;better&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;Just to clarify: you don want one obs for each source/target combination with the earliest start date and the latest end date?&lt;/P&gt;</description>
      <pubDate>Mon, 02 Oct 2023 06:27:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-by-date-range/m-p/896607#M354288</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2023-10-02T06:27:40Z</dc:date>
    </item>
    <item>
      <title>Re: Merge data by date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-by-date-range/m-p/896617#M354294</link>
      <description>Is it possible that a SOURCE/TARGET might contain two separate date ranges that do not overlap?  If so, what should the result  be?&lt;BR /&gt;</description>
      <pubDate>Mon, 02 Oct 2023 08:50:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-by-date-range/m-p/896617#M354294</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-10-02T08:50:54Z</dc:date>
    </item>
    <item>
      <title>Re: Merge data by date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-by-date-range/m-p/897753#M354813</link>
      <description>&lt;P&gt;Yes, that situation exists. If the &lt;SPAN&gt;two separate date ranges&lt;/SPAN&gt; don't overlap, I prefer not to merge them and leave them as they are.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2023 05:54:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-by-date-range/m-p/897753#M354813</guid>
      <dc:creator>Alice_SAS</dc:creator>
      <dc:date>2023-10-09T05:54:47Z</dc:date>
    </item>
    <item>
      <title>Re: Merge data by date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-by-date-range/m-p/897754#M354814</link>
      <description>&lt;P&gt;For time ranges that are connected or overlap, I want to save the start and end times. For non-overlapping times, I don't want to merge them.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2023 05:58:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-by-date-range/m-p/897754#M354814</guid>
      <dc:creator>Alice_SAS</dc:creator>
      <dc:date>2023-10-09T05:58:31Z</dc:date>
    </item>
    <item>
      <title>Re: Merge data by date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-by-date-range/m-p/897755#M354815</link>
      <description>&lt;P&gt;Thanks for your answer. But I forgot one situation. I don't want to merge non-overlapping times. For example, what should I do in the following situation?&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;source&lt;/TD&gt;&lt;TD&gt;target&lt;/TD&gt;&lt;TD&gt;start&lt;/TD&gt;&lt;TD&gt;end&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;D&lt;/TD&gt;&lt;TD&gt;M&lt;/TD&gt;&lt;TD&gt;20030507&lt;/TD&gt;&lt;TD&gt;20040506&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;D&lt;/TD&gt;&lt;TD&gt;M&lt;/TD&gt;&lt;TD&gt;20050809&lt;/TD&gt;&lt;TD&gt;20071001&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;In this situation, I don't want to do any merge, what should I do? Thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2023 06:04:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-by-date-range/m-p/897755#M354815</guid>
      <dc:creator>Alice_SAS</dc:creator>
      <dc:date>2023-10-09T06:04:52Z</dc:date>
    </item>
    <item>
      <title>Re: Merge data by date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-by-date-range/m-p/897757#M354816</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/455700"&gt;@Alice_SAS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Yes, that situation exists. If the &lt;SPAN&gt;two separate date ranges&lt;/SPAN&gt; don't overlap, I prefer not to merge them and leave them as they are.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Please post data containing this case and post the data in usable form.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2023 06:30:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-by-date-range/m-p/897757#M354816</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2023-10-09T06:30:38Z</dc:date>
    </item>
    <item>
      <title>Re: Merge data by date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-by-date-range/m-p/897764#M354817</link>
      <description>&lt;P&gt;I have the data:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data have;&lt;BR /&gt;input source:$1. target:$1. start: YYMMDD8. end: YYMMDD8.;&lt;BR /&gt;format start YYMMDDn8. end YYMMDDn8.;&lt;BR /&gt;datalines;&lt;BR /&gt;A X 20050106 20070506&lt;BR /&gt;B Y 20030601 20060704&lt;BR /&gt;B Y 20060704 20080905&lt;BR /&gt;B Y 20080905 20091211&lt;BR /&gt;B P 20040704 20080905&lt;BR /&gt;B P 20061001 20100806&lt;BR /&gt;C L 20070911 20081120&lt;BR /&gt;D M 20030507 20040506&lt;BR /&gt;D M 20050809 20071001&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to obtain the form:&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;input source:$1. target:$1. start: YYMMDD8. end: YYMMDD8.;&lt;BR /&gt;format start YYMMDDn8. end YYMMDDn8.;&lt;BR /&gt;datalines;&lt;BR /&gt;A X 20050106 20070506&lt;BR /&gt;B Y 20030601 20091211&lt;BR /&gt;B P 20040704 20100806&lt;BR /&gt;C L 20070911 20081120&lt;BR /&gt;D M 20030507 20040506&lt;BR /&gt;D M 20050809 20071001&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2023 07:58:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-by-date-range/m-p/897764#M354817</guid>
      <dc:creator>Alice_SAS</dc:creator>
      <dc:date>2023-10-09T07:58:26Z</dc:date>
    </item>
    <item>
      <title>Re: Merge data by date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-by-date-range/m-p/897766#M354818</link>
      <description>&lt;P&gt;For the sample data you shared something like below should work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input source:$1. target:$1. start: YYMMDD8. end: YYMMDD8.;
  format start YYMMDDn8. end YYMMDDn8.;
  datalines;
A X 20050106 20070506
B Y 20030601 20060704
B Y 20060704 20080905
B Y 20080905 20091211
B P 20040704 20080905
B P 20061001 20100806
C L 20070911 20081120
D M 20030507 20040506
D M 20050809 20071001
;
run;

data want;
  input source:$1. target:$1. start: YYMMDD8. end: YYMMDD8.;
  format start YYMMDDn8. end YYMMDDn8.;
  datalines;
A X 20050106 20070506
B Y 20030601 20091211
B P 20040704 20100806
C L 20070911 20081120
D M 20030507 20040506
D M 20050809 20071001
;
run;

proc sort data=have out=have_sorted;
  by source target start end;
run;

data want2;
  set have_sorted;
  by source target start end;

  _next_obs=min(_n_+1,_nobs);
  set have_sorted(keep=start rename=(start=_next_start)) point=_next_obs nobs=_nobs;
  if last.target or end&amp;lt;_next_start then output;
  drop _next_start;
run;

proc print data=want2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2023 08:43:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-by-date-range/m-p/897766#M354818</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-10-09T08:43:34Z</dc:date>
    </item>
    <item>
      <title>Re: Merge data by date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-by-date-range/m-p/897774#M354820</link>
      <description>&lt;P&gt;Thank you for your help. However, I found an error in the output of your code regarding the results of B Y 20030601&amp;nbsp; 20091211 and B P 20040704 20100806 in the Want table I provided.&amp;nbsp;How can I modify the code？&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2023 09:59:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-by-date-range/m-p/897774#M354820</guid>
      <dc:creator>Alice_SAS</dc:creator>
      <dc:date>2023-10-09T09:59:32Z</dc:date>
    </item>
    <item>
      <title>Re: Merge data by date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-by-date-range/m-p/897778#M354822</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input source:$1. target:$1. start: YYMMDD8. end: YYMMDD8.;
  format start YYMMDDn8. end YYMMDDn8.;
  datalines;
A X 20050106 20070506
B Y 20030601 20060704
B Y 20060704 20080905
B Y 20080905 20091211
B P 20040704 20080905
B P 20061001 20100806
C L 20070911 20081120
D M 20030507 20040506
D M 20050809 20071001
;

data want;
set have end=done;
set
  have (
    firstobs=2
    keep=source target start
    rename=(source=_source target=_target start=_start)
  )
  have (
    obs=1
    keep=source target start
    rename=(source=_source target=_target start=_start)
  )
;
*by source target;
retain __start;
if _n_ = 1 then __start = start;
*if first.source then __start = start;
if
  source ne _source or target ne _target 
  or _start &amp;gt; end + 1
  or done
then do;
  start = __start;
  output;
  __start = _start;
end;
drop _:;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The output:&lt;/P&gt;
&lt;PRE&gt;source	target	start	end
A	X	20050106	20070506
B	Y	20030601	20091211
B	P	20040704	20100806
C	L	20070911	20081120
D	M	20030507	20040506
D	M	20050809	20071001
&lt;/PRE&gt;
&lt;P&gt;matches your "want" example.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2023 10:37:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-by-date-range/m-p/897778#M354822</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-10-09T10:37:15Z</dc:date>
    </item>
    <item>
      <title>Re: Merge data by date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-by-date-range/m-p/897851#M354864</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set have (keep=source target);
  by source target  notsorted;

  merge have (rename=(start=_current_start))
        have (firstobs=2 keep=start rename=(start=_nxt_start));

  retain start ;  
  format start yymmddn8. ;

  if first.target=1 or _current_start-1&amp;gt;lag(end) then start=_current_start;
  if last.target=1  or end+1&amp;lt;_nxt_start;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The SET and MERGE statement set up two streams of data, read in parallel.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first SET is accompanied by the BY statement, allowing use of &lt;EM&gt;&lt;STRONG&gt;first.target&lt;/STRONG&gt;&lt;/EM&gt; and &lt;EM&gt;&lt;STRONG&gt;last.target&lt;/STRONG&gt;&lt;/EM&gt; dummies.&amp;nbsp; The data are assumed to be grouped by source*target, and within each source*target group, to be sorted by start.&amp;nbsp; (and also sorted by END within each group).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The MERGE statement has two "substreams" of data, also read in parallel, but because of the FIRSTOBS=2 option provides a way to look ahead at the upcoming START value (in variable _nxt_start), so upcoming date gaps can easily be found.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first IF identifies the beginning of a continuous sequence of observations.&lt;/P&gt;
&lt;P&gt;The second if identified the end of same.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2023 18:26:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-by-date-range/m-p/897851#M354864</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-10-09T18:26:08Z</dc:date>
    </item>
  </channel>
</rss>

