BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
HitmonTran
Pyrite | Level 9

I want to do a left join on 2 variables.

 

this is my code but there's an error because of the "on" statement "on a.subject, a.date = b.subject, b.date;"

proc sql;
  create table data3 as  select * 
  from data1 as a left join data2 as b 
  on a.subject, a.date = b.subject, b.date;
quit;
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
proc sql;
  create table data3 as  
select * from data1 as a
left join data2 as b on a.subject = b.subject and a.date=b.date; quit;

Separate your conditions with an AND 

View solution in original post

3 REPLIES 3
Reeza
Super User
proc sql;
  create table data3 as  
select * from data1 as a
left join data2 as b on a.subject = b.subject and a.date=b.date; quit;

Separate your conditions with an AND 

PaigeMiller
Diamond | Level 26
proc sql;
  create table data3 as  select * 
  from data1 as a left join data2 as b 
  on a.subject = b.subject and a.date = b.date;
quit;
--
Paige Miller
ballardw
Super User

Hint: when you get errors then include the code with the errors, the entire procedure, data step or macro, with all the messages.

In your case the error would have looked like this:

765  proc sql;
766    create table data3 as  select *
767    from sashelp.class as a left join sashelp.class as b
768    on a.sex, a.age = b.sex, b.age;
                       -
                       22
                       76
ERROR 22-322: Syntax error, expecting one of the following: a name, ;, (, ',', ANSIMISS, AS,
              CROSS, EXCEPT, FULL, GROUP, HAVING, INNER, INTERSECT, JOIN, LEFT, NATURAL, NOMISS,
              ORDER, OUTER, RIGHT, UNION, WHERE.

ERROR 76-322: Syntax error, statement will be ignored.

The presence of all the join options tells us that the error is that where the = in your code appears than SAS is expecting something related to another join. The comma was interpreted as a Cartesian join instruction similar to

proc sql;
   select a.*,b.*
   from someset as a, otherset as b
  ;
quit;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 1748 views
  • 0 likes
  • 4 in conversation