<?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: Update table in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Update-table/m-p/254835#M57002</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/60293"&gt;@SASEnthusiast﻿&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can update multiple columns using the SET &lt;EM&gt;clause&lt;/EM&gt; of PROC SQL's UPDATE &lt;EM&gt;statement&lt;/EM&gt;, but the syntax for this is different. If&amp;nbsp;you compare your code to the &lt;A href="http://support.sas.com/documentation/cdl/en/syntaxidx/68719/HTML/default/index.htm#/documentation/cdl/en/sqlproc/69049/HTML/default/p0ci36zwxhm1xdn1a943yeczfalk.htm" target="_blank"&gt;official syntax of the UPDATE statement&lt;/A&gt;, you will notice that a FROM clause is not mentioned there. The "&lt;EM&gt;sql-expressions&lt;/EM&gt;" in the syntax, however, are allowed to be query expressions, which in turn would involve FROM clauses.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
input id x y w;
cards;
1 11 111 1111
2 22 222 2222
3 33 333 3333
4 44 444 4444
;

data table2;
input id x y z;
cards;
2 20 200 2000
4 40 400 4000 
6 60 600 6000 
;

proc sql;
update table1 t1
set x=(select x from table2 t2 where t1.id=t2.id),
    y=(select y from table2 t2 where t1.id=t2.id)
where id in (select id from table2);
quit;

