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?

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 4203 views
  • 3 likes
  • 4 in conversation