SAS® Programming I:
Essentials
Course Notes
SAS® Programming I: Essentials Course Notes was developed by Michelle Buchecker, Sarah Calhoun,
and Larry Stewart. Editing and production support was provided by the Curriculum Development and
Support Department.
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.
IBM and all other International Business Machines Corporation product or service names are registered
trademarks or trademarks of International Business Machines Corporation in the USA and other
countries.
Oracle and all other Oracle Corporation product or service names are registered trademarks or trademarks
of Oracle Corporation in the USA and other countries.
Other brand and product names are trademarks of their respective companies.
SAS® Programming I: Essentials Course Notes
Copyright ª
United States of America. 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.
2001 by SAS Institute Inc., Cary, NC 27513, USA. All rights reserved. Printed in the
Book code 58693, course code PROG1, prepared date 25Oct01.
Table of Contents
For Your Information
iii
Course Description......................................................................................................................vi
Prerequisites ..............................................................................................................................viii
General Conventions...................................................................................................................ix
Chapter 1 Introduction ................................................................................................. 1
1.1 An Overview of the SAS System ......................................................................................... 3
Chapter 2 Getting Started with the SAS® System ...................................................... 9
2.1 Introduction to SAS Programs ........................................................................................... 11
2.2 Running SAS Programs ..................................................................................................... 16
2.3 Mastering Fundamental Concepts...................................................................................... 39
2.4 Diagnosing and Correcting Syntax Errors.......................................................................... 49
2.5 Exploring Your SAS Environment (Self-Study) ................................................................ 60
2.6 Solutions to Exercises ........................................................................................................ 75
Chapter 3 Getting Familiar with SAS® Data Sets...................................................... 77
3.1 SAS Data Libraries............................................................................................................. 79
3.2 Solutions to Exercises ........................................................................................................ 91
Chapter 4 Producing List Reports............................................................................. 93
4.1 Getting Started with the PRINT Procedure ........................................................................ 95
4.2 Sequencing and Grouping Observations .......................................................................... 106
4.3 Identifying Observations (Self-Study) ............................................................................. 114
4.4 Special WHERE Statement Operators (Self-Study)......................................................... 119
4.5 Solutions to Exercises ...................................................................................................... 122
iv
For Your Information
Chapter 5 Enhancing Output ................................................................................... 125
5.1 Customizing Report Appearance...................................................................................... 127
5.2 Formatting Data Values .................................................................................................... 132
5.3 Creating HTML Reports................................................................................................... 145
5.4 Solutions to Exercises ...................................................................................................... 151
Chapter 6 Creating SAS® Data Sets......................................................................... 153
6.1 Reading Raw Data Files: Column Input........................................................................... 155
6.2 Reading Raw Data Files: Formatted Input ....................................................................... 172
6.3 Examining Data Errors..................................................................................................... 181
6.4 Assigning Variable Attributes........................................................................................... 191
6.5 Changing Variable Attributes (Self-Study)....................................................................... 198
6.6 Reading Excel Spreadsheets (Self-Study)........................................................................ 213
6.7 Solutions to Exercises ...................................................................................................... 226
Chapter 7 DATA Step Programming ........................................................................ 231
7.1 Reading SAS Data Sets and Creating Variables............................................................... 233
7.2 Conditional Processing..................................................................................................... 245
7.3 Dropping and Keeping Variables (Self-Study)................................................................. 261
7.4 Reading Excel Spreadsheets Containing Date Fields (Self-Study).................................. 264
7.5 Solutions to Exercises ...................................................................................................... 269
Chapter 8 Combining SAS® Data Sets ..................................................................... 273
8.1 Concatenating SAS Data Sets .......................................................................................... 275
8.2 Merging SAS Data Sets.................................................................................................... 286
8.3 Combining SAS Data Sets: Additional Features (Self-Study) ......................................... 306
For Your Information
v
8.4 Solutions to Exercises ...................................................................................................... 309
Chapter 9 Producing Summary Reports................................................................. 313
9.1 Introduction to Summary Reports .................................................................................... 315
9.2 Basic Summary Reports ................................................................................................... 318
9.3 The REPORT Procedure................................................................................................... 335
9.4 The TABULATE Procedure (Self-Study)......................................................................... 350
9.5 Solutions to Exercises ...................................................................................................... 365
Chapter 10 Introduction to Graphics (Optional)..................................................... 369
10.1 Producing Bar and Pie Charts ........................................................................................ 371
10.2 Enhancing Output........................................................................................................... 384
10.3 Producing Plots .............................................................................................................. 392
10.4 Solutions to Exercises .................................................................................................... 402
Chapter 11 Additional Resources............................................................................ 405
11.1 Resources........................................................................................................................ 407
Index............................................................................................................................. 415
vi
For Your Information
Course Description
This three-day course focuses on how to
•
•
•
•
•
•
read raw data files and SAS data sets
investigate and summarize data by generating frequency tables and descriptive statistics
create SAS variables and recode data values
subset data
combine multiple SAS files
create listing, summary, HTML, and graph reports.
After completing this course, you should be able to
•
•
•
•
•
•
•
read a SAS data set
read a raw data file
combine SAS data sets through concatenation and merging
create a SAS variable through the assignment statement and conditional logic
investigate and summarize your data
calculate simple statistics
create list, summary, HTML, and graph reports.
For Your Information
vii
To learn more…
A full curriculum of general and statistical instructor-based training is available at
any of the Institute’s training facilities. Institute instructors can also provide on-site
training.
For information on other courses in the curriculum, contact the SAS Education
Division at 1-919-531-7321, or send e-mail to training@sas.com. You can also find
this information on the Web at www.sas.com/training/ as well as in the Training
Course Catalog.
For a list of other SAS books that relate to the topics covered in this Course Notes,
USA customers can contact our SAS Publishing Department at 1-800-727-3228 or
send e-mail to sasbook@sas.com. Customers outside the USA, please contact your
local SAS office.
Also, see the Publications Catalog on the Web at www.sas.com/pubs for a complete
list of books and a convenient order form.
viii
For Your Information
Prerequisites
Before attending this course, you should have completed the Introduction to Programming Concepts
Using SAS® Software course or have at least six months of programming experience.
Specifically, you should be able to
•
•
understand file structures and system commands on your operating systems
use a full-screen text editor
• write system commands to create and access system files
understand programming logic.
•