Tải bản đầy đủ (.pdf) (994 trang)

SAS SAS certification prep guide advanced programming for SAS 9 nov 2007 ISBN 1599945592 pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (16.93 MB, 994 trang )


SAS #ERTIFICATION0REP'UIDE
®

Advanced Programming for SAS®9


The correct bibliographic citation for this manual is as follows: SAS Institute Inc. 2007.
SAS ® Certification Prep Guide: Advanced Programming for SAS ®9. Cary, NC: SAS
Institute Inc.
SAS® Certification Prep Guide: Advanced Programming for SAS®9
Copyright © 2002–2007, SAS Institute Inc., Cary, NC, USA
978-1-59994-559-0
All rights reserved. Produced in the United States of America.
For a hard-copy book: No part of this publication may be reproduced, stored in a
retrieval system, or transmitted, in any form or by any means, electronic, mechanical,
photocopying, or otherwise, without the prior written permission of the publisher, SAS
Institute Inc.
For a Web download or e-book: Your use of this publication shall be governed by the
terms established by the vendor at the time you acquire this publication.
U.S. Government Restricted Rights Notice. Use, duplication, or disclosure of this
software and related documentation by the U.S. government is subject to the Agreement
with SAS Institute and the restrictions set forth in FAR 52.227-19 Commercial Computer
Software-Restricted Rights (June 1987).
SAS Institute Inc., SAS Campus Drive, Cary, North Carolina 27513.
1st printing, November 2007
SAS® Publishing provides a complete selection of books and electronic products to help
customers use SAS software to its fullest potential. For more information about our
e-books, e-learning products, CDs, and hard-copy books, visit the SAS Publishing Web site
at support.sas.com/publishing or call 1-800-727-3228.
SAS® and all other SAS Institute Inc. product or service names are registered trademarks


or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA
registration.
Other brand and product names are registered trademarks or trademarks of their
respective companies.


Contents
About This Book and CD xi

What’s New xi
Purpose xi
Audience
xi
Prerequisites xi
How to Use This Book and CD
xii
Syntax Conventions for This Book
xii
SAS Certification Practice Exam: Advanced Programming for SAS 9
SAS Advanced Programming Exam for SAS 9
Additional Resources xiii

PART

1

SQL Processing With SAS
Chapter 1

xiii


1

4 Performing Queries Using PROC SQL

Overview
4
PROC SQL Basics 4
Writing a PROC SQL Step
Selecting Columns
8
Specifying the Table
10

3

6

Specifying Subsetting Criteria 10
Ordering Rows
10
Querying Multiple Tables
12
Summarizing Groups of Data
16
Creating Output Tables
17
Additional Features
18
Summary

19
Quiz
21

Chapter 2

4 Performing Advanced Queries Using PROC SQL

Overview
27
Viewing SELECT Statement Syntax
Displaying All Columns 29

28

Limiting the Number of Rows Displayed
30
Eliminating Duplicate Rows from Output
31
Subsetting Rows by Using Conditional Operators 32
Subsetting Rows by Using Calculated Values
40
Enhancing Query Output
42
Summarizing and Grouping Data
47
Subsetting Data by Using Subqueries 58
Subsetting Data by Using Noncorrelated Subqueries
60
Subsetting Data by Using Correlated Subqueries 65


25

xiii


iv

Validating Query Syntax
Additional Features
69
Summary
70
Quiz
73

Chapter 3

67

4 Combining Tables Horizontally Using PROC SQL

Overview
80
Understanding Joins
81
Generating a Cartesian Product
81
Using Inner Joins 83
Using Outer Joins

91
Creating an Inner Join with Outer Join-Style Syntax
Comparing SQL Joins and DATA Step Match-Merges
Using In-Line Views
102
Joining Multiple Tables and Views
106
Summary
113
Quiz
116

Chapter 4

Chapter 5

97
97

4 Combining Tables Vertically Using PROC SQL

Overview
126
Understanding Set Operations 127
Using the EXCEPT Set Operator
132
Using the INTERSECT Set Operator
138
Using the UNION Set Operator
142

Using the OUTER UNION Set Operator
146
Comparing Outer Unions and Other SAS Techniques
Summary
151
Quiz
152

79

125

149

4 Creating and Managing Tables Using PROC SQL