proc print data=table1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The above code updates TABLE1 in place. The subqueries &lt;FONT face="courier new,courier"&gt;select x from &lt;/FONT&gt;... and &lt;FONT face="courier new,courier"&gt;select y from&lt;/FONT&gt; ... result in single values (which is important). Thus, the existing values of variables X and Y in TABLE1 are overwritten with the corresponding values from TABLE2 for the same ID.&amp;nbsp;The WHERE clause (&lt;FONT face="courier new,courier"&gt;where id in ...&lt;/FONT&gt;) prevents that values from observations 1 and 3 are overwritten with missing values. Alternatively, this could be achieved by using the COALESCE function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
update table1 t1
set x=coalesce((select x from table2 t2 where t1.id=t2.id), x),
    y=coalesce((select y from table2 t2 where t1.id=t2.id), y);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(This would no longer work if some values selected from TABLE2 were&amp;nbsp;missing values and the intention was to&amp;nbsp;let them replace non-missing values in&amp;nbsp;TABLE1.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As mentioned by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13674"&gt;@LinusH﻿&lt;/a&gt;, you could obtain the same result with a&amp;nbsp;data step using the MODIFY statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
modify table1
       table2;
by id;
where 1&amp;lt;=id&amp;lt;=4;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Without the WHERE statement, SAS would complain about ID 6 in TABLE2, which is not present in TABLE1. So, it would be better to restrict TABLE2 first to those IDs whose values are to be updated in TABLE1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 06 Mar 2016 13:12:34 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2016-03-06T13:12:34Z</dc:date>
    <item>
      <title>Update table</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Update-table/m-p/254811#M56999</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Hi, Is there a way to update multiple columns using Set statement.&lt;/P&gt;
&lt;P&gt;like:&lt;/P&gt;
&lt;PRE class="lang-sql prettyprint prettyprinted"&gt;&lt;CODE&gt;&lt;SPAN class="kwd"&gt;Update&lt;/SPAN&gt;&lt;SPAN class="pln"&gt; table1 &lt;/SPAN&gt;&lt;SPAN class="kwd"&gt;set&lt;/SPAN&gt; &lt;SPAN class="pun"&gt;(&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;a&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;b&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;c&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;d&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;e&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;f&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;g&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;h&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;i&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;j&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;k&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;)=&lt;/SPAN&gt;
&lt;SPAN class="pun"&gt;(&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;t2&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;.&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;a&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;t2&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;.&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;b&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;t2&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;.&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;c&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;t2&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;.&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;d&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;t2&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;.&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;e&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;t2&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;.&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;f&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;t2&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;.&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;g&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;t2&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;.&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;h&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;t2&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;.&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;i&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;t2&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;.&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;j&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;t2&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;.&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;k&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;)&lt;/SPAN&gt;
&lt;SPAN class="kwd"&gt;from&lt;/SPAN&gt;&lt;SPAN class="pln"&gt; table2 t2
&lt;/SPAN&gt;&lt;SPAN class="kwd"&gt;where&lt;/SPAN&gt;&lt;SPAN class="pln"&gt; table1&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;.&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;id&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;=&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;table2&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;.&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;id&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;I am attaching my code below. I am not sure it would be relevant or not. This definetly gives a lot of Errors.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	proc sql;
		update inlib.Video_News
	
		set network= T2.Network, VideoFormat=VideoFormat, DistnChannel= DistnChannel, Platform= Platform, Quarter=T2.Quarter, Budget=T2.Budget_Capacity11
		
		from (select a.network, b.Videoformat, b.distnchannel, b.Platform, a.Quarter, a.device, (budget* Capacity_pct) as Budget_Capacity11 
			
			from inlib.Video_News a
 
				left join (select   c.*, (Budget_Capacity/Capacity_tot) as Capacity_Pct  
							from inlib.Cap_News c
								left join (	select Quarter, Network, distnChannel, VideoFormat, Platform,device, sum(Budget_Capacity) as Capacity_Tot
											from inlib.Cap_News
											where upcase(device)= 'OTT'
											group by Quarter, network, device)d
							on upcase(c.Quarter)= upcase(d.Quarter)
							and upcase(c.Network)= upcase(d.Network)
							and upcase(c.VideoFormat)= upcase(d.VideoFormat)
							and upcase(c.Distnchannel)= upcase(d.Distnchannel)
							and upcase(c.Platform)= upcase(d.Platform) 
							and upcase(c.Device)= upcase(d.Device) 
							where upcase(c.device)= 'OTT') b			

	on compress(upcase(a.device)) = compress(upcase(b.Device)) 
	and compress(upcase(a.Network))= compress(upcase(b.Network)) 
	and compress(upcase(a.Quarter))=compress(upcase(b.Quarter))
	order by  a.network, b.Videoformat,b.distnchannel,  b.Platform,a.Quarter, a.device) T2

	where compress(upcase(device)) = compress(upcase(T2.Device)) 
	and compress(upcase(Network))= compress(upcase(T2.Network)) 
	and compress(upcase(Quarter))=compress(upcase(T2.Quarter))

		;
	quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 06 Mar 2016 07:33:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Update-table/m-p/254811#M56999</guid>
      <dc:creator>SASEnthusiast</dc:creator>
      <dc:date>2016-03-06T07:33:10Z</dc:date>
    </item>
    <item>
      <title>Re: Update table</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Update-table/m-p/254832#M57001</link>
      <description>No, I don't think that there are any similar SQL syntax for this. That's why I'm most of the time favour data step with modify by. It's simpler to code, and usually faster.</description>
      <pubDate>Sun, 06 Mar 2016 12:39:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Update-table/m-p/254832#M57001</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-03-06T12:39:38Z</dc:date>
    </item>
    <item>
      <title>Re: Update table</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Update-table/m-p/254835#M57002</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/60293"&gt;@SASEnthusiast﻿&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can update multiple columns using the SET &lt;EM&gt;clause&lt;/EM&gt; of PROC SQL's UPDATE &lt;EM&gt;statement&lt;/EM&gt;, but the syntax for this is different. If&amp;nbsp;you compare your code to the &lt;A href="http://support.sas.com/documentation/cdl/en/syntaxidx/68719/HTML/default/index.htm#/documentation/cdl/en/sqlproc/69049/HTML/default/p0ci36zwxhm1xdn1a943yeczfalk.htm" target="_blank"&gt;official syntax of the UPDATE statement&lt;/A&gt;, you will notice that a FROM clause is not mentioned there. The "&lt;EM&gt;sql-expressions&lt;/EM&gt;" in the syntax, however, are allowed to be query expressions, which in turn would involve FROM clauses.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
input id x y w;
cards;
1 11 111 1111
2 22 222 2222
3 33 333 3333
4 44 444 4444
;

data table2;
input id x y z;
cards;
2 20 200 2000
4 40 400 4000 
6 60 600 6000 
;

proc sql;
update table1 t1
set x=(select x from table2 t2 where t1.id=t2.id),
    y=(select y from table2 t2 where t1.id=t2.id)
where id in (select id from table2);
quit;

proc print data=table1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The above code updates TABLE1 in place. The subqueries &lt;FONT face="courier new,courier"&gt;select x from &lt;/FONT&gt;... and &lt;FONT face="courier new,courier"&gt;select y from&lt;/FONT&gt; ... result in single values (which is important). Thus, the existing values of variables X and Y in TABLE1 are overwritten with the corresponding values from TABLE2 for the same ID.&amp;nbsp;The WHERE clause (&lt;FONT face="courier new,courier"&gt;where id in ...&lt;/FONT&gt;) prevents that values from observations 1 and 3 are overwritten with missing values. Alternatively, this could be achieved by using the COALESCE function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
update table1 t1
set x=coalesce((select x from table2 t2 where t1.id=t2.id), x),
    y=coalesce((select y from table2 t2 where t1.id=t2.id), y);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(This would no longer work if some values selected from TABLE2 were&amp;nbsp;missing values and the intention was to&amp;nbsp;let them replace non-missing values in&amp;nbsp;TABLE1.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As mentioned by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13674"&gt;@LinusH﻿&lt;/a&gt;, you could obtain the same result with a&amp;nbsp;data step using the MODIFY statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
modify table1
       table2;
by id;
where 1&amp;lt;=id&amp;lt;=4;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Without the WHERE statement, SAS would complain about ID 6 in TABLE2, which is not present in TABLE1. So, it would be better to restrict TABLE2 first to those IDs whose values are to be updated in TABLE1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 06 Mar 2016 13:12:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Update-table/m-p/254835#M57002</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-03-06T13:12:34Z</dc:date>
    </item>
    <item>
      <title>Re: Update table</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Update-table/m-p/254907#M57011</link>
      <description>&lt;P&gt;Rather than a big SQL statement, think about it in terms of Base SAS. &amp;nbsp;I.e. extract the data you need into SAS datasets, then merge them together. &amp;nbsp;It will help you debug it to as you will see the interveening steps. &amp;nbsp;So create a dataste of the inner joined data, then from that create another dataset which is summarised. &amp;nbsp;Finally merge the result to the main data. &amp;nbsp;Whilst one step may seem the best approach, SQL can be heavy on resource, datastep has a good chance of being quicker.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2016 10:07:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Update-table/m-p/254907#M57011</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-03-07T10:07:20Z</dc:date>
    </item>
  </channel>
</rss>

