<?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: proc sql update in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-update/m-p/555321#M154549</link>
    <description>&lt;P&gt;If you insist on using PROC SQL to do this (and not the usual sort by / last.id logic), then you can achieve your desired result with something like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
input id A B C;
cards;
1 2 3 4
2 5 5 5
3 7 6 5
4 9 6 1
5 5 7 2
6 7 8 1
run;

data table2 ;
input id A B C;
cards;
1 2 3 4
1 4 2 5
1 5 3 12
1 7 34 3
2 5 5 5
2 6 12 0
2 9 3 2
2 11 3 1
3 7 6 5
3 23 3 2
3 12 12 44
4 4 23 33
4 9 6 1
5 5 7 2
6 7 8 1
run;

proc sql;
   create table temp as
      select distinct * from 
         (
         select * from table1 
         union all
         select * from table2
         )
      group by ID
      having A=max(A);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/149294"&gt;@ali_far&lt;/a&gt;&amp;nbsp;did this solve your problem?&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 01 May 2019 23:05:15 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2019-05-01T23:05:15Z</dc:date>
    <item>
      <title>proc sql update</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-update/m-p/555285#M154525</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have a simple question:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data table 1;&lt;BR /&gt;input id A B C;&lt;BR /&gt;cards;&lt;BR /&gt;1 2 3 4&lt;BR /&gt;2 5 5 5&lt;BR /&gt;3 7 6 5&lt;BR /&gt;4 9 6 1&lt;BR /&gt;5 5 7 2&lt;BR /&gt;6 7 8 1&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data&amp;nbsp;table 2 ;&lt;BR /&gt;input id A B C;&lt;BR /&gt;cards;&lt;BR /&gt;1 2 3 4&lt;BR /&gt;1 4 2 5&lt;BR /&gt;1 5 3 12&lt;BR /&gt;1 7 34 3&lt;BR /&gt;2 5 5 5&lt;BR /&gt;2 6 12 0&lt;BR /&gt;2 9 3 2&lt;BR /&gt;2 11 3 1&lt;BR /&gt;3 7 6 5&lt;BR /&gt;3 23 3 2&lt;BR /&gt;3 12 12 44&lt;BR /&gt;4 4 23 33&lt;BR /&gt;4 9 6 1&lt;BR /&gt;5 5 7 2&lt;BR /&gt;6 7 8 1&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to update&amp;nbsp; variable B in Table 1 where the id in two tables are the same (first condition) and A is at maximum in table 2.&lt;/P&gt;&lt;P&gt;Because for some id( say 1, 2) in table 2&amp;nbsp; I have several rows.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, it will be the updated table:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data table 1;&lt;BR /&gt;input id A B C;&lt;BR /&gt;cards;&lt;BR /&gt;1 2 34 4&lt;BR /&gt;2 5 3 5&lt;BR /&gt;3 7 3 5&lt;BR /&gt;4 9 6 1&lt;BR /&gt;5 5 7 2&lt;BR /&gt;6 7 8 1&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc SQL;&lt;BR /&gt;update table 1 set B= (select B from table 2 where table 1.id=table 2.id&amp;nbsp; and table 2.A=max); quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I know the above code, after the and is not correct!!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 May 2019 06:02:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-update/m-p/555285#M154525</guid>
      <dc:creator>ali_far</dc:creator>
      <dc:date>2019-05-01T06:02:02Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql update</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-update/m-p/555290#M154529</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc SQL;
   update table1 
   set B = (select distinct B from table2 
               where table1.id=id
               group by ID
               having A=max(A)); 
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 May 2019 07:17:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-update/m-p/555290#M154529</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-05-01T07:17:39Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql update</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-update/m-p/555298#M154534</link>
      <description>Thanks for your reply. Your code works well for what I wanted but unfortunately I got one mistake in the want data set:&lt;BR /&gt;&lt;BR /&gt;Here is the one I want:&lt;BR /&gt;&lt;BR /&gt;data updated table 1;&lt;BR /&gt;input id A B C;&lt;BR /&gt;cards;&lt;BR /&gt;1 7 34 3&lt;BR /&gt;2 11 3 1&lt;BR /&gt;3 23 3 2&lt;BR /&gt;4 9 6 1&lt;BR /&gt;5 5 7 2&lt;BR /&gt;6 7 8 1&lt;BR /&gt;run;&lt;BR /&gt;</description>
      <pubDate>Wed, 01 May 2019 08:26:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-update/m-p/555298#M154534</guid>
      <dc:creator>ali_far</dc:creator>
      <dc:date>2019-05-01T08:26:44Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql update</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-update/m-p/555307#M154542</link>
      <description>&lt;P&gt;Ok, so B is not the only variable you want to update? What is the logic here? &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 01 May 2019 09:10:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-update/m-p/555307#M154542</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-05-01T09:10:45Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql update</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-update/m-p/555310#M154545</link>
      <description>Thanks for your reply.&lt;BR /&gt;exactly, other variables might also get updated.&lt;BR /&gt;&lt;BR /&gt;Table 1 gets updated using the same id with table 2 where we the id has the highest value of B. Then other variable relevant to that ID also should be updated.&lt;BR /&gt;&lt;BR /&gt;Hope it is clear!&lt;BR /&gt;appreciate your time</description>
      <pubDate>Wed, 01 May 2019 09:17:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-update/m-p/555310#M154545</guid>
      <dc:creator>ali_far</dc:creator>
      <dc:date>2019-05-01T09:17:32Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql update</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-update/m-p/555316#M154547</link>
      <description>&lt;P&gt;But that description does not comply with your newly posted &lt;STRONG&gt;want&amp;nbsp;&lt;/STRONG&gt;data set? Here, it seems that you want the table1 updated with values from table2 where the ID has the highest value of &lt;STRONG&gt;A&lt;/STRONG&gt; and not &lt;STRONG&gt;B&lt;/STRONG&gt;?&lt;/P&gt;</description>
      <pubDate>Wed, 01 May 2019 10:51:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-update/m-p/555316#M154547</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-05-01T10:51:23Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql update</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-update/m-p/555321#M154549</link>
      <description>&lt;P&gt;If you insist on using PROC SQL to do this (and not the usual sort by / last.id logic), then you can achieve your desired result with something like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
input id A B C;
cards;
1 2 3 4
2 5 5 5
3 7 6 5
4 9 6 1
5 5 7 2
6 7 8 1
run;

data table2 ;
input id A B C;
cards;
1 2 3 4
1 4 2 5
1 5 3 12
1 7 34 3
2 5 5 5
2 6 12 0
2 9 3 2
2 11 3 1
3 7 6 5
3 23 3 2
3 12 12 44
4 4 23 33
4 9 6 1
5 5 7 2
6 7 8 1
run;

proc sql;
   create table temp as
      select distinct * from 
         (
         select * from table1 
         union all
         select * from table2
         )
      group by ID
      having A=max(A);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/149294"&gt;@ali_far&lt;/a&gt;&amp;nbsp;did this solve your problem?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 May 2019 23:05:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-update/m-p/555321#M154549</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-05-01T23:05:15Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql update</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-update/m-p/556356#M154991</link>
      <description>Hi draycut,&lt;BR /&gt;&lt;BR /&gt;your code works well</description>
      <pubDate>Mon, 06 May 2019 07:13:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-update/m-p/556356#M154991</guid>
      <dc:creator>ali_far</dc:creator>
      <dc:date>2019-05-06T07:13:33Z</dc:date>
    </item>
  </channel>
</rss>

