Performing Queries Using Proc SQL
Performing Queries Using Proc SQL
Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina
1
SELECT Statement
The SELECT statement follows the
PROC SQL statement
The SELECT statement retrieves
and displays data
The SELECT statement is composed
of clauses (e.g., SELECT, FROM,
WHERE)
The SELECT and FROM clauses are
required. The remaining clauses
SELECT (So)
FROM (Few)
WHERE (Workers)
GROUP BY (Go)
HAVING (Home)
ORDER BY (On-time)
7
SELECT Clause
The first clause in the SELECT
statement
Specifies the column(s) to be
selected from the table
Each column listed must be
separated by a comma
A label can be specified for a
column in the SELECT clause
SELECT Clause
New columns (i.e., columns that are
not in the table) can be created
with the SELECT clause
The new columns may contain text
or a calculation
New columns will appear in the
output but will not be kept unless a
table or view is created
FROM Clause
The
10
At Bats
Hits
BB
97
36
Scott Wingo
240
81
44
Brady Thomas
231
73
23
Evan Marzilli
220
64
25
Robert Beary
211
61
12
Adrian Morales
249
70
30
Peter Mooney
254
71
44
Jake Williams
209
56
21
Jackie Bradley
Jr.
162
40
22
11
player
atbats
Christian
Walker
271
Scott Wingo
240
Brady Thomas
231
Evan Marzilli
220
Robert Beary
211
Adrian Morales
249
Peter Mooney
254
Jake Williams
209
Jackie Bradley
Jr.
162
12
player
avg
Christian Walker
.358
Scott Wingo
.338
Brady Thomas
.316
Evan Marzilli
.291
Robert Beary
.289
Adrian Morales
.281
Peter Mooney
.280
Jake Williams
.268
.247
13
WHERE Clause
The WHERE clause subsets the data
from the table(s) based on one or
more conditions
The WHERE clause can be any valid
SAS expression
The columns specified in the
WHERE clause do not have to
appear in the SELECT clause
14
player
atbats
Christian
Walker
271
Scott Wingo
240
Brady Thomas
231
Adrian Morales
249
Peter Mooney
254
15
HAVING Clause
Works with the GROUP BY clause to
restrict groups that are displayed in
the output, based on one or more
conditions
Discussed in more detail in Chapter
2
16
ORDER BY Clause
player
atbats
Christian
Walker
271
Peter Mooney
254
Adrian Morales
249
Scott Wingo
240
Brady Thomas
231
Evan Marzilli
220
Robert Beary
211
Jake Williams
209
Jackie Bradley
Jr.
162
18
atbats
proc sql;
select player,
atbats
from bbstats
order by 2 desc;
quit;
Christian
Walker
271
Peter Mooney
254
Adrian Morales
249
Scott Wingo
240
Brady Thomas
231
Evan Marzilli
220
Robert Beary
211
Jake Williams
209
Jackie Bradley
Jr.
162
19
20
21