BookmarkSubscribeRSS Feed
PJorge
Calcite | Level 5

Hi,

I'm trying to add two tables with similar data but I have more than one possible match between them. Example below:

Table 1

Field 1Field 2Field 3
LAStreet 1House 1
LAStreet 2House 1
LAStreet 2House 2
NYStreet 1House 1
NYStreet 1House 2
NYStreet 2House 1

and Table 2

Field 1Field 2Field 3Code
LAStreet 1House 1Code1
LAStreet 2House 1Code12
LAStreet 2House 2Code54
NYStreet 1Code15
NYStreet 2Code43
NYStreet 3Code67

I want to match the code on Table 2 to each row on Table 1 however for NY it doesn't have a field 3 specification and the code should be linked only to street number whatever the house number is.

I want the output to be something like this:

Field 1Field 2Field 3Code
LAStreet 1House 1Code1
LAStreet 2House 1Code12
LAStreet 2House 2Code54
NYStreet 1House 1Code15
NYStreet 1House 2Code15
NYStreet 2House 1Code43

Is there any way I do this on SAS EG directly?

Thanks

2 REPLIES 2
Fugue
Quartz | Level 8

I'm not sure what you mean by "do this in SAS EG directly", but you could accomplish this in several different ways in EG.

The simpliest (in my view) is to manually add a program node and then hand-write PROC SQL or data step code. But, you could also use Query Builder to join the tables and output two data sets (one with matches for both street and house, the other with matches where house is missing in table1) and then join them together.

UrvishShah
Fluorite | Level 6

Hi,

First, just add two variables in your TABEL1 as below:

temp_field2 = field2;

temp_field3 = field3;

And use the following code to merge back with tabel1 to add CODE information...

data both(drop = field2 field3 rename = (temp_field2 = field2 temp_field3 = field3));

   merge one(in = a) two(in = b);

   by field1 field2;

   if a;

run;

-Urvish

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 1236 views
  • 1 like
  • 3 in conversation