BookmarkSubscribeRSS Feed
FatCaptain
Fluorite | Level 6
I have 2 tables and I'm wanting to update the values in the first with the corresponding values in the second, e.g.

table1
a 1
b 2
c 3
d 4

table2
b 5
c 6

to result in;

table1
a 1
b 5
c 6
d 4


I'm not that familiar with SAS SQL syntax and the following code I wrote is displaying an error.

PROC SQL ;
UPDATE
table1 AS a
LEFT JOIN
table2 AS b
ON
a.var1 EQ b.var1
SET
a.var2 = b.var2
;
QUIT ;


65 LEFT JOIN
----
79
76
ERROR 79-322: Expecting a SET.

ERROR 76-322: Syntax error, statement will be ignored.


Does SAS SQL support this kind of updating?

As always, any help with this is greatly appreciated.

Cheers,

Fat Captain.
6 REPLIES 6
Oleg_L
Obsidian | Level 7
One of many ways.

[pre]

data table1;
infile cards;
input var1 $ var2;
cards;
a 1
b 2
c 3
d 4
;
data table2;
infile cards;
input var1 $ var2;
cards;
b 5
c 6
;
proc sql noprint;
create table table1 as select a.var1, coalesce(b.var2,a.var2) as var2
from table1 as a left join table2 as b on a.var1=b.var1;
quit;
[/pre]
SAS83
Fluorite | Level 6
Another way.. Make sure to sort the two tables before merge .. but for this case they are already sorted so no sorting is done..

data table1;
infile cards;
input var1 $ var2 @@;
cards;
a 1 b 2 c 3 d 4
;
data table2;
infile cards;
input var1 $ var2 @@;
cards;
b 5 c 6
;

data x;
merge table1 table2;
by var1;
run;
SAS83
Fluorite | Level 6
Make sure Table1 is first then Table2 in the merge statement.. if you dont use ina and inb....

data table1;
infile cards;
input var1 $ var2 @@;
cards;
a 1 b 2 c 3 d 4
;
data table2;
infile cards;
input var1 $ var2 @@;
cards;
b 5 c 6
;

data x;
merge table1 (in=ina) table2 (in=inb);
by var1;
if ina;
run;

this is similar to SQL left join..
Reeza
Super User
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.

[pre]
data have1;
input var1 $1 var2;
datalines;
a 1
b 2
c 3
d 4
;
run;

data have2;
input var1 $1 var2;
datalines;
b 5
c 6
;
run;


proc sql;
update have1 as h1
set var2=(select var2 from have2 as h2 where h1.var1=h2.var1)
where var1 in (select var1 from have2);
quit;
[/pre]
PhilFromGER
SAS Employee

Reeza provides the ideal solution, eliminating the need to create a new data set.

 

proc sql;
 update have1 as h1
 set var2=(select var2 from have2 as h2 where h1.var1=h2.var1)
 where var1 in (select var1 from have2);
 quit;

 

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.

FatCaptain
Fluorite | Level 6
Thank you all for your responses.

Cheers,

Fat Captain.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 23977 views
  • 4 likes
  • 5 in conversation