Previous Page | Next Page

The SQL Procedure

SELECT Statement


Selects columns and rows of data from tables and views.
Restriction: The clauses in the SELECT statement must appear in the order shown.
See also:

table-expression

query-expression


SELECT <DISTINCT> object-item <, ...object-item>
<INTO macro-variable-specification
<, ... macro-variable-specification>>
FROM from-list
<WHERE sql-expression>
<GROUP BY group-by-item
<, ... group-by-item>>
<HAVING sql-expression>
<ORDER BY order-by-item
<, ... order-by-item>>;


SELECT Clause


Lists the columns that will appear in the output.
See Also: column-definition
Featured in: Creating a Table and Inserting Data into It and Creating a Table from a Query's Result

SELECT <DISTINCT> object-item <, ... object-item>


Arguments

alias

assigns a temporary, alternate name to the column.

DISTINCT

eliminates duplicate rows.

Tip: A row is considered a duplicate when all of its values are the same as the values of another row. The DISTINCT argument applies to all columns in the SELECT list. One row is displayed for each existing combination of values.

Note:   DISTINCT works on the internal or stored value, not necessarily on the value as it is displayed. Numeric precision can cause multiple rows to be returned with values that appear to be the same.  [cautionend]

Tip: If available, PROC SQL uses index files when processing SELECT DISTINCT statements.
Featured in: Producing All the Possible Combinations of the Values in a Column
object-item

is one of the following:

*

represents all columns in the tables or views that are listed in the FROM clause.

case-expression <AS alias>

derives a column from a CASE expression. See CASE expression.

column-name <AS alias>
<column-modifier <... column-modifier>>

names a single column. See column-name and column-modifier.

sql-expression <AS alias>
<column-modifier <... column-modifier>>

derives a column from an sql-expression. See sql-expression and column-modifier.

table-name.*

specifies all columns in the PROC SQL table that is specified in table-name.

table-alias.*

specifies all columns in the PROC SQL table that has the alias that is specified in table-alias.

view-name.*

specifies all columns in the SAS view that is specified in view-name.

view-alias.*

specifies all columns in the SAS view that has the alias that is specified in view-alias.


Asterisk (*) Notation

The asterisk (*) represents all columns of the table or tables listed in the FROM clause. When an asterisk is not prefixed with a table name, all the columns from all tables in the FROM clause are included; when it is prefixed (for example, table-name.* or table-alias.*), all the columns from that table only are included.

Note:   A warning will occur if you create an output table using the SELECT * syntax when columns with the same name exist in the multiple tables that are listed on the FROM clause. You can avoid the warning by using one of the following actions:

  [cautionend]

Column Aliases

A column alias is a temporary, alternate name for a column. Aliases are specified in the SELECT clause to name or rename columns so that the result table is clearer or easier to read. Aliases are often used to name a column that is the result of an arithmetic expression or summary function. An alias is one word only. If you need a longer column name, then use the LABEL= column-modifier, as described in column-modifier. The keyword AS is required with a column alias to distinguish the alias from other column names in the SELECT clause.

Column aliases are optional, and each column name in the SELECT clause can have an alias. After you assign an alias to a column, you can use the alias to refer to that column in other clauses.

If you use a column alias when creating a PROC SQL view, then the alias becomes the permanent name of the column for each execution of the view.


INTO Clause


Stores the value of one or more columns for use later in another PROC SQL query or SAS statement.
Restriction: An INTO clause cannot be used in a CREATE TABLE statement.
See also: Using the PROC SQL Automatic Macro Variables in the SAS 9.2 SQL Procedure User's Guide

INTO macro-variable-specification
<, ... macro-variable-specification>


Arguments

macro-variable

specifies a SAS macro variable that stores the values of the rows that are returned.

macro-variable-specification

is one of the following:

:macro-variable <SEPARATED BY 'character(s)' <NOTRIM>>

stores the values that are returned into a single macro variable.

:macro-variable-1 - :macro-variable-n <NOTRIM>

stores the values that are returned into a range of macro variables.

Tip: When you specify a range of macro variables, the SAS Macro Facility creates only the number of macro variables that are needed. For example, if you specify :var1-:var9999 and only 55 variables are needed, only :var1-:var55 is created. The SQLOBS automatic variable is useful if a subsequent part of your program needs to know how many variables were actually created. In this example, SQLOBS would have the value of 55.
NOTRIM

protects the leading and trailing blanks from being deleted from values that are stored in a range of macro variables or multiple values that are stored in a single macro variable.

SEPARATED BY 'character'

specifies a character that separates the values of the rows.


Details


Examples

These examples use the PROCLIB.HOUSES table:

                                 The SAS System                                1

                               Style       SqFeet
                               ------------------
                               CONDO          900
                               CONDO         1000
                               RANCH         1200
                               RANCH         1400
                               SPLIT         1600
                               SPLIT         1800
                               TWOSTORY      2100
                               TWOSTORY      3000
                               TWOSTORY      1940
                               TWOSTORY      1860

