<?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 join two tables, by a variable with the same column names ? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-join-two-tables-by-a-variable-with-the-same-column-names/m-p/768779#M243872</link>
    <description>&lt;P&gt;I want to fill rows where Year=2019 with data from&amp;nbsp;"production122019_nnd".&lt;/P&gt;
&lt;P&gt;When I want to combine my original table "Base_Sinistre_1" with "production122019_nnd" which includes only data for the year 2019 to complete the rows of&amp;nbsp;"Base_Sinistre_1" where Survenance=2019 I got null data with this warning message :&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;WARNING: Variable ANNEE_prod already exists on file WORK.BASE_SINISTRE_2.&lt;U&gt;&lt;BR /&gt;&lt;/U&gt;WARNING: Variable CONTRAT already exists on file WORK.BASE_SINISTRE_2.&lt;U&gt;&lt;BR /&gt;&lt;/U&gt;WARNING: Variable AGENCE already exists on file WORK.BASE_SINISTRE_2.&lt;U&gt;&lt;BR /&gt;&lt;/U&gt;WARNING: Variable USAGEE already exists on file WORK.BASE_SINISTRE_2.&lt;/P&gt;</description>
    <pubDate>Tue, 21 Sep 2021 13:47:12 GMT</pubDate>
    <dc:creator>KarimaTouati</dc:creator>
    <dc:date>2021-09-21T13:47:12Z</dc:date>
    <item>
      <title>How to join two tables, by a variable with the same column names ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-join-two-tables-by-a-variable-with-the-same-column-names/m-p/768723#M243859</link>
      <description>&lt;P&gt;Hello folks !&lt;/P&gt;&lt;P&gt;I've got a problem, which I barely able to solve.&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;I have to make a table C based on another table A and B where the two tables have the same column names but for different years (for example 2018 and 2019). I have already a column to flag the year in the first table A.&lt;/P&gt;&lt;P&gt;When making a left join (with proc sql)&amp;nbsp;by the given ID while keeping the same variable names on the second table B (where I have data only for year 2019), I got null values for the year 2019 in the table C.&lt;/P&gt;&lt;P&gt;I don't want to change the variable names since I want to fill the table C in the same row and not have new columns for the year 2019.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example there is a "A" table with a,b,c,d,Year 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),Year.&amp;nbsp;&lt;/P&gt;&lt;P&gt;But as I use left join I got null data from the table B.&lt;/P&gt;&lt;P&gt;I want to mention that I want to fill my data fo the same row.&lt;/P&gt;&lt;P&gt;How should I join them? Any ideas?&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&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;&lt;CODE class=" language-sas"&gt;proc sql;
create table Base_Sinistre_2 as select A.*,
put(B.Annee,8.) as ANNEE_prod,
B.Contrat,
B.Agence,
B.usagee
From Base_Sinistre_1 as A
left join production122019_nnd as B On A.Num_POLICE=B.Contrat and put(A.Survenance,8.)=put(B.Annee,8.)
Where B.Code_Produit not in (580,595) ;
Quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;And here is the result.&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="B.PNG" style="width: 879px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/63771i5BFD88D198AD7195/image-size/large?v=v2&amp;amp;px=999" role="button" title="B.PNG" alt="B.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I will do the same for the table on which I have data for 2020.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Sep 2021 13:39:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-join-two-tables-by-a-variable-with-the-same-column-names/m-p/768723#M243859</guid>
      <dc:creator>KarimaTouati</dc:creator>
      <dc:date>2021-09-21T13:39:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to join two tables, by a variable with the same column names ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-join-two-tables-by-a-variable-with-the-same-column-names/m-p/768748#M243862</link>
      <description>&lt;P&gt;When you get only missing values for the B. variables, then you do not have matches.&lt;/P&gt;
&lt;P&gt;In particular, I would inspect these columns:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;A.Survenance_sin=put(B.Annee,10.)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Could it be that A.Survenance_sin has leading zeroes, or a different length overall?&lt;/P&gt;
&lt;P&gt;Also note that&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;put(B.Annee,10.)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;will right-adjust the string, so the number 1 would end up as&lt;/P&gt;
&lt;PRE&gt;         1&lt;/PRE&gt;
&lt;P&gt;(9 leading blanks)&lt;/P&gt;</description>
      <pubDate>Tue, 21 Sep 2021 11:46:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-join-two-tables-by-a-variable-with-the-same-column-names/m-p/768748#M243862</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-09-21T11:46:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to join two tables, by a variable with the same column names ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-join-two-tables-by-a-variable-with-the-same-column-names/m-p/768761#M243866</link>
      <description>&lt;P&gt;I checked the length of both variables but it was not the problem&lt;/P&gt;&lt;P&gt;I have edited my question and added the output table for overview.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Sep 2021 12:32:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-join-two-tables-by-a-variable-with-the-same-column-names/m-p/768761#M243866</guid>
      <dc:creator>KarimaTouati</dc:creator>
      <dc:date>2021-09-21T12:32:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to join two tables, by a variable with the same column names ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-join-two-tables-by-a-variable-with-the-same-column-names/m-p/768764#M243867</link>
      <description>&lt;P&gt;This does not make much sense&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;put(A.Survenance,8.)=put(B.Annee,8.)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If those two variables are numeric why not just compare them directly?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;A.Survenance=B.Annee&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Sep 2021 12:44:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-join-two-tables-by-a-variable-with-the-same-column-names/m-p/768764#M243867</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-21T12:44:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to join two tables, by a variable with the same column names ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-join-two-tables-by-a-variable-with-the-same-column-names/m-p/768768#M243868</link>
      <description>&lt;P&gt;Even if I do so, that doesn't solve the problem.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Sep 2021 12:53:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-join-two-tables-by-a-variable-with-the-same-column-names/m-p/768768#M243868</guid>
      <dc:creator>KarimaTouati</dc:creator>
      <dc:date>2021-09-21T12:53:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to join two tables, by a variable with the same column names ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-join-two-tables-by-a-variable-with-the-same-column-names/m-p/768770#M243869</link>
      <description>&lt;P&gt;I do not understand what the question is.&lt;/P&gt;
