<?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: SQL Update - Updating a table with corresponding values in a second table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SQL-Update-Updating-a-table-with-corresponding-values-in-a/m-p/308305#M66143</link>
    <description>&lt;P&gt;Reeza provides the ideal solution, eliminating the need to create a new data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
 update have1 as h1
 set var2=(select var2 from have2 as h2 where h1.var1=h2.var1)
 &lt;STRONG&gt;&lt;FONT color="#000000"&gt;where var1 in (select var1 from have2);&lt;/FONT&gt;&lt;/STRONG&gt;
 quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I reckon the where statement only improves performance. If you're updating only a few values in a very large data set then this will significantly speed up the update. Definitely my favorite solution to the problem.&lt;/P&gt;</description>
    <pubDate>Mon, 31 Oct 2016 15:57:02 GMT</pubDate>
    <dc:creator>PhilFromGER</dc:creator>
    <dc:date>2016-10-31T15:57:02Z</dc:date>
    <item>
      <title>SQL Update - Updating a table with corresponding values in a second table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Update-Updating-a-table-with-corresponding-values-in-a/m-p/21621#M3488</link>
      <description>I have 2 tables and I'm wanting to update the values in the first with the corresponding values in the second, e.g.&lt;BR /&gt;
&lt;BR /&gt;
table1&lt;BR /&gt;
a 1&lt;BR /&gt;
b 2&lt;BR /&gt;
c 3&lt;BR /&gt;
d 4&lt;BR /&gt;
&lt;BR /&gt;
table2&lt;BR /&gt;
b 5&lt;BR /&gt;
c 6&lt;BR /&gt;
&lt;BR /&gt;
to result in;&lt;BR /&gt;
&lt;BR /&gt;
table1&lt;BR /&gt;
a 1&lt;BR /&gt;
b 5&lt;BR /&gt;
c 6&lt;BR /&gt;
d 4&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
I'm not that familiar with SAS SQL syntax and the following code I wrote is displaying an error.&lt;BR /&gt;
&lt;BR /&gt;
PROC SQL ;&lt;BR /&gt;
UPDATE&lt;BR /&gt;
	table1 AS a&lt;BR /&gt;
LEFT JOIN&lt;BR /&gt;
	table2 AS b&lt;BR /&gt;
ON&lt;BR /&gt;
	a.var1 EQ b.var1&lt;BR /&gt;
SET&lt;BR /&gt;
	a.var2 = b.var2&lt;BR /&gt;
;&lt;BR /&gt;
QUIT ;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
65   LEFT JOIN&lt;BR /&gt;
     ----&lt;BR /&gt;
     79&lt;BR /&gt;
     76&lt;BR /&gt;
ERROR 79-322: Expecting a SET.&lt;BR /&gt;
&lt;BR /&gt;
ERROR 76-322: Syntax error, statement will be ignored.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Does SAS SQL support this kind of updating?&lt;BR /&gt;
&lt;BR /&gt;
As always, any help with this is greatly appreciated.&lt;BR /&gt;
&lt;BR /&gt;
Cheers,&lt;BR /&gt;
&lt;BR /&gt;
Fat Captain.</description>
      <pubDate>Thu, 03 Mar 2011 13:38:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Update-Updating-a-table-with-corresponding-values-in-a/m-p/21621#M3488</guid>
      <dc:creator>FatCaptain</dc:creator>
      <dc:date>2011-03-03T13:38:09Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Update - Updating a table with corresponding values in a second table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Update-Updating-a-table-with-corresponding-values-in-a/m-p/21622#M3489</link>
      <description>One of many ways.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
&lt;BR /&gt;
data table1;&lt;BR /&gt;
infile cards;&lt;BR /&gt;
input var1 $ var2;&lt;BR /&gt;
cards;&lt;BR /&gt;
a 1&lt;BR /&gt;
b 2&lt;BR /&gt;
c 3&lt;BR /&gt;
d 4&lt;BR /&gt;
;&lt;BR /&gt;
data table2;&lt;BR /&gt;
infile cards;&lt;BR /&gt;
input var1 $ var2;&lt;BR /&gt;
cards;&lt;BR /&gt;
b 5&lt;BR /&gt;
c 6&lt;BR /&gt;
;&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
create table table1 as select a.var1, coalesce(b.var2,a.var2) as var2&lt;BR /&gt;
from table1 as a left join table2 as b on a.var1=b.var1;&lt;BR /&gt;
quit;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Thu, 03 Mar 2011 14:29:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Update-Updating-a-table-with-corresponding-values-in-a/m-p/21622#M3489</guid>
      <dc:creator>Oleg_L</dc:creator>
      <dc:date>2011-03-03T14:29:31Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Update - Updating a table with corresponding values in a second table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Update-Updating-a-table-with-corresponding-values-in-a/m-p/21623#M3490</link>
      <description>Another way.. Make sure to sort the two tables before merge .. but for this case they are already sorted so no sorting is done..&lt;BR /&gt;
