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

I just want to double check my thinking on this...  When using multiple set operators in SQL in a single SQL statement, are the actions performed incrementally?

 

For example, I have in my code:

 

 

proc sql;
select distinct vv.naics_code
from eds.pt_naic_codes as vv

union

select distinct loc.naics_code 
from eds.pa_location as loc

union

select distinct qcew.naics_code_pt_naic_codes as naics_code
from eds.ps_qcew_data as qcew

except

select sched.naics_code
from eds.ps_scheduling_naics as sched;
quit;

 

and I want it to work so that it appends the first three tables before excluding the fourth table.  

 

As written, will I be getting what I expect?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

The most reliable place to look is the SAS documentation which says:

 

"A query expression with set operators is evaluated as follows.

  • Each table expression is evaluated to produce an (internal) intermediate result table.
  • Each intermediate result table then becomes an operand linked with a set operator to form an expression, for example, A UNION B.
  • If the query expression involves more than two table expressions, then the result from the first two becomes an operand for the next set operator and operand, such as (A UNION B) EXCEPT C, ((A UNION B) EXCEPT C) INTERSECT D, and so on.
  • Evaluating a query expression produces a single output table.
Set operators follow this order of precedence unless they are overridden by parentheses in the expressions: INTERSECT is evaluated first. OUTER UNION, UNION, and EXCEPT have the same level of precedence."
 
So yes, the query will perform as you expect.
 
Note that using the keyword ALL might improve performance a bit, if you don't need to remove duplicates.
PG

View solution in original post

2 REPLIES 2
ballardw
Super User

Parentheses may be your friend to help control "order".

Perhaps:

proc sql;
(
select distinct vv.naics_code
from eds.pt_naic_codes as vv

union

select distinct loc.naics_code 
from eds.pa_location as loc

union

select distinct qcew.naics_code_pt_naic_codes as naics_code
from eds.ps_qcew_data as qcew
)
except

select sched.naics_code
from eds.ps_scheduling_naics as sched;
quit;
PGStats
Opal | Level 21

The most reliable place to look is the SAS documentation which says:

 

"A query expression with set operators is evaluated as follows.

  • Each table expression is evaluated to produce an (internal) intermediate result table.
  • Each intermediate result table then becomes an operand linked with a set operator to form an expression, for example, A UNION B.
  • If the query expression involves more than two table expressions, then the result from the first two becomes an operand for the next set operator and operand, such as (A UNION B) EXCEPT C, ((A UNION B) EXCEPT C) INTERSECT D, and so on.
  • Evaluating a query expression produces a single output table.
Set operators follow this order of precedence unless they are overridden by parentheses in the expressions: INTERSECT is evaluated first. OUTER UNION, UNION, and EXCEPT have the same level of precedence."
 
So yes, the query will perform as you expect.
 
Note that using the keyword ALL might improve performance a bit, if you don't need to remove duplicates.
PG

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
  • 2 replies
  • 1678 views
  • 2 likes
  • 3 in conversation