SAS® 9.1.3 Language Reference: Concepts Third Edition The correct bibliographic citation for this manual is as follows: SAS Institute Inc. 2005. SAS ® 9.1.3 Language Reference: Concepts, Third Edition. Cary, NC: SAS Institute Inc. SAS® 9.1.3 Language Reference: Concepts, Third Edition Copyright © 2005, SAS Institute Inc., Cary, NC, USA ISBN 1-59047-905-X (e-book) ISBN-13: 978-1-59047-849-0 ISBN-10: 1-59047-840-1 (hard-copy book) 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 2005 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
PART1 SAS System Concepts 1
Chapter 1 R Essential Concepts of Base SAS Software 3 What Is SAS? 3 Overview of Base SAS Software 4 Components of the SAS Language 4 Ways to Run Your SAS Session 7 Customizing Your SAS Session 9 Conceptual Information about Base SAS Software 10
Chapter 2 R SAS Processing 11 Definition of SAS Processing 11 Types of Input to a SAS Program 12 The DATA Step 13 The PROC Step 14
Chapter 3 R Rules for Words and Names in the SAS Language 15 Words in the SAS Language 15 Names in the SAS Language 18
Chapter 4 R SAS Language Elements 23 What Are the SAS Language Elements? 25 Data Set Options 25 Formats and Informats 27 Functions and CALL Routines 38 ARM Macros 55 Statements 69 SAS System Options 71
Chapter 5 R SAS Variables 77 Definition of SAS Variables 78 SAS Variable Attributes 78 Ways to Create Variables 80 Variable Type Conversions 84 Aligning Variable Values 85 Automatic Variables 85 SAS Variable Lists 86 Dropping, Keeping, and Renaming Variables 88 Numeric Precision in SAS Software 90
Chapter 6 R Missing Values 101 Definition of Missing Values 101 iv
Special Missing Values 102 Order of Missing Values 103 When Variable Values Are Automatically Set to Missing by SAS 104 When Missing Values Are Generated by SAS 105 Working with Missing Values 107
Chapter 7 R Expressions 109 Definitions for SAS Expressions 110 Examples of SAS Expressions 110 SAS Constants in Expressions 110 SAS Variables in Expressions 116 SAS Functions in Expressions 117 SAS Operators in Expressions 117
Chapter 8 R Dates, Times, and Intervals 127 About SAS Date, Time, and Datetime Values 127 About Date and Time Intervals 137
Chapter 9 R Error Processing and Debugging 147 Types of Errors in SAS 147 Error Processing in SAS 156 Debugging Logic Errors in the DATA Step 159
Chapter 10 R SAS Output 161 Definitions for SAS Output 162 Routing SAS Output 163 The SAS Log 163 Traditional SAS Listing Output 167 Changing the Destination of the Log and the Output 170 Output Delivery System 170
Chapter 11 R BY-Group Processing in SAS Programs 193 Definition of BY-Group Processing 193 References for BY-Group Processing 193
Chapter 12 R WHERE-Expression Processing 195 Definition of WHERE-Expression Processing 195 Where to Use a WHERE Expression 196 Syntax of WHERE Expression 197 Combining Expressions by Using Logical Operators 205 Constructing Efficient WHERE Expressions 206 Processing a Segment of Data That Is Conditionally Selected 206 Deciding Whether to Use a WHERE Expression or a Subsetting IF Statement 208
Chapter 13 R Optimizing System Performance 211 Definitions for Optimizing System Performance 211 Collecting and Interpreting Performance Statistics 212 v
Techniques for Optimizing I/O 213 Techniques for Optimizing Memory Usage 218 Techniques for Optimizing CPU Performance 218 Calculating Data Set Size 219
Chapter 14 R Support for Parallel Processing 221 Definition of Parallel Processing 221 Threaded I/O 221 Threaded Application Processing 222
Chapter 15 R Monitoring Performance Using Application Response Measurement (ARM) 223 Introduction to ARM 223 How Does ARM Work? 225 Will ARM Affect an Application’s Performance? 225 Using the ARM Interface 226 Examples of Gathering Performance Data 229
Chapter 16 R The SAS Registry 233 Introduction to the SAS Registry 234 Managing the SAS Registry 236 Configuring Your Registry 244
Chapter 17 R Printing with SAS 247 Introduction to Universal Printing 248 Managing Printing Tasks with the Universal Printing User Interface 250 Configuring Universal Printing with Programming Statements 269 Forms Printing 275
PART2 Windowing Environment Concepts 277
Chapter 18 R Introduction to the SAS Windowing Environment 279 Basic Features of the SAS Windowing Environment 279 Main Windows of the SAS Windowing Environment 284
Chapter 19 R Managing Your Data in the SAS Windowing Environment 301 Introduction to Managing Your Data in the SAS Windowing Environment 301 Copying and Viewing Files in a Data Library 301 Using the Workspace to Manipulate Data in a Data Set 306 Importing and Exporting Data 314
PART3 DATA Step Concepts 319
Chapter 20 R DATA Step Processing 321 Why Use a DATA Step? 322 Overview of DATA Step Processing 322 Processing a DATA Step: A Walkthrough 325 vi
About DATA Step Execution 330 About Creating a SAS Data Set with a DATA Step 335 Writing a Report with a DATA Step 339 The DATA Step and ODS 347
Chapter 21 R Reading Raw Data 349 Definition of Reading Raw Data 349 Ways to Read Raw Data 350 Kinds of Data 351 Sources of Raw Data 354 Reading Raw Data with the INPUT Statement 355 How SAS Handles Invalid Data 360 Reading Missing Values in Raw Data 360 Reading Binary Data 362 Reading Column-Binary Data 364
Chapter 22 R BY-Group Processing in the DATA Step 367 Definitions for BY-Group Processing 367 Syntax for BY-Group Processing 368 Understanding BY Groups 369 Invoking BY-Group Processing 370 Determining Whether the Data Requires Preprocessing for BY-Group Processing 370 Preprocessing Input Data for BY-Group Processing 371 How the DATA Step Identifies BY Groups 372 Processing BY-Groups in the DATA Step 374
Chapter 23 R Reading, Combining, and Modifying SAS Data Sets 379 Definitions for Reading, Combining, and Modifying SAS Data Sets 381 Overview of Tools 381 Reading SAS Data Sets 382 Combining SAS Data Sets: Basic Concepts 383 Combining SAS Data Sets: Methods 394 Error Checking When Using Indexes to Randomly Access or Update Data 420
Chapter 24 R Using DATA Step Component Objects 429 Introduction 429 Using the Hash Object 430 Using the Hash Iterator Object 437
Chapter 25 R Array Processing 441 Definitions for Array Processing 441 A Conceptual View of Arrays 442 Syntax for Defining and Referencing an Array 444 Processing Simple Arrays 444 Variations on Basic Array Processing 448 Multidimensional Arrays: Creating and Processing 449 Specifying Array Bounds 451 vii
Examples of Array Processing 453
PART4 SAS Files Concepts 459
Chapter 26 R SAS Data Libraries 461 Definition of a SAS Data Library 461 Library Engines 463 Library Names 463 Accessing Remote SAS Libraries on SAS/CONNECT, SAS/SHARE, and WebDAV Servers 465 Library Concatenation 466 Permanent and Temporary Libraries 468 SAS System Libraries 469 Sequential Data Libraries 472 Tools for Managing Libraries 472
Chapter 27 R SAS Data Sets 475 Definition of a SAS Data Set 475 Descriptor Information for a SAS Data Set 475 Data Set Names 476 Special SAS Data Sets 478 Sorted Data Sets 479 Tools for Managing Data Sets 479 Viewing and Editing SAS Data Sets 480
Chapter 28 R SAS Data Files 481 Definition of a SAS Data File 483 Differences between Data Files and Data Views 483 Understanding an Audit Trail 485 Understanding Generation Data Sets 493 Understanding Integrity Constraints 499 Understanding SAS Indexes 512 Compressing Data Files 532
Chapter 29 R SAS Data Views 535 Definition of SAS Data Views 535 Benefits of Using SAS Data Views 536 When to Use SAS Data Views 537 DATA Step Views 537 PROC SQL Views 541 Comparing DATA Step and PROC SQL Views 542 SAS/ACCESS Views 542
Chapter 30 R Stored Compiled DATA Step Programs 545 Definition of a Stored Compiled DATA Step Program 545 Uses for Stored Compiled DATA Step Programs 545 viii
Restrictions and Requirements for Stored Compiled DATA Step Programs 546 How SAS Processes Stored Compiled DATA Step Programs 546 Creating a Stored Compiled DATA Step Program 547 Executing a Stored Compiled DATA Step Program 549 Differences between Stored Compiled DATA Step Programs and DATA Step Views 552 Examples of DATA Step Programs 553
Chapter 31 R DICTIONARY Tables 555 Definition of a DICTIONARY Table 555 How to View DICTIONARY Tables 555
Chapter 32 R SAS Catalogs 559 Definition of a SAS Catalog 559 SAS Catalog Names 559 Tools for Managing SAS Catalogs 560 Profile Catalog 561 Catalog Concatenation 562
Chapter 33 R About SAS/ACCESS Software 567 Definition of SAS/ACCESS Software 567 Dynamic LIBNAME Engine 567 SQL Procedure Pass-Through Facility 569 ACCESS Procedure and Interface View Engine 570 DBLOAD Procedure 571 Interface DATA Step Engine 571
Chapter 34 R Processing Data Using Cross-Environment Data Access (CEDA) 573 Definition of Cross-Environment Data Access (CEDA) 573 Advantages of CEDA 574 SAS File Processing with CEDA 574 Processing a File with CEDA 576 Alternatives to Using CEDA 578 Creating New Files in a Foreign Data Representation 579 Examples of Using CEDA 579
Chapter 35 R SAS 9.1 Compatibility with SAS Files From Earlier Releases 581 Introduction to Version Compatibility 581 Comparing SAS System 9 to Earlier Releases 581 Using SAS Library Engines 582
Chapter 36 R File Protection 585 Definition of a Password 585 Assigning Passwords 586 Removing or Changing Passwords 588 Using Password-Protected SAS Files in DATA and PROC Steps 588 How SAS Handles Incorrect Passwords 589 Assigning Complete Protection with the PW= Data Set Option 589 ix
Using Passwords with Views 590 SAS Data File Encryption 592
Chapter 37 R SAS Engines 595 Definition of a SAS Engine 595 Specifying an Engine 595 How Engines Work with SAS Files 596 Engine Characteristics 597 About Library Engines 600 Special-Purpose Engines 602
Chapter 38 R SAS File Management 605 Improving Performance of SAS Applications 605 Moving SAS Files Between Operating Environments 605 Repairing Damaged SAS Files 605
Chapter 39 R External Files 609 Definition of External Files 609 Referencing External Files Directly 610 Referencing External Files Indirectly 610 Referencing Many External Files Efficiently 612 Referencing External Files with Other Access Methods 612 Working with External Files 613
PART5 Industry Protocols Used in SAS 615
Chapter 40 R The SMTP E-Mail Interface 617 Sending E-Mail through SMTP 617 System Options That Control SMTP E-Mail 617 Statements That Control SMTP E-mail 618
Chapter 41 R Universal Unique Identifiers 619 Universal Unique Identifiers and the Object Spawner 619 Using SAS Language Elements to Assign UUIDs 621
PART6 Appendices 623
Appendix 1 R Recommended Reading 625 Recommended Reading 625
Index 627 x 1 PART1
SAS System Concepts
Chapter 1...... Essential Concepts of Base SAS Software 3
Chapter 2...... SAS Processing 11
Chapter 3...... Rules for Words and Names in the SAS Language 15
Chapter 4...... SAS Language Elements 23
Chapter 5...... SAS Variables 77
Chapter 6...... Missing Values 101
Chapter 7...... Expressions 109
Chapter 8...... Dates, Times, and Intervals 127
Chapter 9...... Error Processing and Debugging 147
Chapter 10...... SAS Output 161
Chapter 11...... BY-Group Processing in SAS Programs 193
Chapter 12...... WHERE-Expression Processing 195
Chapter 13...... Optimizing System Performance 211
Chapter 14...... Support for Parallel Processing 221
Chapter 15...... Monitoring Performance Using Application Response Measurement (ARM) 223 2
Chapter 16...... The SAS Registry 233
Chapter 17...... Printing with SAS 247 3
CHAPTER 1 Essential Concepts of Base SAS Software
What Is SAS? 3 Overview of Base SAS Software 4 Components of the SAS Language 4 SAS Files 4 SAS Data Sets 5 External Files 5 Database Management System Files 6 SAS Language Elements 6 SAS Macro Facility 6 Ways to Run Your SAS Session 7 Starting a SAS Session 7 Different Types of SAS Sessions 7 SAS Windowing Environment 7 Interactive Line Mode 8 Noninteractive Mode 8 Batch Mode 9 Customizing Your SAS Session 9 Setting Default System Option Settings 9 Executing Statements Automatically 9 Customizing the SAS Windowing Environment 10 Conceptual Information about Base SAS Software 10 SAS System Concepts 10 DATA Step Concepts 10 SAS Files Concepts 10
What Is SAS?
SAS is a set of solutions for enterprise-wide business users as well as a powerful fourth-generation programming language for performing tasks such as these: data entry, retrieval, and management report writing and graphics statistical and mathematical analysis business planning, forecasting, and decision support operations research and project management quality improvement applications development With Base SAS software as the foundation, you can integrate with SAS many SAS business solutions that enable you to perform large scale business functions, such as 4 Overview of Base SAS Software R Chapter 1
data warehousing and data mining, human resources management and decision support, financial management and decision support, and others.
Overview of Base SAS Software
The core of the SAS System is Base SAS software, which consists of the following: SAS language a programming language that you use to manage your data. SAS procedures software tools for data analysis and reporting. macro facility a tool for extending and customizing SAS software programs and for reducing text in your programs. DATA step a programming tool that helps you find logic problems in DATA step debugger programs. Output Delivery a system that delivers output in a variety of easy-to-access formats, System (ODS) such as SAS data sets, listing files, or Hypertext Markup Language (HTML). SAS windowing an interactive, graphical user interface that enables you to easily environment run and test your SAS programs. This document, along with SAS Language Reference: Dictionary, covers only the SAS language. For a complete guide to Base SAS software functionality, also see these documents: SAS Output Delivery System: User’s Guide, SAS National Language Support (NLS): User’s Guide, Base SAS Procedures Guide, SAS Metadata LIBNAME Engine: User’s Guide, SAS XML LIBNAME Engine User’s Guide, Base SAS Glossary, SAS Macro Language: Reference, and the Getting Started with SAS online tutorial. The SAS windowing environment is described in the online Help.
Components of the SAS Language
SAS Files When you work with SAS, you use files that are created and maintained by SAS, as well as files that are created and maintained by your operating environment, and that are not related to SAS. Files with formats or structures known to SAS are referred to as SAS files. All SAS files reside in a SAS data library. The most commonly used SAS file is a SAS data set. A SAS data set is structured in a format that SAS can process. Another common type of SAS file is a SAS catalog. Many different kinds of information that are used in a SAS job are stored in SAS catalogs, such as instructions for reading and printing data values, or function key settings that you use in the SAS windowing environment. A SAS stored program is a type of SAS file that contains compiled code that you create and save for repeated use. Operating Environment Information: In some operating environments, a SAS data library is a physical relationship among files; in others, it is a logical relationship. Refer to the SAS documentation for your operating environment for details about the characteristics of SAS data libraries in your operating environment. R Essential Concepts of Base SAS Software R External Files 5
SAS Data Sets There are two kinds of SAS data sets: SAS data file SAS data view. A SAS data file both describes and physically stores your data values. A SAS data view, on the other hand, does not actually store values. Instead, it is a query that creates a logical SAS data set that you can use as if it were a single SAS data set. It enables you to look at data stored in one or more SAS data sets or in other vendors’ software files. SAS data views enable you to create logical SAS data sets without using the storage space required by SAS data files. A SAS data set consists of the following: descriptor information data values. The descriptor information describes the contents of the SAS data set to SAS. The data values are data that has been collected or calculated. They are organized into rows, called observations, and columns, called variables. An observation is a collection of data values that usually relate to a single object. A variable is the set of data values that describe a given characteristic. The following figure represents a SAS data set.
Figure 1.1 Representation of a SAS Data Set descriptor portion descriptive information
variables
ID NAME TEAM STRTWGHT ENDWGHT
1 1023 David Shaw red 189 165
2 1049 Amelia Serrano yellow 145 124 observation data values 3 1219 Alan Nance red 210 192
4 1246 Ravi Sinha yellow 194 177
5 1078 Ashley McKnight red 127 118
Usually, an observation is the data that is associated with an entity such as an inventory item, a regional sales office, a client, or a patient in a medical clinic. Variables are characteristics of these entities, such as sale price, number in stock, and originating vendor. When data values are incomplete, SAS uses a missing value to represent a missing variable within an observation.
External Files Data files that you use to read and write data, but which are in a structure unknown to SAS, are called external files. External files can be used for storing raw data that you want to read into a SAS data file 6 Database Management System Files R Chapter 1