Praise from the Experts
“I wish that Ron Cody’s new book, Learning SAS® by Example: A Programmer’s Guide,
had been around when I was first learning SAS! This impressive book steadily moves the
reader from SAS novice to SAS authority in 26 well-written, well-paced chapters. All of
the basics are covered in this book; the fundamentals of the DATA step, getting data into
and out of SAS, conditional and iterative processing, SAS functions and formats,
important SAS procedures, creating frequency and tabular reports, and generating output
in publishable formats such as RTF and PDF.
“This book is well suited for people who are just learning SAS, as well as for people who
would like a broad-based SAS reference. There are plenty of solid examples that
emphasize the material in the chapters. Each chapter ends with exercises that test the
reader’s understanding of the concepts and the SAS code presented. Because of its
pacing, breadth, and scope, I have no doubt that Learning SAS® by Example: A
Programmer’s Guide will become the preferred book for learning SAS software in
business, government, and educational institutions the world over.”
Michael A. Raithel
Senior Systems Analyst
Westat
“It is refreshing to find the highlights of SAS all in one book, essentially a ‘soup to nuts’
of SAS concepts! The author has organized the book well. It starts with simple
applications of SAS and gradually works up to advanced topics like ODS and SQL. As
the name implies, every concept is presented with lucid examples.
“This book is ideal for programmers who are novice SAS users but cannot afford the time
to learn SAS in a formal manner. Even advanced users of SAS can benefit from the
excellent examples and refresh their memories of long-forgotten details of options and
syntax.”
Priya Suresh
Research Programmer Analyst
RTI International
Research Triangle Park, North Carolina
“Teachers and students will love the detailed code examples and numerous problems.
The book is destined to become a classic like Ron’s Applied Statistics and the SAS®
Programming Language, which is now in its fifth edition.”
Ian Whitlock
“Ron Cody’s newest book can serve not only as a textbook for a SAS programming
course, but also as a reference for intermediate-level programmers. The book begins with
basic data input methods and advances through merging data sets and more complex
tasks.
“I would recommend Learning SAS® by Example: A Programmer’s Guide to anyone
wishing to learn or refresh his/her SAS programming skills.”
Karol H. Katz, M.S.
Programmer Analyst
Yale University School of Medicine
“Ron Cody has earned a reputation as an excellent lecturer, instructor, and author. His
most recent work, Learning SAS® by Example: A Programmer’s Guide, does not merely
adhere to his historic level of excellence; it raises it to a new level.
“Ron has succeeded in condensing the major components of Base SAS into one easy-to-
read book. Using simple examples, he illustrates the use of the DATA step and many of
its components, basic PROCs, and even macros and ODS.
“Learning SAS® by Example: A Programmer’s Guide is now at the top of my list of texts
to assist programmers in teaching themselves SAS.”
Andrew T. Kuligowski
SouthEast SAS Users Group
Learning SAS®
by Example
A Programmer’s Guide
Ron Cody
The correct bibliographic citation for this manual is as follows: Cody, Ron. 2007. Learning SAS® by Example:
A Programmer’s Guide. Cary, NC: SAS Institute Inc.
Learning SAS® by Example: A Programmer’s Guide
Copyright © 2007, SAS Institute Inc., Cary, NC, USA
ISBN 978-1-59994-165-3
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, February 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/pubs 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
List of Programs xv
Preface xxix
Acknowledgments xxxi
Part 1 Getting Started 1
Chapter 1 What Is SAS? 3
1.1 Introduction 3
1.2 Getting Data into SAS 4
1.3 A Sample SAS Program 4
1.4 SAS Names 7
1.5 SAS Data Sets and SAS Data Types 8
1.6 The SAS Display Manager and SAS Enterprise Guide 9
1.7 Problems 9
Chapter 2 Writing Your First SAS Program 11
2.1 A Simple Program to Read Raw Data and Produce a
Report 11
2.2 Enhancing the Program 18
2.3 More on Comment Statements 20
2.4 How SAS Works (a Look Inside the “Black Box”) 22
2.5 Problems 25
Part 2 DATA Step Processing 27
Chapter 3 Reading Raw Data from External Files 29
3.1 Introduction 30
3.2 Reading Data Values Separated by Blanks 30
3.3 Specifying Missing Values with List Input 32
3.4 Reading Data Values Separated by Commas (CSV Files) 33
3.5 Using an Alternative Method to Specify an External File 34
iv Contents
3.6 Reading Data Values Separated by Delimiters Other Than
Blanks or Commas 34
3.7 Placing Data Lines Directly in Your Program (the DATALINES
Statement) 36
3.8 Specifying INFILE Options with the DATALINES Statement 37
3.9 Reading Raw Data from Fixed Columns—Method 1: Column
Input 37
3.10 Reading Raw Data from Fixed Columns—Method 2: Formatted
Input 39
3.11 Using a FORMAT Statement in a DATA Step versus in a
Procedure 43
3.12 Using Informats with List Input 43
3.13 Supplying an INFORMAT Statement with List Input 45
3.14 Using List Input with Embedded Delimiters 46
3.15 Problems 47
Chapter 4 Creating Permanent SAS Data Sets 53
4.1 Introduction 54
4.2 SAS Libraries—The LIBNAME Statement 54
4.3 Why Create Permanent SAS Data Sets? 55
4.4 Examining the Descriptor Portion of a SAS Data Set Using
PROC CONTENTS 56
4.5 Listing All the SAS Data Sets in a SAS Library Using
PROC CONTENTS 59
4.6 Viewing the Descriptor Portion of a SAS Data Set Using the
SAS Explorer 60
4.7 Viewing the Data Portion of a SAS Data Set Using PROC
PRINT 63
4.8 Viewing the Data Portion of a SAS Data Set Using the SAS
VIEWTABLE Window 64
4.9 Using a SAS Data Set as Input to a DATA Step 65
4.10 DATA _NULL_: A Data Set That Isn’t 67
4.11 Problems 68
Contents v
Chapter 5 Creating Formats and Labels 71
5.1 Adding Labels to Your Variables 71
5.2 Using Formats to Enhance Your Output 73
5.3 Regrouping Values Using Formats 76
5.4 More on Format Ranges 78
5.5 Storing Your Formats in a Format Library 79
5.6 Permanent Data Set Attributes 80
5.7 Accessing a Permanent SAS Data Set with User-Defined
Formats 82
5.8 Displaying Your Format Definitions 83
5.9 Problems 84
Chapter 6 Reading and Writing Data from an Excel
Spreadsheet 87
6.1 Introduction 87
6.2 Using the Import Wizard to Convert a Spreadsheet to a SAS
Data Set 88
6.3 Creating an Excel Spreadsheet from a SAS Data Set 93
6.4 Using an Engine to Read an Excel Spreadsheet 95
6.5 Using the SAS Output Delivery System to Convert a SAS Data
Set to an Excel Spreadsheet 96
6.6 Problems 98
Chapter 7 Performing Conditional Processing 101
7.1 Introduction 102
7.2 The IF and ELSE IF Statements 102
7.3 The Subsetting IF Statement 105
7.4 The IN Operator 107
7.5 Using a SELECT Statement for Logical Tests 108
7.6 Using Boolean Logic (AND, OR, and NOT Operators) 109
7.7 A Caution When Using Multiple OR Operators 111
7.8 The WHERE Statement 112
7.9 Some Useful WHERE Operators 113
7.10 Problems 114