Chapter Contents |
Previous |
Next |
The SQL Procedure |
See also: | table-expression , query-expression |
SELECT
<DISTINCT> object-item <,object-item>...
|
SELECT Clause |
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 |
Column Aliases |
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 |
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>... |
:macro-variable-specification is one of the following:
:macro-variable <SEPARATED BY 'character' <NOTRIM>>; |
:macro-variable-1 - :macro-variable-n <NOTRIM>; |
Arguments |
Details |
Examples |
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* |
FROM Clause |
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 |
The optional keyword AS is often used to distinguish a table alias from other table names.
In-Line Views |
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 |
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 |
/* 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
GROUP BY Clause |
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)
HAVING Clause |
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 |
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.
ORDER BY Clause |
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.