<?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 of two tables with same column names in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merge-of-two-tables-with-same-column-names/m-p/274983#M54937</link>
    <description>&lt;P&gt;If you want to do this for all columns of two datasets having the same structure, you can use proc contents with the out parameter. Then you can use the generated output table to create a macrovariable contaning the list of variables in your select :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;[not tested]&lt;/P&gt;
&lt;P&gt;SELECT cat("a.",NAME," AS A_",NAME), cat("b.",NAME," AS B_",NAME)&lt;/P&gt;
&lt;P&gt;INTO :a_cols SEPARATED BY ',', :b_cols SEPARATED BY ','&lt;/P&gt;
&lt;P&gt;FROM contents_a; /* Table generated by proc contents on table A */&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SELECT &amp;amp;a_cols., &amp;amp;_bcols&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the structure differ, you can adapt by creating intermediary tables containing only the relevant columns&lt;/P&gt;</description>
    <pubDate>Fri, 03 Jun 2016 15:13:54 GMT</pubDate>
    <dc:creator>gamotte</dc:creator>
    <dc:date>2016-06-03T15:13:54Z</dc:date>
    <item>
      <title>Merge of two tables with same column names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-of-two-tables-with-same-column-names/m-p/274974#M54933</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to merge these two tables. Both tables have the exact same variable names. Is there any way to append a string to the front of each variable name? Like having all variables coming from table aliased as 'A' would have 'A_' as part of the variable name. &amp;nbsp;And columns coming from the table aliased as 'B' would be prefixed with 'B_'. &amp;nbsp;Or is there a built-in way to handle this? &amp;nbsp;I would be doing this so that I could later subset based on variable comparisons rules between like variables. &amp;nbsp;For example: &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if&amp;nbsp;A_DELIVERY &amp;gt; B_DELIVERY;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table MergedData as 
		select  a.delivery, b.delivery, a.type_cd, b.type_cd, ...more
	        from iv_ora.ods_iv_recon_selected_mp a,  bids_ora.ods_bi_recon_selected_mp b                                                                                                                                                                                                                                                                                                                                                                                                                                                   
		where a.imb_dlvry_zip_5 = '14221'
		  and a.imb_code = b.imb_code;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jun 2016 14:54:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-of-two-tables-with-same-column-names/m-p/274974#M54933</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2016-06-03T14:54:57Z</dc:date>
    </item>
    <item>
      <title>Re: Merge of two tables with same column names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-of-two-tables-with-same-column-names/m-p/274978#M54935</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;SELECT a.delivery AS A_delivery, b.delivery AS B_delivery, ,...</description>
      <pubDate>Fri, 03 Jun 2016 15:02:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-of-two-tables-with-same-column-names/m-p/274978#M54935</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2016-06-03T15:02:41Z</dc:date>
    </item>
    <item>
      <title>Re: Merge of two tables with same column names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-of-two-tables-with-same-column-names/m-p/274982#M54936</link>
      <description>yah, but there are hundreds of columns so I was hoping for a less manual way of doing this. Any thoughts?</description>
      <pubDate>Fri, 03 Jun 2016 15:06:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-of-two-tables-with-same-column-names/m-p/274982#M54936</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2016-06-03T15:06:19Z</dc:date>
    </item>
    <item>
      <title>Re: Merge of two tables with same column names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-of-two-tables-with-same-column-names/m-p/274983#M54937</link>
      <description>&lt;P&gt;If you want to do this for all columns of two datasets having the same structure, you can use proc contents with the out parameter. Then you can use the generated output table to create a macrovariable contaning the list of variables in your select :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;[not tested]&lt;/P&gt;