Overview
161
Understanding Methods of Creating Tables
162
Creating an Empty Table by Defining Columns
163
Displaying the Structure of a Table
168
Creating an Empty Table That Is Like Another Table
169
Creating a Table from a Query Result
172
Inserting Rows of Data into a Table
174

Creating a Table That Has Integrity Constraints 182
Handling Errors in Row Insertions
190
Displaying Integrity Constraints for a Table
193
Updating Values in Existing Table Rows 194
Deleting Rows in a Table
202
Altering Columns in a Table
204
Dropping Tables
210
Summary
211
Quiz
217

159


v

Chapter 6

4 Creating and Managing Indexes Using PROC SQL

Overview
222
Understanding Indexes 223
Deciding Whether to Create an Index

Creating an Index
228
Displaying Index Specifications 229
Managing Index Usage 231
Dropping Indexes 235
Summary
237
Quiz
240

Chapter 7

225

4 Creating and Managing Views Using PROC SQL

Overview
244
Creating and Using PROC SQL Views 245
Displaying the Definition for a PROC SQL View
Managing PROC SQL Views
248
Updating PROC SQL Views
251
Dropping PROC SQL Views 253
Summary
254
Quiz
257


Chapter 8

PART

2

SAS Macro Language
Chapter 9

247

4 Managing Processing Using PROC SQL

Overview
262
Specifying SQL Options
263
Controlling Execution
264
Controlling Output
265
Testing and Evaluating Performance
Resetting Options
271
Using Dictionary Tables 273
Additional Features
277
Summary
279
Quiz

281

221

261

269

285

4 Introducing Macro Variables

287

Overview
288
Basic Concepts
289
Using Automatic Macro Variables 292
Using User-Defined Macro Variables 293
Processing Macro Variables 295
Displaying Macro Variable Values in the SAS Log
298
Using Macro Functions to Mask Special Characters 301
Using Macro Functions to Manipulate Character Strings
306
Using SAS Functions with Macro Variables 313
Combining Macro Variable References with Text 315

243



vi

Summary
319
Quiz
322

Chapter 10

4 Processing Macro Variables at Execution Time

325

Overview
326
Creating a Macro Variable During DATA Step Execution 327
Creating Multiple Macro Variables During DATA Step Execution 341
Referencing Macro Variables Indirectly 344
Obtaining Macro Variable Values During DATA Step Execution
350
Creating Macro Variables During PROC SQL Step Execution 352
Working with PROC SQL Views 359
Using Macro Variables in SCL Programs 360
Summary
363
Quiz
366


Chapter 11

4 Creating and Using Macro Programs

Overview
373
Basic Concepts
373
Developing and Debugging Macros
379
Using Macro Parameters 382
Understanding Symbol Tables 388
Processing Statements Conditionally 397
Processing Statements Iteratively 406
Using Arithmetic and Logical Expressions
Summary
414
Quiz
418

Chapter 12

4 Storing Macro Programs

371

411

423


Overview
424
Understanding Session-Compiled Macros
424
Storing Macro Definitions in External Files 425
Storing Macro Definitions in Catalog SOURCE Entries
Using the Autocall Facility 431
Using Stored Compiled Macros 435
Summary
442
Quiz
444

PART

3

Advanced SAS Programming Techniques
Chapter 13

4 Creating Samples and Indexes

427

449

451

Overview
452

Creating a Systematic Sample from a Known Number of Observations
453
Creating a Systematic Sample from an Unknown Number of Observations
455
Creating a Random Sample with Replacement
456
Creating a Random Sample without Replacement 459
Using Indexes 460


vii

Creating Indexes in the DATA Step 462
Managing Indexes with PROC DATASETS 464
Managing Indexes with PROC SQL
466
Documenting and Maintaining Indexes 467
Summary
473
Quiz
477

Chapter 14

4 Combining Data Vertically

481

Overview
482

Using a FILENAME Statement 483
Using an INFILE Statement
486
Appending SAS Data Sets 494
Additional Features
502
Summary
504
Quiz
507

Chapter 15

4 Combining Data Horizontally

513

Overview
514
Reviewing Terminology
515
Working with Lookup Values Outside of SAS Data Sets
519
Combining Data with the DATA Step Match-Merge
521
Using PROC SQL to Join Data
525
Comparing DATA Step Match-Merges and PROC SQL Joins
526
Combining Summary Data and Detail Data

