Set Operator
|
Treatment of Rows
|
Treatment of Columns
|
Example
|
---|---|---|---|
EXCEPT
|
Selects unique rows
from the first table that are not found in the second table.
|
Overlays columns based
on their position in the SELECT clause without regard to the individual
column names.
|
proc sql;
select *
from table1
except
select *
from table2;
|
INTERSECT
|
Selects unique rows
that are common to both tables.
|
Overlays columns based
on their position in the SELECT clause without regard to the individual
column names.
|
proc sql;
select *
from table1
intersect
select *
from table2;
quit;
|
UNION
|
Selects unique rows
from both tables.
|
Overlays columns based
on their position in the SELECT clause without regard to the individual
column names.
|
proc sql;
select *
from table1
union
select *
from table2;
quit;
|
OUTER UNION
|
Selects all rows from
both tables.
The OUTER UNION operator
concatenates the results of the queries.
|
Does not overlay columns.
|
proc sql;
select *
from table1
outer union
select *
from table2;
quit;
|
Syntax of an SQL query
using a set operator:
SELECT column-1<,
... column-n>
FROM table-1
| view-1<,
... table-n | view-n>
<optional
query clauses>
set-operator <ALL> <CORR>
SELECT column-1<,
... column-n>
FROM table-1
| view-1<,
... table-n | view-n>
<optional
query clauses>;
SELECT
specifies the column(s)
to appear in the result.
FROM
specifies the table(s)
or view(s) to be queried.
optional query clauses
are used to refine
the query further and include the clauses WHERE, GROUP BY, HAVING,
and ORDER BY.
|
proc sql; select * from certadv.stress17 union select * from certadv.stress18; quit;
proc sql; select * from table1 set-operator select * from table2 set-operator select * from table3; quit;
proc sql; select * from certadv.mechanicslevel1 outer union select * from certadv.mechanicslevel2 outer union select * from certadv.mechanicslevel3; quit;
Table1 UNION Table2
). (Table1
UNION Table2) EXCEPT Table3
((Table1
UNION Table2) EXCEPT Table3) INTERSECT Table4
title 'PROC SQL Query Result'; proc sql; select * /*1*/ from certadv.col1 except /*2*/ select * from certadv.col2; quit;
1 | The SELECT clause in each query uses an asterisk (*) to overlay columns based on their positions in the tables. |
2 | The EXCEPT set operator overlays columns. |
ERROR: Column 1 from the first contributor of EXCEPT is not the same type as its counterpart from the second.
proc sql;
select *
from table1
set-operator <all> <corr>
select *
from table2;
quit;
Keyword
|
Action
|
Situation
|
---|---|---|
ALL
|
Makes only one pass
through the data and does not remove duplicate rows.
|
It does not matter whether
there are duplicates.
Duplicates are not possible.
ALL cannot be used with
OUTER UNION.
|
CORR (or CORRESPONDING)
|
Compares and overlays
columns by name instead of by position:
If an alias is assigned
to a column in the SELECT clause, CORR uses the alias instead of
the permanent column name.
|
Two tables have some
or all columns in common, but the columns are not in the same order.
|
3.21.43.192