Chapter Contents

Previous

Next
The SQL Procedure

SELECT Statement


Selects columns and rows of data from tables and views.

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

case-expression
See CASE expression .

column-modifier
See column-modifier .

column-name
See column-name .

DISTINCT
eliminates duplicate rows.
Featured in: Producing All the Possible Combinations of the Values in a Column

sql-expression
See sql-expression .

table-alias
is an alias for a PROC SQL table.

table-name
specifies a PROC SQL table.

view-name
specifies any type of SAS data view.

view-alias
specifies the alias for any type of SAS data view.


Asterisk(*) Notation
The asterisk (*) represents all columns of the table(s) 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.


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, 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, 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 Macro Variables Set by PROC SQL


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.

NOTRIM
protects the leading and trailing blanks from being deleted from the macro variable value when the macro variables are created.

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

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

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.

CONNECTION TO
See CONNECTION TO .

joined-table
See joined-table .

query-expression
See query-expression .

table-name
specifies a PROC SQL table.

view-name
specifies any type of SAS data view.


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. Table aliases are always required when joining a table with itself. 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 32 tables in any one query. The 32-table limit includes underlying tables that may 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


Writing Efficient WHERE Clauses
Here are some guidelines for writing efficient WHERE clauses that enable PROC SQL to use indexes effectively:


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

integer
equates to a column's position.

column-name
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. Or, if the query involves remerged data, 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(s) to be evaluated. If you omit the GROUP BY clause, 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 SEX to determine the oldest employee of each sex. 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 sex
      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 <,order-by-item>...;


Arguments

ASC
orders the data in ascending order. This is the default order.

column-name
See column-name .

DESC
orders the data in descending order.

integer
equates to a column's position.

sql-expression
See sql-expression .


Details


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.