535
Using an Index to Combine Data
541
Using a Transactional Data Set
546
Summary
549
Quiz
554

Chapter 16

4 Using Lookup Tables to Match Data

559

Overview
560
Using Multidimensional Arrays
561
Using Stored Array Values
564
Using PROC TRANSPOSE
570
Merging the Transposed Data Set
574
Using Hash Objects as Lookup Tables 579
Summary
592
Quiz

597

Chapter 17

4 Formatting Data

603

Overview
604
Creating Custom Formats Using the VALUE Statement 605
Creating Custom Formats Using the PICTURE Statement
608
Managing Custom Formats 612
Using Custom Formats 615
Creating Formats from SAS Data Sets
618


viii

Creating SAS Data Sets from Custom Fomats
Summary
626
Quiz
629

Chapter 18

622


4 Modifying SAS Data Sets and Tracking Changes

Overview
634
Using the MODIFY Statement
635
Modifying All Observations in a SAS Data Set 637
Modifying Observations Using a Transaction Data Set
Modifying Observations Located by an Index
641
Controlling the Update Process
645
Understanding Integrity Constraints 647
Placing Integrity Constraints on a Data Set
649
Documenting Integrity Constraints 653
Removing Integrity Constraints 653
Understanding Audit Trails
654
Initiating and Reading Audit Trails
655
Controlling Data in the Audit Trail
657
Controlling the Audit Trail
661
Understanding Generation Data Sets
662
Initiating Generation Data Sets
663

Processing Generation Data Sets 664
Summary
669
Quiz
673

PART

4

Optimizing SAS Programs
Chapter 19

677

4 Introduction to Efficient SAS Programming

Overview
679
Overview of Computing Resources 680
Assessing Efficiency Needs at Your Site
681
Understanding Efficiency Trade-offs
683
Using SAS System Options to Track Resources 683
Using Benchmarks to Compare Techniques
685
Summary
687


Chapter 20

4 Controlling Memory Usage

689

Overview
689
Controlling Page Size and the Number of Buffers
Using the SASFILE Statement
698
Additional Features
703
Summary
704
Quiz
705

Chapter 21
Overview

4 Controlling Data Storage Space
708

638

690

707


679

633


ix

Reducing Data Storage Space for Character Variables
709
Reducing Data Storage Space for Numeric Variables
711
Compressing Data Files 719
Using SAS DATA Step Views to Conserve Data Storage Space
Summary
738
Quiz
739

Chapter 22

4 Utilizing Best Practices

741

Overview
742
Executing Only Necessary Statements
743
Eliminating Unnecessary Passes through the Data
Reading and Writing Only Essential Data

763
Storing Data in SAS Data Sets
774
Avoiding Unnecessary Procedure Invocation
776
Summary
781
Quiz
782

Chapter 23

730

4 Selecting Efficient Sorting Strategies

758

785

Overview
786
Avoiding Unnecessary Sorts
787
Using a Threaded Sort
800
Calculating and Allocating Sort Resources
802
Handling Large Data Sets
805

Removing Duplicate Observations Efficiently 816
Additional Features
824
Summary
829
Quiz
831

Chapter 24

4 Querying Data Efficiently

833

Overview
834
Using an Index for Efficient WHERE Processing 836
Identifying Available Indexes
839
Identifying Conditions That Can Be Optimized 842
Estimating the Number of Observations
845
Comparing Probable Resource Usage 848
Deciding Whether to Create an Index
850
Comparing Procedures That Produce Detail Reports 854
Comparing Tools for Summarizing Data
856
Summary
874

Quiz
876

PART

5

Quiz Answer Keys
Appendix 1

879

4 Quiz Answer Keys

881

Chapter 1: Performing Queries Using PROC SQL 881
Chapter 2: Performing Advanced Queries Using PROC SQL

884


x

Chapter 3: Combining Tables Horizontally Using PROC SQL 889
Chapter 4: Combining Tables Vertically Using PROC SQL
895
Chapter 5: Creating and Managing Tables Using PROC SQL 903
Chapter
Chapter

Chapter
Chapter

6:
7:
8:
9:

Creating and Managing Indexes Using PROC SQL 906
Creating and Managing Views Using PROC SQL
910
Managing Processing Using PROC SQL 913
Introducing Macro Variables
916

