Download presentation
Presentation is loading. Please wait.
Published bySilas Shelton Modified over 9 years ago
1
Performing Queries Using PROC SQL Chapter 1 1 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina
2
PROC SQL Basics PROC SQL is the SAS implementation of Structured Query Language (SQL) PROC SQL is the SAS implementation of Structured Query Language (SQL) SQL is a standardized language and is widely used to retrieve and update data in tables SQL is a standardized language and is widely used to retrieve and update data in tables PROC SQL can often be used as an alternative to other SAS procedures or the data step PROC SQL can often be used as an alternative to other SAS procedures or the data step 2
3
PROC SQL Basics The PROC SQL statement does not need to be repeated with each query The PROC SQL statement does not need to be repeated with each query PROC SQL does not require a run statement PROC SQL does not require a run statement To end PROC SQL, submit another procedure, a data step, or a quit statement. To end PROC SQL, submit another procedure, a data step, or a quit statement. 3
4
Uses for SQL Retrieve data from tables Retrieve data from tables Add or modify data in a table Add or modify data in a table Add, modify, or drop columns in a table Add, modify, or drop columns in a table Create tables and views Create tables and views Join tables Join tables Create reports Create reports 4
5
Basic PROC SQL Step proc sql; select column1, column2,…, columnn select column1, column2,…, columnn from table1 (or view1) from table1 (or view1) ; ; 5
6
SELECT Statement The SELECT statement follows the PROC SQL statement The SELECT statement follows the PROC SQL statement The SELECT statement retrieves and displays data The SELECT statement retrieves and displays data The SELECT statement is composed of clauses (e.g., SELECT, FROM, WHERE) The SELECT statement is composed of clauses (e.g., SELECT, FROM, WHERE) The SELECT and FROM clauses are required. The remaining clauses are optional. The SELECT and FROM clauses are required. The remaining clauses are optional. 6
7
The Order of Clauses Within the PROC SQL SELECT Statement Matters SELECT (So) SELECT (So) FROM (Few) FROM (Few) WHERE (Workers) WHERE (Workers) GROUP BY (Go) GROUP BY (Go) HAVING (Home) HAVING (Home) ORDER BY (On-time) ORDER BY (On-time) 7
8
SELECT Clause The first clause in the SELECT statement The first clause in the SELECT statement Specifies the column(s) to be selected from the table Specifies the column(s) to be selected from the table Each column listed must be separated by a comma Each column listed must be separated by a comma A label can be specified for a column in the SELECT clause A label can be specified for a column in the SELECT clause 8
9
SELECT Clause New columns (i.e., columns that are not in the table) can be created with the 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 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 New columns will appear in the output but will not be kept unless a table or view is created 9
10
FROM Clause The FROM clause specifies which table(s) or view(s) to be queried The FROM clause specifies which table(s) or view(s) to be queried 10
11
2011 SC Baseball Stats (bbstats) PlayerAt BatsHitsBB Christian Walker2719736 Scott Wingo2408144 Brady Thomas2317323 Evan Marzilli2206425 Robert Beary2116112 Adrian Morales2497030 Peter Mooney2547144 Jake Williams2095621 Jackie Bradley Jr.1624022 11
12
Example – Select and From Clauses proc sql; select player, atbats select player, atbats from bbstats; from bbstats;quit; playeratbats Christian Walker271 Scott Wingo240 Brady Thomas231 Evan Marzilli220 Robert Beary211 Adrian Morales249 Peter Mooney254 Jake Williams209 Jackie Bradley Jr.162 12
13
Example – Select and From Clauses proc sql; select player, hits/atbats as avg select player, hits/atbats as avg from bbstats; from bbstats;quit; playeravg Christian Walker.358 Scott Wingo.338 Brady Thomas.316 Evan Marzilli.291 Robert Beary.289 Adrian Morales.281 Peter Mooney.280 Jake Williams.268 Jackie Bradley Jr..247 13
14
WHERE Clause The WHERE clause subsets the data from the table(s) based on one or more conditions 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 WHERE clause can be any valid SAS expression The columns specified in the WHERE clause do not have to appear in the SELECT clause The columns specified in the WHERE clause do not have to appear in the SELECT clause 14
15
Example – Where Clause proc sql; select player, atbats select player, atbats from bbstats from bbstats where atbats > 230; where atbats > 230;quit; playeratbats Christian Walker271 Scott Wingo240 Brady Thomas231 Adrian Morales249 Peter Mooney254 15
16
HAVING Clause Works with the GROUP BY clause to restrict groups that are displayed in the output, based on one or more conditions 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 Discussed in more detail in Chapter 2 16
17
ORDER BY Clause ORDER BY sorts rows by the values of a specified column or specified columns. Each column must be separated by a comma ORDER BY sorts rows by the values of a specified column or specified columns. Each column must be separated by a comma Descending sort – specify the keyword DESC following the column name Descending sort – specify the keyword DESC following the column name In the ORDER BY clause, you can reference the column by its name or the position in the SELECT clause. Use an integer to indicate the column’s position. In the ORDER BY clause, you can reference the column by its name or the position in the SELECT clause. Use an integer to indicate the column’s position. 17
18
Example – Order By Clause proc sql; select player, atbats select player, atbats from bbstats from bbstats order by atbats desc; order by atbats desc;quit; playeratbats Christian Walker271 Peter Mooney254 Adrian Morales249 Scott Wingo240 Brady Thomas231 Evan Marzilli220 Robert Beary211 Jake Williams209 Jackie Bradley Jr.162 18
19
Example – Order By Clause proc sql; select player, atbats select player, atbats from bbstats from bbstats order by 2 desc; order by 2 desc;quit; This example uses the column’s position in the SELECT clause in the ORDER by clause playeratbats Christian Walker271 Peter Mooney254 Adrian Morales249 Scott Wingo240 Brady Thomas231 Evan Marzilli220 Robert Beary211 Jake Williams209 Jackie Bradley Jr.162 19
20
CREATE TABLE Statement Use the CREATE TABLE statement to create a new table from the results of a query Use the CREATE TABLE statement to create a new table from the results of a query 20
21
PROC SQL Step for Creating a Table from a Query Result proc sql; create table table-name as create table table-name as select column1, column2,…, columnn select column1, column2,…, columnn from table1 (or view1) from table1 (or view1) ; ; 21
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.