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.
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.
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.