BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

3 REPLIES 3
Patrick
Opal | Level 21

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

Tom
Super User Tom
Super User

Look into using the MODIFY statement.

Look at Example 3 in this help document.

SAS(R) 9.3 Statements: Reference

LinusH
Tourmaline | Level 20

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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