<?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: use data step to merge table with overlapping date ranges to granular table with one date per ro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/use-data-step-to-merge-table-with-overlapping-date-ranges-to/m-p/557317#M155341</link>
    <description>&lt;P&gt;Why not SQL:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table want as
   select a.*, b.* 
   from   dist_periods as a, temp_actuals as b
      where  a.period_start_date le  b.day le a.run_date
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;Of course if you have large data that may take&amp;nbsp;some time as it is not optimized in any way.&lt;/P&gt;</description>
    <pubDate>Wed, 08 May 2019 22:58:39 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2019-05-08T22:58:39Z</dc:date>
    <item>
      <title>use data step to merge table with overlapping date ranges to granular table with one date per row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/use-data-step-to-merge-table-with-overlapping-date-ranges-to/m-p/557315#M155339</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*1*/
data temp_actuals;
	format day date8.;
	do day= 21185 to 21243;
		temp=35+rand('uniform')*10;
		output;
	end;
run;
	
/*2*/
data dist_periods (drop=counter);
	ID=0;
	format 
		period_start_date date8.
		run_date date8.
		period_end_date date8.;
	do period_start_date= 21212 to 21214 by 2;	
		run_date=period_start_date+14;
		period_end_date=period_start_date+29;
		if _N_ = 1 then counter =1;
		id+counter;
		output;
	end;
run;

/*3*/
proc sql;
	create table comb1 as
		select 
			t2.*,
			t1.*
		from 
			temp_actuals t1, 
			(select * from dist_periods where period_start_date=21212) t2
		where 
			t1.day &amp;gt;= t2.period_start_date and
			t1.day &amp;lt;= t2.run_date
	;

/*4*/
	proc sql;
	create table comb2 as
		select 
			t2.*,
			t1.*
		from 
			temp_actuals t1, 
			(select * from dist_periods where period_start_date=21214) t2
		where 
			t1.day &amp;gt;= t2.period_start_date and
			t1.day &amp;lt;= t2.run_date
	;

/*5*/
data combined;
	set comb1 comb2;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;See the 5 steps numbered and commented out in the code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to merge&amp;nbsp;&lt;CODE class=" language-sas"&gt;temp_actuals&amp;nbsp;&lt;/CODE&gt;and &lt;CODE class=" language-sas"&gt;dist_periods&amp;nbsp;&lt;/CODE&gt;into &lt;CODE class=" language-sas"&gt;combined&amp;nbsp;&lt;/CODE&gt;in one&amp;nbsp; data step without using a macro (there are a few reasons for avoiding macro that I don't want to get into, please don't provide a solution like that, I'm 99% sure this can be done in a data step).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To resummarize, i want to combine the data in step 1 with the data in step 2 to get the data in step 5.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Although the example has 2 rows, the real &lt;CODE class=" language-sas"&gt;dist_periods&amp;nbsp;&lt;/CODE&gt; file can have any number of rows in the hundreds, so writing out joins for every iteration of &lt;CODE class=" language-sas"&gt;dist_periods&amp;nbsp;&lt;/CODE&gt;is not feasible.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's me trying to describe the routine, probably not that helpful:&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;for each row 1 to n in dist_periods,&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;join each dist_periods row to every row in temp_actuals that is within the date range in dist_periods&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;repeat process for row 2 to n of dist_periods&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sorry I can't be more descriptive, but that's why google has failed me, i don't know how to describe it well but the code should do the trick. I'm trying to be a better data step programmer; leaning in proc sql is an easy way to forget Programming II class.&lt;/P&gt;</description>
      <pubDate>Wed, 08 May 2019 22:40:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/use-data-step-to-merge-table-with-overlapping-date-ranges-to/m-p/557315#M155339</guid>
      <dc:creator>sshockl</dc:creator>
      <dc:date>2019-05-08T22:40:25Z</dc:date>
    </item>
    <item>
      <title>Re: use data step to merge table with overlapping date ranges to granular table with one date per ro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/use-data-step-to-merge-table-with-overlapping-date-ranges-to/m-p/557317#M155341</link>
      <description>&lt;P&gt;Why not SQL:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table want as
   select a.*, b.* 
   from   dist_periods as a, temp_actuals as b
      where  a.period_start_date le  b.day le a.run_date
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;Of course if you have large data that may take&amp;nbsp;some time as it is not optimized in any way.&lt;/P&gt;</description>
      <pubDate>Wed, 08 May 2019 22:58:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/use-data-step-to-merge-table-with-overlapping-date-ranges-to/m-p/557317#M155341</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-05-08T22:58:39Z</dc:date>
    </item>
    <item>
      <title>Re: use data step to merge table with overlapping date ranges to granular table with one date per ro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/use-data-step-to-merge-table-with-overlapping-date-ranges-to/m-p/557344#M155356</link>
      <description>&lt;P&gt;Thanks, that works--what a brain fart on my part, I don't know why I assumed that wouldn't work.&lt;/P&gt;</description>
      <pubDate>Thu, 09 May 2019 02:13:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/use-data-step-to-merge-table-with-overlapping-date-ranges-to/m-p/557344#M155356</guid>
      <dc:creator>sshockl</dc:creator>
      <dc:date>2019-05-09T02:13:24Z</dc:date>
    </item>
  </channel>
</rss>

