DATA Step, Macro, Functions and more

Faster way to write a SQL Update Query

Accepted Solution Solved
Reply
Super User
Posts: 19,822
Accepted Solution

Faster way to write a SQL Update Query

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;


Accepted Solutions
Solution
‎08-18-2014 10:27 PM
Super User
Super User
Posts: 7,060

Re: Faster way to write a SQL Update Query

Look into using the MODIFY statement.

Look at Example 3 in this help document.

SAS(R) 9.3 Statements: Reference

View solution in original post


All Replies
Respected Advisor
Posts: 4,173

Re: Faster way to write a SQL Update Query

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

Solution
‎08-18-2014 10:27 PM
Super User
Super User
Posts: 7,060

Re: Faster way to write a SQL Update Query

Look into using the MODIFY statement.

Look at Example 3 in this help document.

SAS(R) 9.3 Statements: Reference

Super User
Posts: 5,431

Re: Faster way to write a SQL Update Query

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.

Data never sleeps
🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 601 views
  • 0 likes
  • 4 in conversation