&lt;BR /&gt;
data table1;&lt;BR /&gt;
infile cards;&lt;BR /&gt;
input var1 $ var2 @@;&lt;BR /&gt;
cards;&lt;BR /&gt;
a 1 b 2 c 3 d 4&lt;BR /&gt;
;&lt;BR /&gt;
data table2;&lt;BR /&gt;
infile cards;&lt;BR /&gt;
input var1 $ var2 @@;&lt;BR /&gt;
cards;&lt;BR /&gt;
b 5 c 6&lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
data x;&lt;BR /&gt;
merge table1 table2;&lt;BR /&gt;
by var1;&lt;BR /&gt;
run;</description>
      <pubDate>Thu, 03 Mar 2011 14:37:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Update-Updating-a-table-with-corresponding-values-in-a/m-p/21623#M3490</guid>
      <dc:creator>SAS83</dc:creator>
      <dc:date>2011-03-03T14:37:49Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Update - Updating a table with corresponding values in a second table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Update-Updating-a-table-with-corresponding-values-in-a/m-p/21624#M3491</link>
      <description>Make sure Table1 is first then Table2 in the merge statement.. if you dont use ina and inb....&lt;BR /&gt;
&lt;BR /&gt;
data table1;&lt;BR /&gt;
infile cards;&lt;BR /&gt;
input var1 $ var2 @@;&lt;BR /&gt;
cards;&lt;BR /&gt;
a 1 b 2 c 3 d 4&lt;BR /&gt;
;&lt;BR /&gt;
data table2;&lt;BR /&gt;
infile cards;&lt;BR /&gt;
input var1 $ var2 @@;&lt;BR /&gt;
cards;&lt;BR /&gt;
b 5 c 6&lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
data x;&lt;BR /&gt;
merge table1 (in=ina) table2 (in=inb);&lt;BR /&gt;
by var1;&lt;BR /&gt;
if ina;&lt;BR /&gt;
run; &lt;BR /&gt;
&lt;BR /&gt;
this is similar to SQL left join..</description>
      <pubDate>Thu, 03 Mar 2011 14:43:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Update-Updating-a-table-with-corresponding-values-in-a/m-p/21624#M3491</guid>
      <dc:creator>SAS83</dc:creator>
      <dc:date>2011-03-03T14:43:43Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Update - Updating a table with corresponding values in a second table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Update-Updating-a-table-with-corresponding-values-in-a/m-p/21625#M3492</link>
      <description>Here's a sql solution since all the other are datastep and its what you were looking for initially. I doubt its more efficient, but useful to know.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data have1;&lt;BR /&gt;
input var1 $1 var2;&lt;BR /&gt;
datalines;&lt;BR /&gt;
a 1&lt;BR /&gt;
b 2&lt;BR /&gt;
c 3&lt;BR /&gt;
d 4&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data have2;&lt;BR /&gt;
input var1 $1 var2;&lt;BR /&gt;
datalines;&lt;BR /&gt;
b 5&lt;BR /&gt;
c 6&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
	update have1 as h1&lt;BR /&gt;
	set var2=(select var2 from have2 as h2 where h1.var1=h2.var1)&lt;BR /&gt;
	where var1 in (select var1 from have2);&lt;BR /&gt;
quit;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Fri, 04 Mar 2011 00:35:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Update-Updating-a-table-with-corresponding-values-in-a/m-p/21625#M3492</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2011-03-04T00:35:14Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Update - Updating a table with corresponding values in a second table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Update-Updating-a-table-with-corresponding-values-in-a/m-p/21626#M3493</link>
      <description>Thank you all for your responses.&lt;BR /&gt;
&lt;BR /&gt;
Cheers,&lt;BR /&gt;
&lt;BR /&gt;
Fat Captain.</description>
      <pubDate>Fri, 04 Mar 2011 11:25:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Update-Updating-a-table-with-corresponding-values-in-a/m-p/21626#M3493</guid>
      <dc:creator>FatCaptain</dc:creator>
      <dc:date>2011-03-04T11:25:47Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Update - Updating a table with corresponding values in a second table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Update-Updating-a-table-with-corresponding-values-in-a/m-p/308305#M66143</link>
      <description>&lt;P&gt;Reeza provides the ideal solution, eliminating the need to create a new data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
 update have1 as h1
 set var2=(select var2 from have2 as h2 where h1.var1=h2.var1)
 &lt;STRONG&gt;&lt;FONT color="#000000"&gt;where var1 in (select var1 from have2);&lt;/FONT&gt;&lt;/STRONG&gt;
 quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I reckon the where statement only improves performance. If you're updating only a few values in a very large data set then this will significantly speed up the update. Definitely my favorite solution to the problem.&lt;/P&gt;</description>
      <pubDate>Mon, 31 Oct 2016 15:57:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Update-Updating-a-table-with-corresponding-values-in-a/m-p/308305#M66143</guid>
      <dc:creator>PhilFromGER</dc:creator>
      <dc:date>2016-10-31T15:57:02Z</dc:date>
    </item>
  </channel>
</rss>