Chapter 10: Processing Macro Variables at Execution Time
Chapter 11: Creating and Using Macro Programs 923
Chapter 12: Storing Macro Programs 928

919

Chapter 13: Creating Samples and Indexes 931
Chapter 14: Combining Data Vertically
935
Chapter 15: Combining Data Horizontally
941
Chapter 16: Using Lookup Tables to Match Data
946
Chapter 17: Formatting Data 952
Chapter 18: Modifying SAS Data Sets and Tracking Changes

Chapter
Chapter
Chapter
Chapter

19:
20:
21:
22:

Introduction to Efficient SAS Programming
Controlling Memory Usage
958
Controlling Data Storage Space 959
Utilizing Best Practices 961

Chapter 23: Selecting Efficient Sorting Strategies
Chapter 24: Querying Data Efficiently
965

Index

967

963

958

954



xi

About This Book and CD

What’s New
For future updates, go to
and
select the Updates link.

Purpose
The SAS Certification Prep Guide: Advanced Programming for SAS 9 prepares you to
take the SAS Advanced Programming exam for SAS 9. New and experienced SAS users
who want to prepare for this exam will find this guide to be an invaluable, convenient,
and comprehensive resource that covers all of the topics tested on the exam.
Major topics include SQL processing with SAS, the SAS macro language, advanced
SAS programming techniques, and optimizing SAS programs. You will also become
familiar with the enhancements and new functionality that are available in SAS 9.
The book includes quizzes that enable you to test your understanding of material in
each chapter. Additionally, solutions to all quizzes are included at the back of the book.

Audience
The SAS Certification Prep Guide: Advanced Programming for SAS 9 is for new or
experienced SAS programmers who want to prepare for the SAS Advanced
Programming exam for SAS 9.

Prerequisites
Candidates must earn the SAS Certified Base Programmer for SAS 9 credential
before taking the SAS Advanced Programming exam for SAS 9. The SAS Certification
Prep Guide: Base Programming for SAS 9 covers all of the objectives tested on the SAS

Base Programming exam for SAS 9, including importing and exporting raw data files,
creating and modifying SAS data sets, and identifying and correcting data syntax and
programming logic errors.
If you want to test yourself to see if you have the necessary prerequisite base
programming knowledge, go to support.sas.com/certify, where you will find
information about certification credentials, exam preparation, and more!


xii About This Book and CD

How to Use This Book and CD
The SAS Certification Prep Guide: Advanced Programming for SAS 9 includes a companion
CD, which can be found in the envelope inside the back cover of this book. The CD will
enable you to practice your new skills.

Syntax Conventions for This Book
This is an example of how the general form of SAS code is shown in the book:
General form, basic PROC SQL step to perform a query:

PROC SQL;
SELECT column-1<,...column-n>
FROM table-1|view-1<,...table-n|view-n>
W HERE expression>
where
PROC SQL
invokes the SQL procedure
SELECT

specifies the column(s) to be selected
FROM
specifies the table(s) to be queried
WHERE
subsets the data based on a condition
GROUP BY
classifies the data into groups based on the specified column(s)
ORDER BY
sorts the rows that the query returns by the value(s) of the specified column(s).

For example, in the general form above,
SELECT, FROM, WHERE, GROUP BY, and ORDER BY
are in uppercase because they must be spelled as shown
column-1, table-1, view-1, and expression
are in italics because each represents a value that you supply
<,...column-n>
is enclosed in angle brackets because it is optional syntax
table-1 and view-1
are separated by a vertical bar ( | )to indicate that they are mutually exclusive.
The general forms of SAS statements and commands that are shown in this book
include only the syntax that you need to know to prepare for the certification exam. For
complete syntax, see the appropriate SAS reference guide.


About This Book and CD

xiii

SAS Certification Practice Exam: Advanced Programming for SAS 9
The SAS Certification Practice Exam: Advanced Programming for SAS 9 was

designed to help you prepare for the SAS Advanced Programming exam for SAS 9. This
practice exam was constructed to test the same knowledge and skills as the official
certification exam. You can access this exam under the SAS Certification category at
support.sas.com/selfpaced. (There is an additional fee charged for this practice
exam.)