&lt;P&gt;From your photograph it looks like whatever table provided those columns on the right only has data for YEAR=2018.&lt;/P&gt;
&lt;P&gt;What is it that you want to happen differently?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note: Please post data as text not photographs.&amp;nbsp; Please use the Insert Code and/or Insert SAS Code button for posting data and/or program code.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Sep 2021 13:02:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-join-two-tables-by-a-variable-with-the-same-column-names/m-p/768770#M243869</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-21T13:02:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to join two tables, by a variable with the same column names ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-join-two-tables-by-a-variable-with-the-same-column-names/m-p/768779#M243872</link>
      <description>&lt;P&gt;I want to fill rows where Year=2019 with data from&amp;nbsp;"production122019_nnd".&lt;/P&gt;
&lt;P&gt;When I want to combine my original table "Base_Sinistre_1" with "production122019_nnd" which includes only data for the year 2019 to complete the rows of&amp;nbsp;"Base_Sinistre_1" where Survenance=2019 I got null data with this warning message :&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;WARNING: Variable ANNEE_prod already exists on file WORK.BASE_SINISTRE_2.&lt;U&gt;&lt;BR /&gt;&lt;/U&gt;WARNING: Variable CONTRAT already exists on file WORK.BASE_SINISTRE_2.&lt;U&gt;&lt;BR /&gt;&lt;/U&gt;WARNING: Variable AGENCE already exists on file WORK.BASE_SINISTRE_2.&lt;U&gt;&lt;BR /&gt;&lt;/U&gt;WARNING: Variable USAGEE already exists on file WORK.BASE_SINISTRE_2.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Sep 2021 13:47:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-join-two-tables-by-a-variable-with-the-same-column-names/m-p/768779#M243872</guid>
      <dc:creator>KarimaTouati</dc:creator>
      <dc:date>2021-09-21T13:47:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to join two tables, by a variable with the same column names ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-join-two-tables-by-a-variable-with-the-same-column-names/m-p/768786#M243877</link>
      <description>&lt;P&gt;So you cannot have two variables with the same name.&lt;/P&gt;
&lt;P&gt;SQL code will keep the first one it sees and drop the second one.&lt;/P&gt;
&lt;P&gt;It is not clear how you want to handle those columns when there is a match on the keys.&lt;/P&gt;
&lt;P&gt;Do you always want the value from the second table instead of the first?&amp;nbsp; Just only include the variable from the second table in the list of columns.&lt;/P&gt;
&lt;P&gt;Do you want to use the value from the second table only if it is not missing? Use coalesce:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; ,coalesce(B.ANNEE_prod,A.ANNEE_prod) as  ANNEE_prod&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Do you want to use the value from the second table even if it is missing, but only when the join was successful?&amp;nbsp; Then perhaps use CASE statement&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;case when (not missing(B.keyvar) then B.ANNEE_prod else A.ANNEE_prod end as  ANNEE_prod&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Detecting when a table is contributing to a join can be&amp;nbsp;tricky in SQL syntax.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Sep 2021 13:59:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-join-two-tables-by-a-variable-with-the-same-column-names/m-p/768786#M243877</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-21T13:59:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to join two tables, by a variable with the same column names ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-join-two-tables-by-a-variable-with-the-same-column-names/m-p/768802#M243886</link>
      <description>&lt;P&gt;INPUT data in the form of a data step will allow us to test code. We can't code against pictures. Displaying incorrect/ undesired results does not show what the desired result is.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instructions here: &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt; will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the &amp;lt;/&amp;gt; icon or attached as text to show exactly what you have and that we can test code against.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Sep 2021 14:38:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-join-two-tables-by-a-variable-with-the-same-column-names/m-p/768802#M243886</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-09-21T14:38:34Z</dc:date>
    </item>
  </channel>
</rss>

