Is there a more efficient way to write this SQL Update Query?
It has to be an update, not a table overwrite, though the update can be through SQL or a data step.
Thanks!
PROC SQL;
UPDATE TABLE1
SET VAR1 = (select P.VAR1 from TABLE2 as P where P.GROUP = I.GROUP and P.NUM = &ID)
, VAR2 = (select P.VAR2 from TABLE2 as P where P.GROUP = I.GROUP and P.NUM = &ID)
, VAR3 = (select P.VAR3 from TABLE2 as P where P.GROUP = I.GROUP and P.NUM = &ID)
, VAR4 = (select P.VAR4 from TABLE2 as P where P.GROUP = I.GROUP and P.NUM = &ID)
, VAR5 = (select P.VAR5 from TABLE2 as P where P.GROUP = I.GROUP and P.NUM = &ID)
;
QUIT;
Look into using the MODIFY statement.
Look at Example 3 in this help document.
You could use the SAS UPDATE statement SAS(R) 9.4 Statements: Reference, Third Edition
You could load "table2" into a hash table, look-up the values and if there is a match to the hash replace the record using a SAS MODIFY statement with REPLACE (this way no sorting of table1 required).
For your SQL: You could subset table2 (where NUM=&ID) before using it in your sub-selects
Look into using the MODIFY statement.
Look at Example 3 in this help document.
I agree with Tom, MODIFY is usually the fastest way to do updates, given that the transaction table is relatively small.
If you wish do keep the SQL, be sure to have proper indexes (on both master and transaction tables), it could speed things up.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.