Desktop productivity for business analysts and programmers

Adding two tables with multiple row matches between them

Reply
Occasional Contributor
Posts: 6

Adding two tables with multiple row matches between them

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

Super Contributor
Posts: 307

Re: Adding two tables with multiple row matches between them

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.

Regular Contributor
Posts: 195

Re: Adding two tables with multiple row matches between them

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

Ask a Question
Discussion stats
  • 2 replies
  • 203 views
  • 1 like
  • 3 in conversation