For information about how to register for the official SAS Advanced Programming
exam for SAS 9, see the SAS Global Certification Program Web site at
support.sas.com/certify/.

Additional Resources
Other resources might be helpful when learning SAS programming. You can refer to
them as needed to enhance your understanding of the material covered in this book. You
can access SAS help, documentation, and other resources from your SAS software or on
the Web.
From SAS Software
Help

SAS 9: Select Help Ź Help and Documentation.
SAS Enterprise Guide: Select Help Ź SAS Enterprise
Guide Help.

Documentation

SAS 9: Select Help Ź SAS Help and Documentation.
SAS Enterprise Guide: Access online documentation on the
Web (see On the Web below).


xiv About This Book and CD


On the Web
support.sas.com/resources

System Requirements
Install Center
Product Documentation
Papers
Samples & SAS Notes
Focus Areas

support.sas.com/learn

Bookstore
Training
Certification
SAS Learning Edition
Higher Education Resources
SAS OnDemand for Academics

support.sas.com/community

Users Groups
Events
E-newsletters
RSS & Blogs
Discussion Forums


1


1

P A R T

SQL Processing With SAS
Chapter

1. . . . . . . . . . Performing Queries Using PROC SQL

Chapter

2 . . . . . . . . . . Performing Advanced Queries Using PROC SQL

25

Chapter

3 . . . . . . . . . . Combining Tables Horizontally Using PROC SQL

79

Chapter

4 . . . . . . . . . . Combining Tables Vertically Using PROC SQL

Chapter

5 . . . . . . . . . . Creating and Managing Tables Using PROC SQL


Chapter

6 . . . . . . . . . . Creating and Managing Indexes Using PROC SQL

Chapter

7 . . . . . . . . . . Creating and Managing Views Using PROC SQL

Chapter

8 . . . . . . . . . . Managing Processing Using PROC SQL

3

261

125
159
221
243


2


3

CHAPTER

1

Performing Queries Using PROC
SQL

Overview 4
Introduction 4
Objectives 4
PROC SQL Basics 4
How PROC SQL Is Unique 5
Writing a PROC SQL Step 6
The SELECT Statement 7
Selecting Columns 8
Creating New Columns 9
Specifying the Table 10
Specifying Subsetting Criteria 10
Ordering Rows 10
Ordering by Multiple Columns 12
Querying Multiple Tables 12
Specifying Columns That Appear in Multiple Tables 13
Specifying Multiple Table Names 14
Subsetting Rows 14
Ordering Rows 15
Summarizing Groups of Data 16
Example 16
Summary Functions 16
Creating Output Tables 17
Example 17
Additional Features 18
Summary 19
Text Summary 19
PROC SQL Basics 19

Writing a PROC SQL Step 19
Selecting Columns 19
Specifying the Table 19
Specifying Subsetting Criteria 19
Ordering Rows 19
Querying Multiple Tables 20
Summarizing Groups of Data 20
Creating Output Tables 20
Additional Features 20
Syntax 20
Sample Programs 20
Querying a Table 20
Summarizing Groups of Data 21
Creating a Table from the Results of a Query on Two Tables

21


4

Overview

4

Chapter 1

Points to Remember
Quiz

21


21

Overview
Introduction
Sometimes you need quick answers to questions about your data. You might want to
query (retrieve data from) a single SAS data set or a combination of data sets to
3 examine relationships between data values
3 view a subset of your data
3 compute values quickly.
The SQL procedure (PROC SQL) provides an easy, flexible way to query and combine
your data. This chapter shows you how to create a basic query using one or more tables
(data sets). You’ll also learn how to create a new table from your query.

Objectives
In this chapter, you learn to
3 invoke the SQL procedure
3 select columns
3 define new columns
3 specify the table(s) to be read
3 specify subsetting criteria
3 order rows by values of one or more columns
3 group results by values of one or more columns
3 end the SQL procedure
3 summarize data
3 generate a report as the output of a query
3 create a table as the output of a query.

PROC SQL Basics
PROC SQL is the SAS implementation of Structured Query Language (SQL), which

is a standardized language that is widely used to retrieve and update data in tables and
in views that are based on those tables.


Performing Queries Using PROC SQL

4

How PROC SQL Is Unique

5

The following chart shows terms used in data processing, SAS, and SQL that are
synonymous. The SQL terms are used in this chapter.
Data Processing