With the macro-variable-specification, you can do the following:


FROM Clause


Specifies source tables or views.
Featured in: Creating a Table and Inserting Data into It, Joining Two Tables, Joining Three Tables, and Querying an In-Line View

FROM from-list


Arguments

alias

specifies a temporary, alternate name for a table, view, or in-line view that is specified in the FROM clause.

column

names the column that appears in the output. The column names that you specify are matched by position to the columns in the output.

from-list

is one of the following:

table-name <<AS> alias>

names a single PROC SQL table. table-name can be a one-level name, a two-level libref.table name, or a physical pathname that is enclosed in single quotation marks.

view-name <<AS> alias>

names a single SAS view. view-name can be a one-level name, a two-level libref.view name, or a physical pathname that is enclosed in single quotation marks.

joined-table

specifies a join. See joined-table.

(query-expression) <<AS> alias>
<(column <, ... column>)>

specifies an in-line view. See query-expression.

CONNECTION TO

specifies a DBMS table. See CONNECTION TO.

Note:   With table-name and view-name, you can use data set options by placing them in parentheses immediately after table-name or view-name. See Using SAS Data Set Options with PROC SQL in the SAS 9.2 SQL Procedure User's Guidefor details.  [cautionend]


Table Aliases

A table alias is a temporary, alternate name for a table that is specified in the FROM clause. Table aliases are prefixed to column names to distinguish between columns that are common to multiple tables. Column names in reflexive joins (joining a table with itself) must be prefixed with a table alias in order to distinguish which copy of the table the column comes from. Column names in other kinds of joins must be prefixed with table aliases or table names unless the column names are unique to those tables.

The optional keyword AS is often used to distinguish a table alias from other table names.


In-Line Views

The FROM clause can itself contain a query-expression that takes an optional table alias. This kind of nested query-expression is called an in-line view. An in-line view is any query-expression that would be valid in a CREATE VIEW statement. PROC SQL can support many levels of nesting, but it is limited to 256 tables in any one query. The 256-table limit includes underlying tables that can contribute to views that are specified in the FROM clause.

An in-line view saves you a programming step. Rather than creating a view and referring to it in another query, you can specify the view in-line in the FROM clause.

Characteristics of in-line views include the following:


WHERE Clause


Subsets the output based on specified conditions.
Featured in: Joining Two Tables and Joining Three Tables

WHERE sql-expression

Argument

sql-expression

See sql-expression.


Details


GROUP BY Clause


Specifies how to group the data for summarizing.
Featured in: Creating a View from a Query's Result and Joining Two Tables and Calculating a New Value

GROUP BY group-by-item <, ..., group-by-item>


Arguments

group-by-item

is one of the following:

integer

is a positive integer that equates to a column's position.

column-name

is the name of a column or a column alias. See column-name.

sql-expression

See sql-expression.


Details


HAVING Clause


Subsets grouped data based on specified conditions.
Featured in: Creating a View from a Query's Result and Joining Two Tables and Calculating a New Value

HAVING sql-expression

Argument

sql-expression

See sql-expression.


Subsetting Grouped Data

The HAVING clause is used with at least one summary function and an optional GROUP BY clause to summarize groups of data in a table. A HAVING clause is any valid SQL expression that is evaluated as either true or false for each group in a query. Alternatively, if the query involves remerged data, then the HAVING expression is evaluated for each row that participates in each group. The query must include one or more summary functions.

Typically, the GROUP BY clause is used with the HAVING expression and defines the group or groups to be evaluated. If you omit the GROUP BY clause, then the summary function and the HAVING clause treat the table as one group.

The following PROC SQL step uses the PROCLIB.PAYROLL table (shown in Creating a Table from a Query's Result) and groups the rows by Gender to determine the oldest employee of each gender. In SAS, dates are stored as integers. The lower the birthdate as an integer, the greater the age. The expression birth=min(birth) is evaluated for each row in the table. When the minimum birthdate is found, the expression becomes true and the row is included in the output.

proc sql;
   title 'Oldest Employee of Each Gender';
   select *
      from proclib.payroll
      group by gender
      having birth=min(birth);

Note:    This query involves remerged data because the values returned by a summary function are compared to values of a column that is not in the GROUP BY clause. See Remerging Data for more information about summary functions and remerging data.  [cautionend]


ORDER BY Clause


Specifies the order in which rows are displayed in a result table.
See also: query-expression
Featured in: Retrieving Values with the SOUNDS-LIKE Operator

ORDER BY order-by-item <ASC|DESC><, ... order-by-item <ASC|DESC>>;


Arguments

order-by-item

is one of the following:

integer

equates to a column's position.

column-name

is the name of a column or a column alias. See column-name.

sql-expression

See sql-expression.

ASC

orders the data in ascending order. This is the default order; if neither ASC nor DESC is specified, the data is ordered in ascending order.

DESC

orders the data in descending order.


Details

Previous Page | Next Page | Top of Page