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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 740 views
  • 1 like
  • 3 in conversation