SAS
®
Programming II:
Manipulating Data with the
DATA Step
Course Notes
SAS
®
Programming II: Manipulating Data with the DATA Step Course Notes was developed by
Jemshaid Cheema and Melinda Thielbar. Additional contributions were made by Marty Hultgren,
Kent Reeve, Warren Repole, and Roger Staum. 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.
Other brand and product names are trademarks of their respective companies.
SAS
®
Programming II: Manipulating Data with the DATA Step Course Notes
Copyright
2004 by SAS Institute Inc., Cary, NC 27513, USA. All rights reserved. Printed in the
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.
Book code 59798, course code PROG2, prepared date 22Apr04.
For Your Information iii
Table of Contents
Course Description...................................................................................................................... vi
Prerequisites ...............................................................................................................................vii
General Conventions .................................................................................................................viii
Chapter 1 Introduction .......................................................................................... 1-1
1.1 Overview..........................................................................................................................1-3
1.2 Review of SAS Basics .....................................................................................................1-7
1.3 Review of DATA Step Processing..................................................................................1-15
1.4 Review of Displaying SAS Data Sets............................................................................1-21
1.5 Working with Existing SAS Data Sets...........................................................................1-28
1.6 Prerequisite Syntax (Self-Study)....................................................................................1-44
1.7 Navigating the SAS Windowing Environment (Self-Study) .........................................1-47
Chapter 2 Controlling Input and Output .............................................................. 2-1
2.1 Outputting Multiple Observations ...................................................................................2-3
2.2 Writing to Multiple SAS Data Sets................................................................................2-15
2.3 Selecting Variables and Observations ............................................................................2-23
2.4 Writing to an External File.............................................................................................2-39
2.5 Solutions to Exercises....................................................................................................2-58
Chapter 3 Summarizing Data ................................................................................ 3-1
3.1 Creating an Accumulating Total Variable ........................................................................3-3
3.2 Accumulating Totals for a Group of Data......................................................................3-12
3.3 Solutions to Exercises....................................................................................................3-30
iv For Your Information
Chapter 4 Reading and Writing Different Types of Data..................................... 4-1
4.1 Reading Delimited Raw Data Files..................................................................................4-3
4.2 Controlling When a Record Loads.................................................................................4-30
4.3 Reading Hierarchical Raw Data Files............................................................................4-56
4.4 Solutions to Exercises....................................................................................................4-90
Chapter 5 Data Transformations........................................................................... 5-1
5.1 Introduction......................................................................................................................5-3
5.2 Manipulating Character Values........................................................................................5-8
5.3 Manipulating Numeric Values .......................................................................................5-39
5.4 Manipulating Numeric Values Based on Dates..............................................................5-48
5.5 Converting Variable Type ..............................................................................................5-53
5.6 Solutions to Exercises....................................................................................................5-71
Chapter 6 Debugging Techniques (Self-Study)................................................... 6-1
6.1 Using the PUT Statement.................................................................................................6-3
6.2 Using the DEBUG Option .............................................................................................6-14
Chapter 7 Processing Data Iteratively.................................................................. 7-1
7.1 DO Loop Processing........................................................................................................7-3
7.2 SAS Array Processing....................................................................................................7-27
7.3 Using SAS Arrays..........................................................................................................7-38
7.4 Solutions to Exercises....................................................................................................7-53
Chapter 8 Combining SAS
®
Data Sets ................................................................. 8-1
8.1 Match-Merging Two or More SAS Data Sets..................................................................8-3
8.2 Simple Joins Using the SQL Procedure (Self-Study) ....................................................8-19
For Your Information v
8.3 Solutions to Exercises....................................................................................................8-31
Chapter 9 Learning More....................................................................................... 9-1
9.1 Where Do I Go From Here?.............................................................................................9-3
9.2 SAS Resources.................................................................................................................9-6
Appendix A Index ..................................................................................................... A-1