![]() Chapter Contents |
![]() Previous |
![]() Next |
| The SQL Procedure |
| See also: | table-expression, query-expression |
SELECT
<DISTINCT> object-item <,object-item>...
|
| 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>... |
object-item is one of the following:
| * |
| case-expression <AS alias> |
|
column-name <AS alias>
<column-modifier <column-modifier>...> |
|
sql-expression <AS
alias>
<column-modifier <column-modifier>...> |
| table-name.* |
| table-alias.* |
| view-name.* |
| view-alias.* |
| Arguments |
| Featured in: | Producing All the Possible Combinations of the Values in a Column |
| 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.
| See also: | Using Macro Variables Set by PROC SQL |
|
INTO
:macro-variable-specification
<, :macro-variable-specification>... |
:macro-variable-specification is one of the following:
| :macro-variable <SEPARATED BY 'character' <NOTRIM>>; |
| :macro-variable-1 - :macro-variable-n <NOTRIM>; |
| Arguments |
| 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:
proc sql noprint; select style, sqfeet into :style, :sqfeet from proclib.houses; %put &style &sqfeet;
The results are written to the SAS log:
1 proc sql noprint; 2 select style, sqfeet 3 into :style, :sqfeet 4 from proclib.houses; 5 6 %put &style &sqfeet; CONDO 900 |
The following PROC SQL step puts the values from the first four rows of the PROCLIB.HOUSES table into macro variables:
proc sql noprint; select distinct Style, SqFeet into :style1 - :style3, :sqfeet1 - :sqfeet4 from proclib.houses; %put &style1 &sqfeet1; %put &style2 &sqfeet2; %put &style3 &sqfeet3; %put &sqfeet4;
The %PUT statements write the results to the SAS log:
1 proc sql noprint; 2 select distinct style, sqfeet 3 into :style1 - :style3, :sqfeet1 - :sqfeet4 4 from proclib.houses; 5 6 %put &style1 &sqfeet1; CONDO 900 7 %put &style2 &sqfeet2; CONDO 1000 8 %put &style3 &sqfeet3; CONDO 1200 9 %put &sqfeet4; 1400 |
proc sql; select distinct style into :s1 separated by ',' from proclib.houses; %put &s1;
The results are written to the SAS log:
3 proc sql; 4 select distinct style 5 into :s1 separated by ',' 6 from proclib.houses; 7 8 %put &s1 CONDO,RANCH,SPLIT,TWOSTORY |
proc sql noprint; select style, sqfeet into :style1 - :style4 notrim, :sqfeet separated by ',' notrim from proclib.houses; %put *&style1* *&sqfeet*; %put *&style2* *&sqfeet*; %put *&style3* *&sqfeet*; %put *&style4* *&sqfeet*;
The results are written to the SAS log, as shown in Macro Variable Values.
3 proc sql noprint; 4 select style, sqfeet 5 into :style1 - :style4 notrim, 6 :sqfeet separated by ',' notrim 7 from proclib.houses; 8 9 %put *&style1* *&sqfeet*; *CONDO * * 900, 1000, 1200, 1400, 1600, 1800, 2100, 3000* 10 %put *&style2* *&sqfeet*; *CONDO * * 900, 1000, 1200, 1400, 1600, 1800, 2100, 3000* 11 %put *&style3* *&sqfeet*; *RANCH * * 900, 1000, 1200, 1400, 1600, 1800, 2100, 3000* 12 %put *&style4* *&sqfeet*; *RANCH * * 900, 1000, 1200, 1400, 1600, 1800, 2100, 3000* |
| 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 |
from-list is one of the following:
| table-name <<AS> alias> |
| view-name <<AS> alias> |
| joined-table |
|
(query-expression) <<AS>
alias
<(column <,column>...)>> |
| CONNECTION TO |
| Arguments |
| 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:
| Featured in: | Joining Two Tables and Joining Three Tables |
| WHERE sql-expression |
| Argument |
| Details |
where max(measure1) > 50;However, this WHERE clause will work:
where max(measure1,measure2) > 50;
| Writing Efficient WHERE Clauses |
Here are some guidelines for writing efficient WHERE clauses that enable PROC SQL to use indexes effectively:
/* inefficient:*/ where country like '%INA' /* efficient: */ where country like 'A%INA'
/* inefficient:*/ where salary>12*4000 /* efficient: */ where salary>48000
where miles>3800 and boarded>100
| 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>... |
group-by-item is one of the following:
| integer |
| column-name |
| sql-expression |
| Arguments |
| Details |
group by sum(x)
| Featured in: | Creating a View from a Query's Result and Joining Two Tables and Calculating a New Value |
| HAVING sql-expression |
| Argument |
| 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.
| See also: | query-expression |
| Featured in: | Retrieving Values with the SOUNDS-LIKE Operator |
| ORDER BY order-by-item <,order-by-item>...; |
order-by-item is one of the following:
| integer <ASC|DESC> |
| column-name <ASC|DESC> |
| sql-expression <ASC|DESC> |
| Arguments |
| Details |
proc sql; select country from census order by pop95-pop90 desc;
NOTE: The query as specified involves ordering by an item that doesn't appear in its SELECT clause.
proc sql; select * from measure order by put(pol_a,fmt_a.);
![]() Chapter Contents |
![]() Previous |
![]() Next |
![]() Top of Page |
Copyright © 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.