BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ybz12003
Rhodochrosite | Level 12

Hello:

 

I would like to combine three tables with everything inside by ID and Place.   I am trying to use Proc SQL full join so that I don't need to sort the table.   I wrote the code below.   It seems didn't work.

 

proc sql;
   create table final as
   select a.*,  
             b.*,
             c.*
from test1 a
   full join test2 b on a.ID=b.ID and a.place=b.place  
full join test3 c on a.ID=c.ID and a.place=c.place  where date ne . order by ID place; quit;

 

1 ACCEPTED SOLUTION

Accepted Solutions
utrocketeng
Quartz | Level 8

good observation @novinosrin,

@ybz12003 you may need to add the 'a' alias to the date filter:

proc sql;
create table final as
select
a.*,
b.*,
c.*
from
test1 a
full join test2 b on a.ID = b.ID and a.Place = b.Place
full join test3 c on a.ID = c.ID and a.Place = c.Place
where a.date ne .
order by ID place;
quit;

View solution in original post

12 REPLIES 12
utrocketeng
Quartz | Level 8

something like this?

proc sql;
create table final as
select a.*,
b.*,
c.*
from
test1 a
full join test2 b on a.ID = b.ID and a.Place = b.Place
full join test3 c on a.ID = c.ID and a.Place = c.Place
where date ne .
order by ID place;
quit;

ybz12003
Rhodochrosite | Level 12

I got worning message:

WARNING: Variable id already exists on file WORK.FINAL.

WARNING: Variable place already exists on file WORK.FINAL.

novinosrin
Tourmaline | Level 20

Well that's exactly what rock engineer and I were discussing. You just need to list down col names rather than use *

ybz12003
Rhodochrosite | Level 12

Too many variables names to list them all, I have 900 for each table.

novinosrin
Tourmaline | Level 20

Switch to datastep. If you want code help, plz post a sample of HAVE and WANT

novinosrin
Tourmaline | Level 20

I think @utrocketeng  is in the right direction, but just concerned if date variable exists in more than one dataset, then you may have to prefix it with an alias to avoid ambiguity. 

utrocketeng
Quartz | Level 8

good observation @novinosrin,

@ybz12003 you may need to add the 'a' alias to the date filter:

proc sql;
create table final as
select
a.*,
b.*,
c.*
from
test1 a
full join test2 b on a.ID = b.ID and a.Place = b.Place
full join test3 c on a.ID = c.ID and a.Place = c.Place
where a.date ne .
order by ID place;
quit;

ybz12003
Rhodochrosite | Level 12

Actually, I popped up another question.   I not only want to have the overlapped ID in all of the table but the ID is not duplicated.   How to do this?

DBailey
Lapis Lazuli | Level 10

coalesce(a.id, b.id, c.id) as id

 

and...I think that if you have records in each table that are duplicate for id and place, then you may need to connect b.place to c.place to keep from generating a cartesian product.

LinusH
Tourmaline | Level 20
900 variables...just stop there. Unless you have a specific data mining task at hand, that is not a viable approach. For starters is makes programming awkward, which you just now should realize. So rethinking your data structure could be yiur6fira concern.
If you still want to go on this track, macro programming with DICTIONARY.CLOUMNS will be your best friend.
Data never sleeps
ybz12003
Rhodochrosite | Level 12

Thanks for all of your wonderful help!

Peter_C
Rhodochrosite | Level 12
I like to get more for less
Coalesce these date variables to select where only one is non-missing or "product" them to select where all are non-missing
PROC SQL ;
create table final as
select coalescec( a.id, b.id, c.id ) as abc_id
, coalescec( a.place, b.place, c.place) as abc_place
, coalesce( a.date, b.date, c.date) as abc_date
, max( a.date, b.date, c.date) as latest_date
, min( a.date, b.date, c.date) as earliest_date
, *
From
test1 a
full join test2 b on a.ID = b.ID and a.Place = b.Place
full join test3 c on a.ID = c.ID and a.Place = c.Place
where a.date * b.date *c.date is not null
(Alternatively
Where latest_date is not null)

order by abc_ID, abc_place;
quit;

Sorry this is untested as I haven't yet found a way of testing sas code on this smartphone

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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