<?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 statement inside a proc sql in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/update-statement-inside-a-proc-sql/m-p/47968#M12941</link>
    <description>Thank u so much . It worked &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
    <pubDate>Wed, 15 Dec 2010 12:31:49 GMT</pubDate>
    <dc:creator>Rose</dc:creator>
    <dc:date>2010-12-15T12:31:49Z</dc:date>
    <item>
      <title>update statement inside a proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/update-statement-inside-a-proc-sql/m-p/47964#M12937</link>
      <description>I want to update tableA.ColumnA with tableB.ColumnB where tableA.id=tableB.id.&lt;BR /&gt;
Can I do this inside a proc sql? I tried the below code &lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
UPDATE tableA SET ColumnA = (&lt;BR /&gt;
SELECT ColumnB FROM tableB  a&lt;BR /&gt;
WHERE a.id =id) &lt;BR /&gt;
WHERE a.id = id;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
But this resulted in the error, ERROR: Unresolved reference to table/correlation name a.

Message was edited by: Rose</description>
      <pubDate>Fri, 10 Dec 2010 14:38:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/update-statement-inside-a-proc-sql/m-p/47964#M12937</guid>
      <dc:creator>Rose</dc:creator>
      <dc:date>2010-12-10T14:38:35Z</dc:date>
    </item>
    <item>
      <title>Re: update statement inside a proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/update-statement-inside-a-proc-sql/m-p/47965#M12938</link>
      <description>Yes.But I am afraid you need 'select' statement.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data op;&lt;BR /&gt;
 set sashelp.class;&lt;BR /&gt;
 where sex eq 'F';&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
create table new_table as&lt;BR /&gt;
 select a.*,b.name  as updated_name&lt;BR /&gt;
  from op as a,sashelp.class as b&lt;BR /&gt;
   where a.age eq b.age;&lt;BR /&gt;
quit;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Mon, 13 Dec 2010 02:44:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/update-statement-inside-a-proc-sql/m-p/47965#M12938</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2010-12-13T02:44:21Z</dc:date>
    </item>
    <item>
      <title>Re: update statement inside a proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/update-statement-inside-a-proc-sql/m-p/47966#M12939</link>
      <description>&lt;P&gt;Hello Rose,&lt;BR /&gt; &lt;BR /&gt; Probably the most direct way to go about this is to use the UPDATE statement in PROC SQL. I will provide a short code example below, point out a potential problem with it, then will modify part of the example to provide more robust code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tableToBeUpdated;
length z $5;
input z $ id y; 
cards;
one 1 50
two 2 30
three 3 30
four 4 60
five 5 70
six 6 80
run;

data tableB;
input id newY; 
cards;
1 500
2 233
3 300
4 472
5 111
6 2010
run;

proc sql;

update tableToBeUpdated tableA
set y = (select newY
from tableB
where tableA.id = id);

quit;

proc print data=tableToBeUpdated; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt; &lt;BR /&gt; The code above works just fine as long as tabelB contains *exactly one* row for each of the id's that are present in tableToBeUpdated. However, if tableB contains no updated values for some of the id's in tableToBeUpdated, the corresponding y values in tableToBeUpdated will be set to missing. This can be readily seen if the tableB dataaset above is replaced by this alternative tableB dataset:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tableB;
input id newY; 
cards;
1 500
3 300
6 2010
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt; A way to protect against this situation is to add a WHERE condition to the UPDATE statement so that only the rows that are in tableToBeUpdated which have id's that are present in tableB have their y values replaced. This modification results in the following UPDATE statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;

update tableToBeUpdated tableA
set y = (select newY
from tableB
where tableA.id = id)
where id in (select id
from tableB);

quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt; qseries@sas&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;(Editor's note: also &lt;A href="https://communities.sas.com/t5/SAS-Procedures/update-statement-inside-a-proc-sql/m-p/47967#M12940" target="_self"&gt;see this helpful reply&lt;/A&gt; from KSharp using the COALESCE function).&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 15:46:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/update-statement-inside-a-proc-sql/m-p/47966#M12939</guid>
      <dc:creator>LewisC_sas</dc:creator>
      <dc:date>2017-01-19T15:46:21Z</dc:date>
    </item>
    <item>
      <title>Re: update statement inside a proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/update-statement-inside-a-proc-sql/m-p/47967#M12940</link>
      <description>&lt;P&gt;Hi.&lt;BR /&gt; Misunderstood what you mean.&lt;BR /&gt; You need to use Function 'coalesce' to updated ID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp(keep=name age);
set sashelp.class;
select(name);
when ('Henry') age=1;
when ('James') age=2;
otherwise;
end;
run;
proc sql;
create table updated as
select a.name,coalesce(b.age,a.age)
from sashelp.class as a left join temp as b on a.name=b.name;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt; Ksharp&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 15:44:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/update-statement-inside-a-proc-sql/m-p/47967#M12940</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-01-19T15:44:35Z</dc:date>
    </item>
    <item>
      <title>Re: update statement inside a proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/update-statement-inside-a-proc-sql/m-p/47968#M12941</link>
      <description>Thank u so much . It worked &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Wed, 15 Dec 2010 12:31:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/update-statement-inside-a-proc-sql/m-p/47968#M12941</guid>
      <dc:creator>Rose</dc:creator>
      <dc:date>2010-12-15T12:31:49Z</dc:date>
    </item>
    <item>
      <title>Re: update statement inside a proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/update-statement-inside-a-proc-sql/m-p/365457#M64764</link>
      <description>&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;I was thinking based on this post that when I submit the following code it wouldn't update my speeds in my master table if it wasn't in my reference table but it seems to still set it to missing if it's not in the reference table. Am I missing something? &lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;sql&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;update&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&amp;nbsp;mastertable tableA&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; speed = (select Speed&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;from referencetable&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;where&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; (tableA.device = device and tablea.daydate between min_daydate AND max_daydate));&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;where&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; device in(select device from referencetable);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Jun 2017 17:28:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/update-statement-inside-a-proc-sql/m-p/365457#M64764</guid>
      <dc:creator>kmcnulty</dc:creator>
      <dc:date>2017-06-08T17:28:38Z</dc:date>
    </item>
    <item>
      <title>Re: update statement inside a proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/update-statement-inside-a-proc-sql/m-p/468452#M70752</link>
      <description>&lt;P&gt;Hello, I am trying to update an existing table&amp;nbsp;in the column called "stratum"&amp;nbsp;(which it is actually doing) which is about 129 records, but it is also changing the other values in that column to missing.&amp;nbsp;&amp;nbsp; I tried using the logic provided by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19773"&gt;@LewisC_sas&lt;/a&gt;&amp;nbsp;but it is still resolving the other values to missing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code attached.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
UPDATE C3B.RP_SAMPLE2 as  A
SET STRATUM = (select STRATUM FROM WORK.SAMPLE_RESTRAT as B
WHERE A.servicepointid=b.servicepointid AND A.recorderid=b.recorderid AND A.Sample_ID=B.Sample_ID AND B.Sample_ID IN (17))
WHERE Stratum IN (Select stratum from WORK.SAMPLE_RESTRAT as B);
quit;
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Jun 2018 16:57:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/update-statement-inside-a-proc-sql/m-p/468452#M70752</guid>
      <dc:creator>tobyfarms</dc:creator>
      <dc:date>2018-06-07T16:57:57Z</dc:date>
    </item>
  </channel>
</rss>

