<?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: How to merge or join two tables, by a variable and keep all the columns? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-or-join-two-tables-by-a-variable-and-keep-all-the/m-p/344003#M79023</link>
    <description>&lt;P&gt;And if you need to compare datasets, have a look at proc compare; there you can (and should) have identically named columns in the input datasets.&lt;/P&gt;</description>
    <pubDate>Fri, 24 Mar 2017 09:58:46 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2017-03-24T09:58:46Z</dc:date>
    <item>
      <title>How to merge or join two tables, by a variable and keep all the columns?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-or-join-two-tables-by-a-variable-and-keep-all-the/m-p/343995#M79019</link>
      <description>&lt;P&gt;Hey!&lt;/P&gt;&lt;P&gt;I've got a problem, which I barely able to solve. &lt;span class="lia-unicode-emoji" title=":confused_face:"&gt;😕&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have to make a table to compare datas (10 categories). I made two independent tables, which both have "IDs" (as "Kategoria"). I want to merge them (or left join them, with proc sql) by the given ID, and to keep all of the columns, to compare both.&lt;/P&gt;&lt;P&gt;For example there is a "A" table with a,b,c,d columns and there is an another "B" table with same columns, but different datas. I want to merge them by 'd' in the "C" - new table - &amp;nbsp;so it should look like this: a,b,c, D (this is the key),a,b,c.&amp;nbsp;&lt;/P&gt;&lt;P&gt;But as I use left join it says error "WARNING: Variable Origin already exists on file TEST.CARS_COMPARED" and 3 more errors like this.&lt;/P&gt;&lt;P&gt;How should I join them? Any ideas? &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the full code:&lt;/P&gt;&lt;PRE&gt;proc sql;
create table test.cars as
select *
from sashelp.cars
order by invoice;
quit;

data _null_;
 length maxinv 8;
 set test.cars end=e;
 retain maxinv;
 if _N_=1 then maxinv=invoice;
 if maxinv&amp;lt;invoice then maxinv=invoice;
 if e then put maxinv=;

 length mininv 8;
 set test.cars end=e;
 retain mininv;
 if _N_=1 then mininv=invoice;
 if mininv&amp;gt;invoice then mininv=invoce;
 if e then put mininv=;
 diffinv=maxinv-mininv;
 put diffinv;
 dis=diffinv/10;
 put dis;
run;

Data test.category;
  set test.cars;
  if invoice =&amp;lt; 9875 then category=1;
  if 9875 &amp;lt; invoice &amp;lt; 26243.5 then category=2;
  if 26243.5 &amp;lt; invoice &amp;lt; 42612 then category=3;
  if 42612 &amp;lt; invoice &amp;lt; 58980.5 then category=4;
  if 58980.5 &amp;lt; invoice &amp;lt; 75349 then category=5;
  if 75349 &amp;lt; invoice &amp;lt; 91717.5 then category=6;
  if 91717.5 &amp;lt; invoice &amp;lt; 108086 then category=7;
  if 108086 &amp;lt; invoice &amp;lt; 124454.5 then category=8;
  if 124454.5 &amp;lt; invoice &amp;lt; 140823 then category=9;
  if 140823 &amp;lt; invoice then category=10;
run;

proc sql;
create table test.cars_EU as
select Origin, MPG_City,
Horsepower as Loero,
Category as Kategoria
from test.category
group by kategoria, Origin
having Origin = 'Europe'
order by Origin, kategoria, Loero;
quit;

proc sql;
create table test.cars_USA as
select Origin, MPG_City,
Horsepower as Loero,
Category as Kategoria
from test.category
group by kategoria, Origin
having Origin = 'USA'
order by Origin, Kategoria, Loero;
quit;

/*HERE I WANT TO MERGE:*/

proc sql;
create table test.cars_compared as
select a.*, b.* from test.cars_eu as a left join test.cars_usa as b
on a.kategoria = b.kategoria;
quit;&lt;/PRE&gt;&lt;P&gt;ERRORS:&lt;/P&gt;&lt;P&gt;WARNING: Variable Origin already exists on file TEST.CARS_COMPARED.&lt;BR /&gt;WARNING: Variable MPG_City already exists on file TEST.CARS_COMPARED.&lt;BR /&gt;WARNING: Variable Loero already exists on file TEST.CARS_COMPARED.&lt;BR /&gt;WARNING: Variable Kategoria already exists on file TEST.CARS_COMPARED.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your help! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Mar 2017 09:37:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-or-join-two-tables-by-a-variable-and-keep-all-the/m-p/343995#M79019</guid>
      <dc:creator>Derdavos</dc:creator>
      <dc:date>2017-03-24T09:37:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge or join two tables, by a variable and keep all the columns?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-or-join-two-tables-by-a-variable-and-keep-all-the/m-p/343999#M79022</link>
      <description>&lt;P&gt;Don't use *, at least for the second dataset, and give the variables new names:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table test.cars_compared as
select
  a.*,
  b.origin as b_origin,
  b.mpg_city as b_mpg_city,
  b.loero as b_loero
from test.cars_eu as a left join test.cars_usa as b
on a.kategoria = b.kategoria;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The names of columns in a dataset have to be unique.&lt;/P&gt;
&lt;P&gt;b.kategoria is omitted as it is identical to a.kategoria by definition.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Mar 2017 09:48:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-or-join-two-tables-by-a-variable-and-keep-all-the/m-p/343999#M79022</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-03-24T09:48:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge or join two tables, by a variable and keep all the columns?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-or-join-two-tables-by-a-variable-and-keep-all-the/m-p/344003#M79023</link>
      <description>&lt;P&gt;And if you need to compare datasets, have a look at proc compare; there you can (and should) have identically named columns in the input datasets.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Mar 2017 09:58:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-or-join-two-tables-by-a-variable-and-keep-all-the/m-p/344003#M79023</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-03-24T09:58:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge or join two tables, by a variable and keep all the columns?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-or-join-two-tables-by-a-variable-and-keep-all-the/m-p/344007#M79024</link>
      <description>&lt;P&gt;Thank you, helped a lot! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Mar 2017 10:13:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-or-join-two-tables-by-a-variable-and-keep-all-the/m-p/344007#M79024</guid>
      <dc:creator>Derdavos</dc:creator>
      <dc:date>2017-03-24T10:13:00Z</dc:date>
    </item>
  </channel>
</rss>