SAS

SQL

file

SAS data set

table

record

observation


row

field

variable

column

PROC SQL can often be used as an alternative to other SAS procedures or the DATA
step. You can use PROC SQL to

3
3
3
3
3
3

retrieve data from and manipulate SAS tables
add or modify data values in a table
add, modify, or drop columns in a table
create tables and views
join multiple tables (whether or not they contain columns with the same name)
generate reports.

Like other SAS procedures, PROC SQL also enables you to combine data from two or
more different types of data sources and present them as a single table. For example,
you can combine data from two different types of external databases, or you can
combine data from an external database and a SAS data set.


How PROC SQL Is Unique
PROC SQL differs from most other SAS procedures in several ways:

3 Unlike other PROC statements, many statements in PROC SQL are composed of
clauses. For example, the following PROC SQL step contains two statements: the
PROC SQL statement and the SELECT statement. The SELECT statement
contains several clauses: SELECT, FROM, WHERE, and ORDER BY.
proc sql;
select empid,jobcode,salary,
salary*.06 as bonus
from sasuser.payrollmaster
where salary<32000
order by jobcode;

3 The PROC SQL step does not require a RUN statement. PROC SQL executes each
query automatically. If you use a RUN statement with a PROC SQL step, SAS


6

Writing a PROC SQL Step

4

Chapter 1

ignores the RUN statement, executes the statements as usual, and generates the
note shown below in the SAS log.
Table 1.1 SAS Log
1884 proc sql;

1885
select empid,jobcode,salary,
1886
salary*.06 as bonus
1887
from sasuser.payrollmaster
1888
where salary<32000
1889
order by jobcode;
1890 run;
NOTE: PROC SQL statements are executed immediately;
The RUN statement has no effect.

3 Unlike many other SAS procedures, PROC SQL continues to run after you submit
a step. To end the procedure, you must submit another PROC step, a DATA step,
or a QUIT statement, as shown:
proc sql;
select empid,jobcode,salary,
salary*.06 as bonus
from sasuser.payrollmaster
where salary<32000
order by jobcode;
quit;

When you submit a PROC SQL step without ending it, the status line displays the
message PROC SQL running.
Note: As a precaution, SAS Enterprise Guide automatically adds a QUIT
statement to your code when you submit it to SAS. However, you should get in the
habit of adding the QUIT statement to your code. 4


Writing a PROC SQL Step
Before creating a query, you must first reference the library in which your table is
stored. Then you write a PROC SQL step to query your table.


Performing Queries Using PROC SQL

4

The SELECT Statement

7

General form, basic PROC SQL step to perform a query:

PROC SQL;
SELECT column-1<,...column-n>
FROM table-1|view-1<,...table-n|view-n>
<WHERE expression>
where
PROC SQL
invokes the SQL procedure
SELECT
specifies the column(s) to be selected
FROM
specifies the table(s) to be queried
WHERE

subsets the data based on a condition
GROUP BY
classifies the data into groups based on the specified column(s)
ORDER BY
sorts the rows that the query returns by the value(s) of the specified column(s).

CAUTION:
Unlike other SAS procedures the order of clauses with a SELECT statement in
PROC SQL is important. Clauses must appear in the order shown above. 4
Note: A query can also include a HAVING clause, which is introduced at the end of
this chapter. To learn more about the HAVING clause, see Chapter 2, “Performing
Advanced Queries Using PROC SQL,” on page 25. 4

The SELECT Statement
The SELECT statement, which follows the PROC SQL statement, retrieves and
displays data. It is composed of clauses, each of which begins with a keyword and is
followed by one or more components. The SELECT statement in the following sample
code contains four clauses: the required clauses SELECT and FROM, and the optional
clauses WHERE and ORDER BY. The end of the statement is indicated by a semicolon.
proc sql;
|-select empid,jobcode,salary,
|
salary*.06 as bonus
|----from sasuser.payrollmaster
|----where salary<32000
|----order by jobcode;

Note: A PROC SQL step that contains one or more SELECT statements is referred
to as a PROC SQL query. The SELECT statement is only one of several statements
that can be used with PROC SQL. 4

The following PROC SQL query creates the output report that is shown below:


8

Selecting Columns

4

Chapter 1

