BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Longimanus
Quartz | Level 8

To all SAS Code Guru's out there! 🙂  This should be pretty easy (I can do this in other languages easily) but I can't do it in SAS.
Never tried it. Before I spend time looking it up I'll ask here (deadline to meet).  So this is what I have:

 

Table A has more than one row per key (key_var).  This is the table  I need to update with the information in Table B that only 

has one row per key.  Both tables are indexed and sorted.  

 

Have                                           Corrections                                           Want

Table_A                                      Table_B                                                  Table_A

Key_var  Var_1   Var_2            Key_var  Fix_Var_1    Fix_Var_2         Key_var      Var_1   Var_2

1                X           A                   1               XX                AA                     1                    XX        AA

2               Y            B                   2              YY                BB                      2                   YY        BB

2               Y            B                   3              ZZ                 CC                     2                   YY        BB

3               Z            C                                                                                     3                   ZZ        CC

 

My code (which unfortunately only updates the first occurance): 

 

data TABLE_A;
   set TABLE_B;
   modify TABLE_A  key=key_var; 
   if _iorc_=0 then do;
     Var_1 = Fix_var_1;
     Var_2 = Fix_var_2;
     replace;
   end;
   else _error_=0;
run;
 
With the above  I only update the first row in Table-A that has the equivalent key in Table-B. Not the rest.  Is there a simple way to alter this in the above or do I need to solve this completely different.  I will keep trying to figure out HOW but like to see what you guys come up with. For a "Guru" this should be a walk in the park. Right? 
 
This is what I get now:  (not good enough as you see). 
 

Have                                           Corrections                                           Want

Table_A                                      Table_B                                                  Table_A

Key_var  Var_1   Var_2            Key_var  Fix_Var_1    Fix_Var_2         Key_var      Var_1   Var_2

1                X           A                   1               XX                AA                     1                    XX        AA

2               Y            B                   2              YY                BB                      2                   YY        BB

2               Y            B                   3              ZZ                 CC                     2                   Y           B         <---- 😒

3               Z            C                                                                                     3                   ZZ        CC

 

Any suggestion? Either a fix in the above or an entire other method  Thanx already now !!! 🤓

1 ACCEPTED SOLUTION
2 REPLIES 2
Longimanus
Quartz | Level 8

Hi folks! 

I have another way to fix this.  Another method that I will try.  I will join the two tables in SQL  so I get each correction row in the FIX-TABLE (Table-B) to be joined to each row in Table-A.  When I have all information in one table it's easy to get the result I want! 😊  

That said:  I am still curious how YOU'd solve this!

 

Have a nice day wherever you are!

 

Cheers!
Longimanus  

 

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 557 views
  • 0 likes
  • 2 in conversation