Desire to match merge two data sets. The wrinkle is that If there is no match using a primary match variable, I'd like to also try to match with a secondary match variable. There is probably a simple way to do this but I haven't found it. Here's an example:
Data A;
TICKER Price
A 10
AA 11
S 21
B 50
Data B;
TICKER SECOND_TICKER OTHER_STUFF;
A A Consumer
AA ZZ REIT
KMT S Consumer
B BMT Industrial
;
Want: A match merge of A and B using ticker, but if no match on ticker, then try finding one by using "second_ticker" as the match variable. This would be the ideal output:
Data C:
TICKER SECOND_TICKER PRICE OTHER_STUFF
A A 10 Consumer
AA zz 11 REIT
S S 21 Consumer
B BMT 50 Industrial
So, there is no match between datasets A and B for the observation with ticker=S when using ticker as the primary match variable. However, there is a match for the no-match observation (ticker= S) between datasets A and B if we try again searching for matches using second_ticker in Dataset B and ticker in A
I can accomplish this in a messy way by defining temporary match variables and doing multiple passes through the data - eventually combining partial results. But, Is there an elegant solution, or am I missing something obvious (SAS v9.4)?
An elegant, relatively fast approach would use two hash tables. In one DATA step:
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.