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

SAS newbie here.   I'm trying to update a table with data from multiple other tables.  I've tried to do it through a proc SQL statement as I'm much more familiar with SQL syntax than SAS data steps, but the proc SQL statement is either running very slowly (there are millions of records being updated in the query) or just not working in SAS.  So I'm hoping to find something much more efficient.

 

My Proc SQL update code looks like this right now:

 

update work.table_1 a

SET field_2 = (select field_2 from table_2 b

            where a.key_1 = b.key_1

               and b.year_db = '2019'

               and b.period_val = '5'),

        field_3 = (select field_3 from table_3 c

            where a.key_1 = c.key_1

               and c.year_db = '2019'

               and c.period_val = '5');

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
tsap
Pyrite | Level 9

Here is a possible alternative to the logic that you are currently using. One thing I will note is that I had to make a lot of assumptions to put this logic together, so it may not generate the intended output you needed. For example, I don't know whether field_2 and field_3 already exist on Table_1 or not. And if it does exist, whether you want the value to update with the values from table_2 and table_3 every time, or only when the value on table_2 or table_3 is not null, otherwise keep the value on table_1 for these fields. There are a couple of other unknowns that I assumed in creating this logic.

 

If this doesn't accomplish what you need, please submit logic to create small dummy datasets to mimic your table_1, table_2, and table_3... and a rough mockup of what the output should look like. That will give us the best idea of what the logic needs to look like to accomplish your request.

 

PROC SQL;
CREATE TABLE WORK.New_Table	AS
	SELECT
		  a.*
		, b.field_2
		, c.field_3

	FROM 	  Table_1		AS a
	LEFT JOIN Table_2		AS b	ON 	a.key_1 = b.key_1
									AND b.year_db = '2019'
									AND b.period_val = '5'
	LEFT JOIN Table_3		AS c	ON  a.key_1 = c.key_1
									AND c.year_db = '2019'
									AND c.period_val = '5';
QUIT;

If you need the final produced table to be named table_1 you can certainly change what I have (new_table), or you can do a datastep afterwards to rename the table, etc.

 

hope this helps.

View solution in original post

1 REPLY 1
tsap
Pyrite | Level 9

Here is a possible alternative to the logic that you are currently using. One thing I will note is that I had to make a lot of assumptions to put this logic together, so it may not generate the intended output you needed. For example, I don't know whether field_2 and field_3 already exist on Table_1 or not. And if it does exist, whether you want the value to update with the values from table_2 and table_3 every time, or only when the value on table_2 or table_3 is not null, otherwise keep the value on table_1 for these fields. There are a couple of other unknowns that I assumed in creating this logic.

 

If this doesn't accomplish what you need, please submit logic to create small dummy datasets to mimic your table_1, table_2, and table_3... and a rough mockup of what the output should look like. That will give us the best idea of what the logic needs to look like to accomplish your request.

 

PROC SQL;
CREATE TABLE WORK.New_Table	AS
	SELECT
		  a.*
		, b.field_2
		, c.field_3

	FROM 	  Table_1		AS a
	LEFT JOIN Table_2		AS b	ON 	a.key_1 = b.key_1
									AND b.year_db = '2019'
									AND b.period_val = '5'
	LEFT JOIN Table_3		AS c	ON  a.key_1 = c.key_1
									AND c.year_db = '2019'
									AND c.period_val = '5';
QUIT;

If you need the final produced table to be named table_1 you can certainly change what I have (new_table), or you can do a datastep afterwards to rename the table, etc.

 

hope this helps.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1 reply
  • 649 views
  • 1 like
  • 2 in conversation