<?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: Using MERGE INTO in Proc SQL in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Using-MERGE-INTO-in-Proc-SQL/m-p/770388#M81041</link>
    <description>&lt;P&gt;The merge step is saying check if post date and amounts match in both tables. If they do match then the rows is already present in Table A. If they don't match then add the rows for columns&amp;nbsp;pos_date,&amp;nbsp;amount&lt;/P&gt;&lt;P&gt;order_num and&amp;nbsp;invoice_num from Table A into Table B&lt;/P&gt;</description>
    <pubDate>Sat, 25 Sep 2021 09:01:15 GMT</pubDate>
    <dc:creator>JoseSAS79</dc:creator>
    <dc:date>2021-09-25T09:01:15Z</dc:date>
    <item>
      <title>Using MERGE INTO in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-MERGE-INTO-in-Proc-SQL/m-p/770383#M81038</link>
      <description>&lt;P&gt;Hi All!&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to update a merge into code that updates a table that sits in Teradata using information from another table that is also in teradata. I have now brought both these two tables into my SAS server environment within a folder , and for simplicity lets call the libname output. I want to update the below code to do the same merge steps using the tables that are now in the output folder. However when I remove the teradata connections and try to do the merge wrapped in proc sql it doesn't work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My current code is something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;connect to teradata as tera (server ='XXXX', Authdomain='XXXX')&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;execute(&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;MERGE INTO TableA as tbl1 using TableB as tbl2&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; on (tbl1.post_date = tbl2.pos_date&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; tbl1.amount = table2.amount)&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;WHEN NOT MATCHED THEN INSERT&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; (&amp;nbsp;tbl2.pos_date&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;table2.amount&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;table2.order_num&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;table2.invoice_num)&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; )by tera;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In order to reference to the new libraries within the SAS server I have updated the code to :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;MERGE INTO Output.TableA as tbl1 using Output.TableB as tbl2&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; on (tbl1.post_date = tbl2.pos_date&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; tbl1.amount = table2.amount)&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;WHEN NOT MATCHED THEN INSERT&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; (&amp;nbsp;tbl2.pos_date&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;table2.amount&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;table2.order_num&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;table2.invoice_num)&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However this MERGE INTO statement doesn't seem to work when its not being done within the teradata connection. Could you please let me know if there is something wrong with my new code where I have removed the tera data connections and updated the lib names? If MERGE INTO can not be done in a PROC &amp;nbsp;SQL , can you please tell me an alternative method to do the steps pls?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance!!&lt;/P&gt;</description>
      <pubDate>Sat, 25 Sep 2021 08:29:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-MERGE-INTO-in-Proc-SQL/m-p/770383#M81038</guid>
      <dc:creator>JoseSAS79</dc:creator>
      <dc:date>2021-09-25T08:29:47Z</dc:date>
    </item>
    <item>
      <title>Re: Using MERGE INTO in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-MERGE-INTO-in-Proc-SQL/m-p/770384#M81039</link>
      <description>&lt;P&gt;MERGE is not a valid statement in SAS PROC SQL.&lt;/P&gt;
&lt;P&gt;What does this do in Teradata?&lt;/P&gt;</description>
      <pubDate>Sat, 25 Sep 2021 08:36:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-MERGE-INTO-in-Proc-SQL/m-p/770384#M81039</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-09-25T08:36:46Z</dc:date>
    </item>
    <item>
      <title>Re: Using MERGE INTO in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-MERGE-INTO-in-Proc-SQL/m-p/770386#M81040</link>
      <description>&lt;P&gt;SAS SQL doesn't have a Merge statement. You need either two SQLs, one Update and one Insert or you could consider to use a SAS Data step Modify statement.&lt;/P&gt;</description>
      <pubDate>Sat, 25 Sep 2021 08:50:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-MERGE-INTO-in-Proc-SQL/m-p/770386#M81040</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-09-25T08:50:39Z</dc:date>
    </item>
    <item>
      <title>Re: Using MERGE INTO in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-MERGE-INTO-in-Proc-SQL/m-p/770388#M81041</link>
      <description>&lt;P&gt;The merge step is saying check if post date and amounts match in both tables. If they do match then the rows is already present in Table A. If they don't match then add the rows for columns&amp;nbsp;pos_date,&amp;nbsp;amount&lt;/P&gt;&lt;P&gt;order_num and&amp;nbsp;invoice_num from Table A into Table B&lt;/P&gt;</description>
      <pubDate>Sat, 25 Sep 2021 09:01:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-MERGE-INTO-in-Proc-SQL/m-p/770388#M81041</guid>
      <dc:creator>JoseSAS79</dc:creator>
      <dc:date>2021-09-25T09:01:15Z</dc:date>
    </item>
    <item>
      <title>Re: Using MERGE INTO in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-MERGE-INTO-in-Proc-SQL/m-p/770389#M81042</link>
      <description>Thanks Patrick - do you mind elaborating how to do it with two proc sql statements (update and insert)?</description>
      <pubDate>Sat, 25 Sep 2021 09:02:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-MERGE-INTO-in-Proc-SQL/m-p/770389#M81042</guid>
      <dc:creator>JoseSAS79</dc:creator>
      <dc:date>2021-09-25T09:02:44Z</dc:date>
    </item>
    <item>
      <title>Re: Using MERGE INTO in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-MERGE-INTO-in-Proc-SQL/m-p/770390#M81043</link>
      <description>&lt;P&gt;For me to provide actual code you would need to provide sample data first via SAS Data step code that creates such data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;May-be the discussion &lt;A href="https://www.mssqltips.com/sqlservertip/1704/using-merge-in-sql-server-to-insert-update-and-delete-at-the-same-time/" target="_self"&gt;here&lt;/A&gt;&amp;nbsp;is already all you need.&lt;/P&gt;</description>
      <pubDate>Sat, 25 Sep 2021 09:09:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-MERGE-INTO-in-Proc-SQL/m-p/770390#M81043</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-09-25T09:09:19Z</dc:date>
    </item>
    <item>
      <title>Re: Using MERGE INTO in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-MERGE-INTO-in-Proc-SQL/m-p/770421#M81044</link>
      <description>&lt;P&gt;So you just want to MERGE two datasets?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   merge tablea tableb;
   by post_date amount;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 25 Sep 2021 16:16:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-MERGE-INTO-in-Proc-SQL/m-p/770421#M81044</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-25T16:16:52Z</dc:date>
    </item>
    <item>
      <title>Re: Using MERGE INTO in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-MERGE-INTO-in-Proc-SQL/m-p/770467#M81045</link>
      <description>Oh didn't realise it was a simple merge step! I tried it with a sample from both tables and it worked. Thanks for that!</description>
      <pubDate>Sun, 26 Sep 2021 07:52:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-MERGE-INTO-in-Proc-SQL/m-p/770467#M81045</guid>
      <dc:creator>JoseSAS79</dc:creator>
      <dc:date>2021-09-26T07:52:01Z</dc:date>
    </item>
  </channel>
</rss>

