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

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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