proc sql;
select empid,jobcode,salary,
salary*.06 as bonus
from sasuser.payrollmaster
where salary<32000
order by jobcode;

A PROC SQL query produces a result set that can be output as a report, a table, or a
PROC SQL view.
Type of Output

PROC SQL Statements

report

SELECT

table


CREATE TABLE and SELECT

PROC SQL view

CREATE VIEW and SELECT

Note: The CREATE TABLE statement is introduced later in this chapter. You can
learn about creating tables in Chapter 5, “Creating and Managing Tables Using PROC
SQL,” on page 159. You can learn more about PROC SQL views in Chapter 7, “Creating
and Managing Views Using PROC SQL,” on page 243. 4
You will learn more about the SELECT statement in the following sections.

Selecting Columns
To specify which column(s) to display in a query, you write a SELECT clause, the
first clause in the SELECT statement. After the keyword SELECT, list one or more
column names and separate the column names with commas. In the SELECT clause,
you can both specify existing columns (columns that are already stored in a table) and
create new columns.
The following SELECT clause specifies the columns EmpID, JobCode, Salary, and
bonus. The columns EmpID, JobCode, and Salary are existing columns. The column
named bonus is a new column.
proc sql;
select empid,jobcode,salary,
salary*.06 as bonus
from sasuser.payrollmaster
where salary<32000
order by jobcode;


4


Performing Queries Using PROC SQL

Creating New Columns

9

Creating New Columns
You can create new columns that contain either text or a calculation. New columns
will appear in output, along with any existing columns that are selected. Keep in mind
that new columns exist only for the duration of the query, unless a table or a view is
created.
To create a new column, include any valid SAS expression in the SELECT clause list
of columns. You can optionally assign a column alias, a name, to a new column by using
the keyword AS followed by the name that you would like to use.
Note:

A column alias must follow the rules for SAS names.

4

In the sample PROC SQL query, shown below, an expression is used to calculate the
new column: the values of Salary are multiplied by .06. The keyword AS is used to
assign the column alias bonus to the new column.
proc sql;
select empid,jobcode,salary,
salary*.06 as bonus
from sasuser.payrollmaster
where salary<32000
order by jobcode;


A column alias is useful because it allows you to reference the column elsewhere in
the query.
Note: You can learn more about referencing a calculated column from other clauses
in Chapter 2, “Performing Advanced Queries Using PROC SQL,” on page 25. 4
Also, the column alias will appear as a column heading in the output.
The following output shows how the calculated column bonus is displayed. Notice
that the column alias bonus appears in lowercase, exactly as it is specified in the
SELECT clause.

In the SELECT clause, you can optionally specify a label for an existing or a new
column. If both a label and a column alias are specified for a new column, the label will
be displayed as the column heading in the output. If only a column alias is specified, it
is important that you specify the column alias exactly as you want it to appear in the
output.


10

Specifying the Table

4

Chapter 1

Note: You can learn about creating new columns that contain text and about
specifying labels for columns in Chapter 2, “Performing Advanced Queries Using PROC
SQL,” on page 25. 4

Specifying the Table

After writing the SELECT clause, you specify the table to be queried in the FROM
clause. Type the keyword FROM, followed by the name of the table, as shown:
proc sql;
select empid,jobcode,salary,
salary*.06 as bonus
from sasuser.payrollmaster
where salary<32000
order by jobcode;

The PROC SQL step above queries the permanent SAS table Payrollmaster, which is
stored in a SAS library to which the libref Sasuser has been assigned.

Specifying Subsetting Criteria
To subset data based on a condition, use a WHERE clause in the SELECT statement.
As in the WHERE statement and the WHERE command used in other SAS procedures,
the expression in the WHERE clause can be any valid SAS expression. In the WHERE
clause, you can specify any column(s) from the underlying table(s). The columns
specified in the WHERE clause do not have to be specified in the SELECT clause.
In the following PROC SQL query, the WHERE clause selects rows in which the
value of the column Salary is less than 32,000. The output is also shown.
proc sql;
select empid,jobcode,salary,
salary*.06 as bonus
from sasuser.payrollmaster
where salary<32000
order by jobcode;

Ordering Rows
The order of rows in the output of a PROC SQL query cannot be guaranteed, unless
you specify a sort order. To sort rows by the values of specific columns, you can use the



×