&lt;P&gt;SELECT cat("a.",NAME," AS A_",NAME), cat("b.",NAME," AS B_",NAME)&lt;/P&gt;
&lt;P&gt;INTO :a_cols SEPARATED BY ',', :b_cols SEPARATED BY ','&lt;/P&gt;
&lt;P&gt;FROM contents_a; /* Table generated by proc contents on table A */&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SELECT &amp;amp;a_cols., &amp;amp;_bcols&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the structure differ, you can adapt by creating intermediary tables containing only the relevant columns&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jun 2016 15:13:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-of-two-tables-with-same-column-names/m-p/274983#M54937</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2016-06-03T15:13:54Z</dc:date>
    </item>
    <item>
      <title>Re: Merge of two tables with same column names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-of-two-tables-with-same-column-names/m-p/275044#M54950</link>
      <description>&lt;P&gt;Minor recomendation JOIN ON the imb_code instead of selecting WHERE after doing the join of all records. It doesn't take many records in the two sets to start seeing performance hits : 1000(records in A) * 1000 (records in B) get combined for 1,000,000 records that you then select from.&lt;/P&gt;
&lt;P&gt;Also use&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;from (select * from iv_ora&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;ods_iv_recon_selected_mp&amp;nbsp;where a&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;imb_dlvry_zip_5 &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token string"&gt;'14221') as a&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token string"&gt;to further reduce the size of the comparison sets.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jun 2016 17:22:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-of-two-tables-with-same-column-names/m-p/275044#M54950</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-06-03T17:22:12Z</dc:date>
    </item>
    <item>
      <title>Re: Merge of two tables with same column names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-of-two-tables-with-same-column-names/m-p/275084#M54963</link>
      <description>&lt;P&gt;Insteat of creating a wide data structure&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A_id B_id A_x B_x ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i.e. multiple columns with the same type of information, you should try to work with a long structure&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;'A' id x ...&lt;/P&gt;
&lt;P&gt;'B' id x ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;that will allow simpler data manipulation and scaling.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, your two tables could be concatenated with&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set&amp;nbsp;iv_ora.ods_iv_recon_selected_mp(in=a) bids_ora.ods_bi_recon_selected_mp;&lt;/P&gt;
&lt;P&gt;if a then source="A" else source="B";&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;or&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;create table want as&lt;/P&gt;
&lt;P&gt;select "A" as source, * from&amp;nbsp;iv_ora.ods_iv_recon_selected_mp&lt;/P&gt;
&lt;P&gt;union all corresponding&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;select "B" as source, * from&amp;nbsp;&lt;/SPAN&gt;bids_ora.ods_bi_recon_selected_mp;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jun 2016 19:15:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-of-two-tables-with-same-column-names/m-p/275084#M54963</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-06-03T19:15:55Z</dc:date>
    </item>
    <item>
      <title>Re: Merge of two tables with same column names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-of-two-tables-with-same-column-names/m-p/275085#M54964</link>
      <description>&lt;P&gt;So if I understand you correctly it would look like this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	proc sql;
	create table QueryData as 
		select  b.actual_dlvry_date as AD_DT, 
			b.imb_code length = 31, 
			b.spm_calc_batch_date
	         from (select * from iv_ora.ods_iv_recon_selected_mp where imb_dlvry_zip_5 = '14221') a
                 inner join (select * from bids_ora.ods_bi_recon_selected_mp where imb_dlvry_zip_5 = '14221') b                                                                                                                                                                                                                                                                                                                                                                                                                                                   
		         on a.imb_code = b.imb_code
		where &amp;amp;rule;
	quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Jun 2016 19:16:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-of-two-tables-with-same-column-names/m-p/275085#M54964</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2016-06-03T19:16:30Z</dc:date>
    </item>
    <item>
      <title>Re: Merge of two tables with same column names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-of-two-tables-with-same-column-names/m-p/275090#M54966</link>
      <description>&lt;P&gt;Interesting, but then how would I do my rule comparison in the long structure? In the wide structure the variables are on the same record and the comparison would look like:&lt;BR /&gt;&lt;BR /&gt;if A_DELIVERY &amp;gt; B_DELIVERY then do...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How would this look in the long structure?&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jun 2016 19:22:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-of-two-tables-with-same-column-names/m-p/275090#M54966</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2016-06-03T19:22:53Z</dc:date>
    </item>
    <item>
      <title>Re: Merge of two tables with same column names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-of-two-tables-with-same-column-names/m-p/275100#M54969</link>
      <description>&lt;P&gt;With SQL, you would do:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;select a.id, a.&lt;SPAN&gt;DELIVERY &amp;gt; b.DELIVERY as comparison&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;from have as a inner join have as b on a.id=b.id&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;where a.source="A" and b.source="B"&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;in a data step, you would do:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;merge &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; have(where=(source="A") rename=delivery=a_delivery) &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; have(where=(source="B")&amp;nbsp;rename=delivery=b_delivery);&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;by id;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;comparison = a_delivery &amp;gt; b_delivery;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;But, in most cases, &lt;EM&gt;source&lt;/EM&gt; is not going to be pertinent information. Fields such as &lt;EM&gt;Year&lt;/EM&gt;, &lt;EM&gt;Location&lt;/EM&gt;, or &lt;EM&gt;Product&lt;/EM&gt; will more likely distinguish information from different sources.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jun 2016 19:48:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-of-two-tables-with-same-column-names/m-p/275100#M54969</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-06-03T19:48:13Z</dc:date>
    </item>
    <item>
      <title>Re: Merge of two tables with same column names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-of-two-tables-with-same-column-names/m-p/289807#M59878</link>
      <description>&lt;P&gt;I keep coming back to your example, but how does creating a variable named Comparison (e.g.&amp;nbsp;&lt;SPAN&gt;a.&lt;/SPAN&gt;&lt;SPAN&gt;DELIVERY &amp;gt; b.DELIVERY as comparison) help me to actually select the records where&amp;nbsp;a.&lt;SPAN&gt;DELIVERY is greater than b.DELIVERY? &amp;nbsp;I need to actually create a ds of records where&amp;nbsp;a.&lt;SPAN&gt;DELIVERY &amp;gt; b.DELIVERY.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Aug 2016 14:38:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-of-two-tables-with-same-column-names/m-p/289807#M59878</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2016-08-05T14:38:54Z</dc:date>
    </item>
    <item>
      <title>Re: Merge of two tables with same column names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-of-two-tables-with-same-column-names/m-p/289823#M59882</link>
      <description>&lt;P&gt;To get the IDs where &lt;SPAN&gt;a.&lt;/SPAN&gt;&lt;SPAN&gt;DELIVERY &amp;gt; b.DELIVERY&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;create table AgtB as&lt;/P&gt;
&lt;P&gt;select a.id&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;from &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;have as a inner join have as b on a.id=b.id and&amp;nbsp;a.&lt;SPAN&gt;DELIVERY &amp;gt; b.DELIVERY&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;where a.source="A" and b.source="B"&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Aug 2016 15:37:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-of-two-tables-with-same-column-names/m-p/289823#M59882</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-08-05T15:37:59Z</dc:date>
    </item>
    <item>
      <title>Re: Merge of two tables with same column names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-of-two-tables-with-same-column-names/m-p/289829#M59884</link>
      <description>&lt;P&gt;To get the IDs where &lt;SPAN&gt;a.&lt;/SPAN&gt;&lt;SPAN&gt;DELIVERY &amp;gt; b.DELIVERY&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table A_gt_B_IDs as
select a.id
from
	have as a inner join 
	have as b on 
		a.id = b.id and&amp;nbsp;
		a.DELIVERY &amp;gt; b.DELIVERY
where a.source="A" and b.source="B";
quit;

/*Then you can, for example, extract all records for those IDs with: */

proc sql;
create table A_gt_B as
select *
from have
where id in (select id from A_gt_B_IDs);
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Aug 2016 15:46:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-of-two-tables-with-same-column-names/m-p/289829#M59884</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-08-05T15:46:38Z</dc:date>
    </item>
    <item>
      <title>Re: Merge of two tables with same column names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-of-two-tables-with-same-column-names/m-p/291695#M60424</link>
      <description>&lt;P&gt;I've got it working! Thanks so much for taking the time to help. I really appreciate it.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Aug 2016 14:49:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-of-two-tables-with-same-column-names/m-p/291695#M60424</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2016-08-15T14:49:25Z</dc:date>
    </item>
  </channel>
</rss>

