BookmarkSubscribeRSS Feed
srmakwana2009
Fluorite | Level 6

I am trying to merge many to many variables using proc sql. i am getting syntex error . can anyone please help what is syntex error is? please help. it's urgent

 BELOW IS MY CODE


proc sort data=pk_dose0;
by nsid date time dose;
run;

proc sort data=dose_poppk;
by nsid date time dose;
run;

proc sql;
create table aa as
select * from pk_dose0 as a join dose_poppk as b on
(a.nsid=b.nsid)
(a.date=b.date)
(a.time=b.time);
quit;


proc sql;
create table both as
select
coalesce(a.nsid, b.nsid) as id,
coalesce(a.date, b.date) as date,
coalesce(a.time, b.time) as time,
farm
from
pk_dose0 as a full join
dose_poppk as b
on a.id = b.id,
on a.date = b.date,
on a.time = b.time);
quit;

 

BELOW IS SCREENSHOT OF ERROR.

srmakwana2009_0-1658639162905.png

 

 

2 REPLIES 2
ballardw
Super User

Commas in the logic instead of how to add the comparison logic. Only one ON per join and you use AND and OR, possibly with ( ) to group a condition to add to any comparison whether an ON( or WHERE) clause.

proc sql;
create table both as
select
coalesce(a.nsid, b.nsid) as id,
coalesce(a.date, b.date) as date,
coalesce(a.time, b.time) as time,
farm
from
pk_dose0 as a full join
dose_poppk as b
on a.id = b.id
    and a.date = b.date
    and a.time = b.time;
quit;

 

However you have another syntax problem with a ) after = b.time that does not have a matching (.

 

You likely have an additional logic issue understanding your problem: Use of coalesce(a.time, b.time) coupled with the join on a.time=b.time. With the JOIN requirement A.time would be the same as B.time always so the coalesce is basically not adding anything. IF you expect to have a missing A.time or B.time where coalesce would make sense then your JOIN criteria have to be significantly different. Same for the A.date and B.date.

You did not actually describe what sort of comparison you want. A danger with posting code that does not work without a description of what is needed is that we will quite often assume that the simplest thing that cleans up errors is what is needed.

It may be that  what you wanted could be:

proc sql;
create table both as
select
coalesce(a.nsid, b.nsid) as id,
coalesce(a.date, b.date) as date,
coalesce(a.time, b.time) as time,
farm
from
pk_dose0 as a full join
dose_poppk as b
on a.id = b.id
    and (a.date = b.date
         or a.time = b.time
        )
 ;
quit;

This would be match on date or the time. But this might be a poor choice if you have the same times on different dates along with the same time on missing dates for the same Id variable values.

 

 

If you actually expect to have missing values and then align them some how you need to provide example data of the two sets and how you expect the result to look after the match along with any matching rules.

 

Save yourself some time and instead of creating a PICTURE of the log just copy the text from the log. The on the forum open a text box using the </> that appears above the message window and paste the text.

tarheel13
Rhodochrosite | Level 12

In the join condition instead of , put 'and'

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

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