BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8

update :

TableA
Id Value
1 abc
2 pqr

 

TableB
Id Value
1
2 pqr

how to update tableb for an id if value is missing, update with valur from tableA?

Final output:

TableB
Id Value
1 abc
2 pqr

7 REPLIES 7
novinosrin
Tourmaline | Level 20
data a;
input id value $;
cards;
1 abc
2 pqr
;
data b;
input id value $;
cards;
1 .
2 pqr
;
data b;
  modify b a;
  by id;
  if _IORC_=0 then replace;
  else _ERROR_=0;
run;
SASPhile
Quartz | Level 8

if there are multiple columns and only one column is to be updated, like below:

 

I dont want score to be replaced. only value ,the below logic doesnt work,right?

 

data a;
input id value $ score;
cards;
1 abc 350
2 pqr 275
;
data b;
input id value $3-6 score ;
cards;
1 371
2 pqr 372
;
run;
data b;
modify b a;
by id;
if _IORC_=0 then replace;
else _ERROR_=0;
run;

VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

@SASPhile please post a better sample dataset that represents your needs.  You are piece mealing your request.  Don't play the manager role in requesting results please.

 

novinosrin
Tourmaline | Level 20

data a;
input id value $ score;
cards;
1 abc 350
2 pqr 275
;
data b;
input id value $ score ;
cards;
1 . 371 
2 pqr 372
;

proc sql;
update b
SET value = (SELECT value
                  FROM a
                  WHERE b.id = a.id)
WHERE value=' ';
quit;
novinosrin
Tourmaline | Level 20

And as a matter of fact, you can push the proc sql update query in-database and have it done at that level rather than at SAS level.

novinosrin
Tourmaline | Level 20

Assuming you are just updating value sourcing from Table A, you could use dataset option to keep id and value alone

 

data b;
  modify b a(keep=id value);/*drop the rest*/
  by id;
  if _IORC_=0  then replace;
  else _ERROR_=0;
run;
Astounding
PROC Star

If TableB never has multiple observations for the same ID, you could use:

 

data want;

update b (in=keepme) a;

by id;

if keepme;

run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 867 views
  • 1 like
  • 4 in conversation