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

Hello!

 

I am learning PROC SQL in more detail.

 

For the given example subquery:

proc sql;
        select empid, lastname, firstname, city, state
           from sasuser.staffmaster
           where empid in
              (select empid
                 from sasuser.payrollmaster
                 where month(dateofbirth)=2);
quit ;

I wrote what I thought was the equivalent using Join:

proc sql;
	select 
	 	empid, lastname, firstname, city, state
	from 
		sassuer.staffmaster as a
		inner join sasuser.payrollmaster as b
		where month(dateofbirth)=2 
	on a.empid = b.empid ;
quit;

[1] Is the correct?

[2] if so, what is the benefit to using subqueries?

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

1) There are a few things wrong with your code.. EmpId is in both tables, so you have to reference one of them. Also, you have a typo in a libref. 

 

 

proc sql;
	select 
	 	a.empid, 
      lastname, 
      firstname, 
      city, 
      state
	from 
		sasuser.staffmaster as a
	inner join sasuser.payrollmaster as b
		on month(dateofbirth)=2 
	and a.empid = b.empid ;
quit;

2)

 

I think this will help you: When to Use Joins and Subqueries

View solution in original post

3 REPLIES 3
Patrick
Opal | Level 21

@mglogan 

Let's say you have in table sasuser.staffmaster one row with empid=1 and in table sasuser.payrollmaster five rows with empid=1

 

With your code:

The inner join will return 5 rows, the subquery will return 1 row  ...also investigate what EXISTS can do for you.

PeterClemmensen
Tourmaline | Level 20

1) There are a few things wrong with your code.. EmpId is in both tables, so you have to reference one of them. Also, you have a typo in a libref. 

 

 

proc sql;
	select 
	 	a.empid, 
      lastname, 
      firstname, 
      city, 
      state
	from 
		sasuser.staffmaster as a
	inner join sasuser.payrollmaster as b
		on month(dateofbirth)=2 
	and a.empid = b.empid ;
quit;

2)

 

I think this will help you: When to Use Joins and Subqueries

VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

the difference is control over what element populated the results table.

did b over write a?

do I want the value of a or b?

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3418 views
  • 3 likes
  • 4 in conversation