I am trying to update one column in one table using value from another table as below.
Is there a way to update the column only if it has null value or else keep the previous value and do no updates. Please let me know what wrong I am doing in the code below.
proc sql;
update table customer a
set a.pre_diags =
case
when a.pre_diags is null then b.pre_diags
else a.pre_diags
end
from retails b;
quit;
Regards
Kajal
You are testing in the wrong place. Apply the test when selecting which observations to update.
update CUSTOMER a
set PRE_DIAGS = (......)
where missing(PRE_DIAGS)
;
Also which of the values of PRE_DIAGS in RETAILS do you want to use to replace the current observations value of PRE_DIAGS in CUSTOMER??
Is there some type of customer id in both datasets that could be used to know where in RETAILS to look? Could the same customer appear more than once in RETAILS? If so which value do you want to use?
update CUSTOMER a
set PRE_DIAGS =
(select max(b.pre_diags) from RETAILS b
where a.customer_id = b.customer_id)
where missing(PRE_DIAGS)
;
See here.
This works:
data HAVE;
set SASHELP.CLASS;
if NAME='Alice' then AGE=.;
run;
proc sql;
update HAVE a
set AGE = (select AGE from SASHELP.CLASS b where a.NAME=b.NAME)
where a.AGE = .; inner join
quit;
Edit:
Adding that the inner join
syntax would be a lot more efficient for large updates, but does not seem to be supported by SAS.
You are testing in the wrong place. Apply the test when selecting which observations to update.
update CUSTOMER a
set PRE_DIAGS = (......)
where missing(PRE_DIAGS)
;
Also which of the values of PRE_DIAGS in RETAILS do you want to use to replace the current observations value of PRE_DIAGS in CUSTOMER??
Is there some type of customer id in both datasets that could be used to know where in RETAILS to look? Could the same customer appear more than once in RETAILS? If so which value do you want to use?
update CUSTOMER a
set PRE_DIAGS =
(select max(b.pre_diags) from RETAILS b
where a.customer_id = b.customer_id)
where missing(PRE_DIAGS)
;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.