BookmarkSubscribeRSS Feed

SQL UNION operator

Started ‎07-06-2018 by
Modified ‎07-06-2018 by
Views 17,676

The UNION operator in Proc SQL is used to append the rows of two or more SELECT statements having the same number of columns with similar data types. Similar columns in each SELECT statement should normally be in the same order, as the Union is done by position not by column name, except where appending dissimilar columns from a select statement is explicitly desired.

 

Usage notes

Note that, unlike using the SET statement with multiple datasets, the default behavior of the UNION operator is to remove duplicate rows, from the results of each SELECT statement as well as overall, resulting in only DISTINCT rows. If retention of duplicate rows is desired, either OUTER UNION must be specified, or else the ALL operator must be used on individual SELECT statements where duplicates are desired.

 

Syntax

SELECT column_name(s) FROM table_name1
<OUTER> UNION <ALL>
SELECT column_name(s) FROM table_name2
...

This article was originally posted by Gerald Zuckier on sasCommunity.org. 

Contributors
Version history
Last update:
‎07-06-2018 03:34 PM
Updated by:

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

SAS AI and Machine Learning Courses

The rapid growth of AI technologies is driving an AI skills gap and demand for AI talent. Ready to grow your AI literacy? SAS offers free ways to get started for beginners, business leaders, and analytics professionals of all skill levels. Your future self will thank you.

Get started

Article Labels
Article Tags