Chapter 5
Additional SQL Features
1
Section 5.1
Setting PROC SQL Options
2
Objectives
• Use PROC SQL options to control processing
details.
• Reset PROC SQL options without re-invoking
the procedure.
3
Controlling Processing
The SQL procedure offers a variety of options
and statements that affect processing.
General form of the PROC SQL statement:
PROC SQL options;
4
Controlling Processing
Selected options:
INOBS=n sets a limit of n rows from each
source table that contributes to a
query.
OUTOBS=n restricts the number of rows that
a query outputs (displays or
writes to a table).
LOOPS=n restricts the number of iterations
of the inner loop of PROC SQL.
continued...
5
Controlling Processing
NOPROMPT|PROMPT modifies the effect of the
INOBS=, OUTOBS=, and
LOOPS= options so that you are
prompted to stop or continue
when a specified limit is
reached.
PRINT|NOPRINT controls whether the results of a
SELECT statement are displayed.
NONUMBER|NUMBER controls whether the row
number is printed as the first
column in the output.
continued...
6
Controlling Processing
NODOUBLE|DOUBLE double-spaces the report.
NOFLOW|FLOW| controls the appearance of
FLOW=n|FLOW=n m wide character columns. The
FLOW option causes text to be
flowed in its column rather
than wrapping the entire row.
Specifying n determines the
width of the flowed column.
Specifying n and m floats the
width of the column between
the limits to achieve a
balanced layout. 7
Controlling Processing
Example: Display the AWARDS table with flowed
character columns and double-spacing.
proc sql flow=13 double;
select *
from awards;
8
Controlling Processing
Points
Required Rank Award
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
2000 1 free night
in hotel
10000 2 50% discount
on flight
20000 3 free domestic
flight
40000 4 free
international
flight
9
Controlling Processing
Example: Prompt the user to stop or continue
processing after 10 rows are read from
[Link].
proc sql inobs=10 prompt;
select FlightNumber,
Date
from [Link];
10
Controlling Processing
Limit specified by INOBS= option has been
reached.
S to stop
C to continue
OK
11
Controlling Processing
Output
FlightNumber Date
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
182 01MAR2000
114 01MAR2000
202 01MAR2000
219 01MAR2000
439 01MAR2000
387 01MAR2000
290 01MAR2000
523 01MAR2000
982 01MAR2000
622 01MAR2000
12
Resetting Options
You can use the RESET statement to add or
change PROC SQL options without re-invoking the
procedure.
General form of the RESET statement:
RESET options;
13
Resetting Options
Example: Display two rows from the payroll table and
print the row number. Then display the
rows without printing the row number.
proc sql outobs=2 number;
select * from [Link];
Emp Job
Row ID Gender Code Salary DateOfBirth DateOfHire
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
1 1919 M TA2 $48,126 16SEP1958 07JUN1985
2 1653 F ME2 $49,151 19OCT1962 12AUG1988
14
Resetting Options
reset nonumber;
select *
from [Link];
Job
EmpID Gender Code Salary DateOfBirth DateOfHire
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
1919 M TA2 $48,126 16SEP1958 07JUN1985
1653 F ME2 $49,151 19OCT1962 12AUG1988
15
Section 5.2
Dictionary Tables and Views
16
Objectives
• Use dictionary tables and views to obtain
information about SAS files.
17
Overview
You can retrieve information about SAS session
metadata by querying dictionary tables with PROC
SQL. Dictionary tables are
• created at initialization
• updated automatically
• limited to read-only access.
18
Overview
The metadata available in dictionary tables
includes
• SAS files
• external files
• system options, macros, titles, and footnotes.
19
Overview
SAS File Metadata
[Link] general information about
data library members
[Link] detailed information about
data sets
[Link] detailed information on
variables and their attributes
[Link] information about catalog
entries
[Link] general information about
data views
[Link] information on indexes
defined for data files 20
Overview
Other Metadata
[Link] currently assigned filerefs
[Link] current settings of SAS
system options
[Link] information about macro
variables
[Link] text assigned to titles and
footnotes
21
Exploring Dictionary Tables
describe table [Link];
Partial Log
create table [Link] (
libname char(8) label='Library Name',
memname char(32) label='Member Name',
memtype char(8) label='Member Type',
memlabel char(256) label='Dataset Label',
typemem char(8) label='Dataset Type',
crdate num format=DATETIME informat=DATETIME
label='Date Created', ...);
22
Using Dictionary Information
Example: Display information about the files
in the AIRLINE library.
options nolabel nocenter;
select memname format=$20.,
nobs,
nvar,
crdate
from [Link]
where libname='AIRLINE';
23
Using Dictionary Information
Output
memname nobs nvar crdate
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
FLIGHTDELAYS 624 8 18MAR[Link]
FLIGHTSCHEDULE 270 4 18MAR[Link]
FREQUENTFLYERS 206 11 18MAR[Link]
INTERNATIONALFLIGHTS 201 4 18MAR[Link]
MARCHFLIGHTS 635 13 18MAR[Link]
MECHANICSLEVEL1 8 3 18MAR[Link]
MECHANICSLEVEL2 14 3 18MAR[Link]
MECHANICSLEVEL3 7 3 18MAR[Link]
PAYROLLCHANGES 6 6 18MAR[Link]
PAYROLLMASTER 148 6 18MAR[Link]
STAFFCHANGES 6 6 18MAR[Link]
STAFFMASTER 148 6 18MAR[Link]
SUPERVISORS 19 3 18MAR[Link]
24
Using Dictionary Information
Example: Determine which tables contain the
EmpID column.
select memname
from [Link]
where libname='AIRLINE'
and name='EmpID';
25
Using Dictionary Information
Output
memname
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
FLIGHTSCHEDULE
MECHANICSLEVEL1
MECHANICSLEVEL2
MECHANICSLEVEL3
PAYROLLCHANGES
PAYROLLMASTER
STAFFCHANGES
STAFFMASTER
SUPERVISORS
26
Using Dictionary Information
To use session metadata in other procedures or in
a DATA step, you can
• create a PROC SQL view based on a dictionary
table
• use views provided in the SASHELP library that
are based on the dictionary tables.
27
Using Dictionary Information
Example: Use [Link] to extract
information from [Link]
in a PROC TABULATE step.
proc tabulate
data=[Link]
format=8.;
class libname memtype;
keylabel N=' ';
table libname, memtype/
rts=10 misstext='None';
run;
28
Using Dictionary Information
Output
„ƒƒƒƒƒƒƒƒ…ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ†
‚ ‚ memtype ‚
‚ ‡ƒƒƒƒƒƒƒƒ…ƒƒƒƒƒƒƒƒ…ƒƒƒƒƒƒƒƒ…ƒƒƒƒƒƒƒƒ…ƒƒƒƒƒƒƒƒ‰
‚ ‚CATALOG ‚ DATA ‚ITEMSTOR‚ MDDB ‚ VIEW ‚
‡ƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒ‰
‚libname ‚ ‚ ‚ ‚ ‚ ‚
‡ƒƒƒƒƒƒƒƒ‰ ‚ ‚ ‚ ‚ ‚
‚AIRLINE ‚ 1‚ 13‚ None‚ None‚ None‚
‡ƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒ‰
‚MAPS ‚ 3‚ 314‚ None‚ None‚ None‚
‡ƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒ‰
‚SASHELP ‚ 128‚ 104‚ 2‚ 2‚ 18‚
‡ƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒ‰
‚SASUSER ‚ 8‚ 80‚ 1‚ None‚ None‚
‡ƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒ‰
‚WORK ‚ 1‚ None‚ 1‚ None‚ None‚
Šƒƒƒƒƒƒƒƒ‹ƒƒƒƒƒƒƒƒ‹ƒƒƒƒƒƒƒƒ‹ƒƒƒƒƒƒƒƒ‹ƒƒƒƒƒƒƒƒ‹ƒƒƒƒƒƒƒƒŒ 29
Section 5.3
Interfacing PROC SQL
with Macro Language
(Optional)
30
Objectives
• Create and use SAS macro variables
in PROC SQL.
• Understand the use of SAS macros
with SQL processing.
• Use the automatic SAS macro variables
created by PROC SQL.
31
Resolving Symbolic References
Macro variable references embedded within
PROC SQL code are resolved as the source code
is tokenized.
This sounds
familiar!
32
...
Resolving Symbolic References
%let datasetname=payrollmaster;
%let bigsalary=100000;
SYMBOL TABLE
Name Value
datasetname payrollmaster
bigsalary 100000
33
...
Resolving Symbolic References
SYMBOL TABLE
Name Value
datasetname payrollmaster
bigsalary 100000
select *
from airline.&datasetname
where Salary > &bigsalary;
34
Resolving Symbolic References
SYMBOL TABLE
Name Value
datasetname payrollmaster
bigsalary 100000
select *
from [Link]
where Salary > 100000;
35
Creating Macro Variables
SQL allows a query to pass data values to
variables in the host software system. The SAS
System chose to implement these host variables
as macro variables.
36
Creating Macro Variables
PROC SQL can create or update macro variables
using an INTO clause. This clause can be used in
three ways.
37
Creating Macro Variables: Method 1
General form of the SELECT statement with an
INTO keyword:
SELECT col1, col2, ...
INTO :mvar1, :mvar2, ...
FROM ...
Method 1 extracts values only from the first
row of the query result.
38
Creating Macro Variables: Method 1
Example
reset noprint;
select avg(salary),
min(salary),
max(salary)
into :mean, :min, :max
from [Link];
%put &mean &min &max;
Log
54079.65 25120.2 155930.6
39
Creating Macro Variables: Method 1
Example
Calculate the average salary of employees with a
particular job code. Store the average in a macro
variable and use the average to display all
employees in that job code who have a salary
above the average. Place the average in a title.
40
Creating Macro Variables: Method 1
Example
%let code=NA1;
select avg(Salary) into :mean
from [Link]
where JobCode="&code";
41
Creating Macro Variables: Method 1
Example (continued)
reset print;
title1 "&code Employees Earning Above-Average Salaries";
title2 "Average Salary for &code Employees Is &mean";
select *
from [Link]
where Salary > &mean
and
JobCode="&code";
42
Creating Macro Variables: Method 1
Output
NA1 Employees Earning Above-Average Salaries
Average Salary for NA1 Employees Is 58845.08
Job
EmpID Gender Code Salary DateOfBirth DateOfHire
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
1839 F NA1 $60,806 02DEC1968 07JUL1991
1332 M NA1 $59,049 20SEP1968 07JUN1989
1443 F NA1 $59,184 21NOV1966 01SEP1989
43
Creating Macro Variables: Method 2
General form of the SELECT statement to create a
macro variable:
SELECT a, b, ...
INTO :a1-:an, :b1-:bn
FROM ...
Method 2 extracts values from the first n rows
of the query result, and puts them into a series
of n macro variables.
44
Creating Macro Variables: Method 2
Example
How many frequent flyers are in each of the
three member types (GOLD, SILVER, BRONZE)?
reset noprint;
select MemberType,
count(*) as Frequency
into :memtype1-:memtype3,
:freq1-:freq3
from [Link]
group by MemberType;
45
Creating Macro Variables: Method 2
Example (continued)
%put Member types: &memtype1 &memtype2 &memtype3;
%put Frequencies: &freq1 &freq2 &freq3;
Log
Member types: BRONZE GOLD SILVER
Frequencies: 61 60 85
46
Creating Macro Variables: Method 3
General form of the SELECT statement to create a
macro variable:
SELECT col1, col2, ...
INTO :macrovar1, :macrovar2, ...
SEPARATED BY 'delimiter'
FROM ...values from all rows of the
Method 3 extracts
query result, and puts them into a single macro
variable, separated by the specified delimiter.
47
Creating Macro Variables: Method 3
Put the unique values of all international
destinations into a single macro variable.
select distinct Destination
into :airportcodes
separated by ' '
from [Link];
%put &airportcodes;
Log
CDG CPH FRA LHR YYZ
48
Automatic Macro Variables
Execution of a PROC SQL query or non-query
statement updates the following automatic
macro variables:
SQLOBS records the number of rows
output or deleted
SQLRC contains the return code
from each SQL statement
SQLOOPS contains the number of iterations
processed by the inner loop of
PROC SQL.
49
Automatic Macro Variables
Macro Program Example
Write a macro that accepts a state code as a
parameter and creates a table containing
employees from that state. Display a
maximum of 10 rows from the table.
50
Automatic Macro Variables
Example (continued)
%macro state(st);
proc sql;
create table &st as
select LastName,
FirstName
from [Link]
where State="&st";
%put NOTE: The table &st has &sqlobs rows.;
51
Automatic Macro Variables
Example (continued)
title1 "&st Employees";
%if &sqlobs > 10 %then %do;
%put NOTE: Only the first 10 rows are displayed.;
title2 "NOTE: Only 10 rows are displayed.";
reset outobs=10;
%end;
select * from &st;
quit;
%mend state;
52
Automatic Macro Variables
Example (continued)
%state(NY)
Log
NOTE: Table [Link] created, with 89 rows
and 2 columns.
NOTE: The table NY has 89 rows.
NOTE: Only the first 10 rows are displayed.
WARNING: Statement terminated early due to
OUTOBS=10 option.
53
Automatic Macro Variables
Example (continued)
Output
NY Employees
NOTE: Only 10 rows are displayed.
LastName FirstName
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
APPLE TROY
ARTHUR BARBARA
BAUCOM WALTER
BLALOCK RALPH
BOSTIC MARIE
BOYCE JONATHAN
BRADLEY JEREMY
BRYANT LEONARD
BURNETTE THOMAS 54
CAHILL MARSHALL
Section 5.4
Program Testing and
Performance
55
Objectives
• Use PROC SQL options to test SQL code.
• Understand SAS log messages
and accurately benchmark SAS code.
56
Testing and Performance Options
PROC SQL statement options are available to aid
in testing programs and evaluating performance.
The following are selected options:
• EXEC|NOEXEC controls whether submitted SQL
statements are executed.
• NOSTIMER|STIMER reports performance
statistics in the SAS log for each SQL statement.
• NOERRORSTOP|ERRORSTOP is used in batch and
noninteractive jobs to make PROC SQL enter
syntax-check mode after an error occurs.
57
Testing and Performance Options
Example
Display the columns that are retrieved when you
use SELECT * in a query and display any macro
variable resolutions, but do not execute the
query.
58
Testing and Performance Options
Example (continued)
%let datasetname=payrollmaster;
proc sql
feedback
noexec;
select *
from airline.&datasetname;
59
Testing and Performance Options
Example (continued)
Log
NOTE: Statement transforms to:
select [Link], [Link],
[Link],
[Link], [Link],
[Link]
from [Link];
NOTE: Statement not executed due to NOEXEC option.
60
Testing and Performance Options
Example
This is a log from a PROC SQL step with the STIMER
statement option, executing a single query. The first
note concerns the invocation of PROC SQL:
NOTE: The SQL statement used the following resources:
CPU time - [Link].01
Elapsed time - [Link].68
EXCP count - 28
Task memory - 110K (20K data, 90K program)
Total memory - 864K (760K data, 104K program)
61
Testing and Performance Options
Example (continued)
The second note concerns the query itself.
NOTE: The SQL statement used the following resources:
CPU time - [Link].23
Elapsed time - [Link].61
EXCP count - 157
Task memory - 1213K (828K data, 385K program)
Total memory - 2258K (1840K data, 418K program)
62
Testing and Performance Options
Example (continued)
The third note reflects the totals for the procedure.
NOTE: The SQL procedure used the following resources:
CPU time - [Link].25
Elapsed time - [Link].34
EXCP count - 186
Task memory - 1213K (828K data, 385K program)
Total memory - 2258K (1840K data, 418K program)
63
General Guidelines for
Benchmarking Programs
Never use elapsed time for comparison
because it may be affected by concurrent
tasks.
Benchmark two programs in separate SAS
sessions. If benchmarking is done within one
SAS session, statistics for the second
program can be misleading because the SAS
supervisor might have loaded modules into
memory from prior steps.
64
General Guidelines for
Benchmarking Programs
Run each program multiple times and
average the performance statistics.
Use realistic data for tests. Program A could
be better than program B on small tables and
worse on large tables.
65