BookmarkSubscribeRSS Feed
jcis7
Pyrite | Level 9

Appreciate any help you can give.

I have one table with facility names and some county names named Facility

I have another table with all county names and email addresses named Contacts

Facility table has only some county names listed in the Contacts Table so I need ultimately the email addresses from the Contacts table for only the county names listed in the Facility Table.

The Facility Table has the county names listed more than once since more than one Facility is listed per county.

The Contacts Table has the county name listed more than once because there's more than one contact for some counties.

How do I get the email addresses for only the counties listed in the Facility Table?

Facility Table

County            Facility

Alameda         Happy Days

Contra Costa  Sunshine Daycare

Contra Costa   BusyBees

Inyo                 Elmwood Montessori

San Joaquin    Buzy Beez

etc...

Contacts Table

County             Email Address                 

Alameda           hamwood@alameda.gov

Alameda          stern@alameda.gov

Contra Costa    lily@cc.gov

  Inyo                 sch@iy.com

Inyo                   tom@iy.com

San Joaquin      kj1@sq.gov

5 REPLIES 5
pradeepalankar
Obsidian | Level 7

data Facility;

infile cards dlm='|';

input County :$50. Facility :$50. ;

cards;

Alameda|Happy Days

Contra Costa|Sunshine Daycare

Contra Costa|BusyBees

Inyo|Elmwood Montessori

San Joaquin|Buzy Beez

;

  data contacts;

  infile cards dlm='|';

  input County :$50. Email :$50.; 

cards;

Alameda|hamwood@alameda.gov

Alameda|stern@alameda.gov

Contra Costa|lily@cc.gov

Inyo|sch@iy.com

Inyo|tom@iy.com

San Joaquin|kj1@sq.gov

;

run;

proc sql;

create table want as

select f.county,f.facility,c.email from facility f inner join contacts c

on f.county=c.county;

quit;

Fugue
Quartz | Level 8

Get rid of duplicate county records:

proc sql;

      select f.county

            , c.email

      from contacts c

            inner join

            ( select distinct county

            from facility ) as f

            on c.county = f.county

;

quit;

jcis7
Pyrite | Level 9


This worked great after adding an 'as' after 'contacts'

Question:  what does the phrase 'inner join' tell sas to do?  join the contacts table with the newly made table f?

why is it sometimes you can ask sas to do an inner join in sql without specifying 'inner join' but for this you have to?  Is it because there

is a subquery?

Appreciate your help!

overmar
Obsidian | Level 7

The term inner join implies that you only want to keep all of the records which match in both tables. When I read your query it looked like you wanted a table with all of the facilities and all of the email addresses that you could match, rather than only those which were in both tables. The way that the example code was shown implied that you had an email address for every county, but my guess is that you do not. If this was what you wanted then you should specify left rather than inner join with the facility table being specified first in your statement.

Patrick
Opal | Level 21

Hi

"why is it sometimes you can ask sas to do an inner join in sql without specifying 'inner join' but for this you have to?"

This is just SQL syntax allowing you to code an inner join implicit or explicit. Below how it would look like implicit.

proc sql;

  select f.county

    , c.email

  from

    contacts as c

    ,

    ( select distinct county

      from facility ) as f

  where c.county = f.county

  ;

quit;

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!

How to Concatenate Values

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.

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
  • 5 replies
  • 1044 views
  • 6 likes
  • 5 in conversation