Statistical Programming in SAS  book cover
2nd Edition

Statistical Programming in SAS





ISBN 9780367357979
Published December 9, 2019 by Chapman & Hall
378 Pages 50 B/W Illustrations

FREE Standard Shipping
USD $84.95

Prices & shipping based on shipping country


Preview

Book Description

Statistical Programming in SAS Second Edition provides a foundation for programming to implement statistical solutions using SAS, a system that has been used to solve data analytic problems for more than 40 years. The author includes motivating examples to inspire readers to generate programming solutions. Upper-level undergraduates, beginning graduate students, and professionals involved in generating programming solutions for data-analytic problems will benefit from this book. The ideal background for a reader is some background in regression modeling and introductory experience with computer programming.

The coverage of statistical programming in the second edition includes

 Getting data into the SAS system, engineering new features, and formatting variables

 Writing readable and well-documented code

 Structuring, implementing, and debugging programs that are well documented

 Creating solutions to novel problems

 Combining data sources, extracting parts of data sets, and reshaping data sets as needed for other analyses

 Generating general solutions using macros

 Customizing output

 Producing insight-inspiring data visualizations

 Parsing, processing, and analyzing text

 Programming solutions using matrices and connecting to R

 Processing text

 Programming with matrices

 Connecting SAS with R

 Covering topics that are part of both base and certification exams.

Table of Contents

Contents

Preface ..............................................................................................................................................ix

Acknowledgments ...................................................................................................................... xiii

Author .............................................................................................................................................xv

1. Structuring, Implementing, and Debugging Programs to Learn about Data ...........1

1.1 Statistical Programming ................................................................................................1

1.2 Learning from Constructed, Artificial Data ...............................................................2

Processing a Particular Data Set—Extracting Variable Names from a

Column of an Input Data Set.........................................................................................2

Learning More about Unfamiliar Statistical Methods—Linear Mixed

Effects Models .................................................................................................................5

Improving Your Intuition about Statistical Theory— Sampling Distribution

of Means ...........................................................................................................................8

1.3 Good Programming Practice ...................................................................................... 11

Document Your Programs! .......................................................................................... 11

Use Meaningful Variable Names ................................................................................ 13

Use a Variety of CaSeS in Program Statements ........................................................ 14

Indent Program Statements That Naturally Go Together ....................................... 14

1.4 SAS Program Structure ................................................................................................ 15

1.5 What Is a SAS Data Set? ............................................................................................... 21

1.6 Internally Documenting SAS Programs ....................................................................22

1.7 Basic Debugging ...........................................................................................................23

1.8 Getting Help ..................................................................................................................27

Using Help in SAS ........................................................................................................27

Getting Help from a Web Browser Search .................................................................29

1.9 Exercises .........................................................................................................................29

2. Reading, Creating, and Formatting Data Sets ................................................................ 31

2.1 What Does a SAS DATA Step Do? .............................................................................. 31

2.2 Reading Data from External Files ..............................................................................33

Reading Data Directly as Part of a Program—Anyone for Datalines? .................34

Reading Data Sets Saved as Text—INFILE Can Be Your Friend (PROC

IMPORT Too!) ................................................................................................................38

Sometimes, Variables Are in Particular Columns or in Particular Formats .........40

2.3 Reading CSV, Excel, and TEXT Files .......................................................................... 41

2.4 Temporary versus Permanent Status of Data Sets ...................................................43

2.5 Formatting and Labeling Variables ............................................................................46

Using Formats to Read and Display Variable Values ..............................................46

Internal Representations and Output Displays ........................................................49

Character, Numeric, Time, and Date Formats ..........................................................53

2.6 User-Defined Formatting .............................................................................................58

Saving Formats for Later Use ......................................................................................63

2.7 Recoding and Transforming Variables in a DATA Step ........................................66

Indicator Variables ......................................................................................................68

2.8 Writing Out a File or Making a Simple Report ......................................................73

Simple Report Generation .........................................................................................73

Exporting a File ...........................................................................................................77

2.9 Exercises .......................................................................................................................80

3. Programming a DATA Step ................................................................................................83

3.1 Writing Programs by Subdividing Tasks ................................................................83

Estimate the Probability That a Randomly Selected 30- to 39-Year-Old

Male Is Taller than a Randomly Selected Female of the Same Age .....................83

Conditional Execution ...........................................................................................84

Looping to Repeat a Task ......................................................................................86

Returning to the Height Probability Simulation ............................................... 87

3.2 Ordering How Tasks Are Done ................................................................................90

Missing Data in Functions .........................................................................................92

3.3 Indexable Lists of Variables (Also Known as Arrays) ...........................................93

Defining Values in the Variable List .........................................................................93

Inputting Values in the Variable List ........................................................................94

Reassign Missing Value Codes for Numeric Variables “.” ...................................95

Recoding Missing Values for All Numeric and Character Variables ..................95

3.4 Functions Associated with Statistical Distributions .............................................96

3.5 Generating Variables Using Random Number Generators ................................ 102

3.6 Remembering Variable Values across Observations ........................................... 105

Processing Multiple Observations for a Single Observation .............................. 106

3.7 Case Study 1: Is the Two-Sample t-Test Robust to Violations of the

Heterogeneous Variance Assumption? ................................................................. 109

Case Study 1 (Revisited with DATA Step Programming) .................................. 118

3.8 Efficiency Considerations—How Long Does It Take? .........................................122

3.9 Case Study 2: Monte Carlo Integration to Estimate an Integral ........................ 123

3.10 Case Study 3: Simple Percentile-Based Bootstrap ................................................ 128

3.11 Case Study 4: Randomization Test for the Equality of Two Populations ......... 130

3.12 Exercises ..................................................................................................................... 134

4. Combining, Extracting, and Reshaping Data ............................................................... 137

4.1 Adding Observations by SET-ing Data Sets.......................................................... 137

4.2 Adding Variables by MERGE-ing Data Sets ......................................................... 140

4.3 Working with Tables in PROC SQL ....................................................................... 148

4.4 Converting Wide to Long Formats ......................................................................... 161

4.5 Converting Long to Wide Formats ......................................................................... 164

4.6 Case Study: Reshaping a World Bank Data Set .................................................... 166

4.7 Building Training and Validation Data Sets ......................................................... 175

4.8 Exercises ..................................................................................................................... 179

4.9 Self-study Lab ............................................................................................................ 180

5. Macro Programming .......................................................................................................... 191

5.1 What Is a Macro and Why Would You Use It? ..................................................... 191

5.2 Motivation for Macros: Numerical Integration to Determine

P(0 < Z < 1.645) ......................................................................................................... 191

5.3 Processing Macros .................................................................................................... 195

5.4 Macro Variables, Parameters, and Functions........................................................ 195

5.5 Conditional Execution, Looping, and Macros ...................................................... 198

More Complicated Macro Variable Construction ................................................203

Changing Locations in a Macro during Execution ..............................................204

5.6 Debugging Macro Code and Programs.................................................................206

Write Out Values of Macro Variables .....................................................................206

Useful SAS Options for Debugging Macros ......................................................... 207

5.7 Saving Macros ........................................................................................................... 211

5.8 Functions and Routines for Macros ....................................................................... 211

5.9 Case Study: Macro for Constructing Training and Test Data Set for Model

Comparison ............................................................................................................... 216

5.10 Case Study: Processing Multiple Data Sets ...........................................................223

5.11 Exercises .....................................................................................................................227

6. Customizing Output and Generating Data Visualizations .......................................229

6.1 Using the Output Delivery System ........................................................................229

Basic Ideas ..................................................................................................................229

Destinations—RTF, HTML, PDF, and More! .........................................................230

What’s Produced and How to Select It ..................................................................235

Another Destination That Stat Programmers Should Visit—OUTPUT ............ 243

6.2 Graphics in SAS ......................................................................................................... 249

6.3 ODS Statistical Graphics ..........................................................................................250

6.4 Modifying Graphics Using the ODS Graphics Editor ......................................... 257

6.5 Graphing with Styles and Templates .....................................................................260

6.6 Statistical Graphics—Entering the Land of SG Procedures ............................... 266

SGPLOT ...................................................................................................................... 266

SGPANEL ................................................................................................................... 269

SGSCATTER .............................................................................................................. 271

6.7 Case Study: Using the SG Procedures ................................................................... 273

6.8 Enhancing SG Displays—Options with SG Procedure Statements .................. 279

6.9 Using Annotate Data Sets to Enhance SG Displays ............................................284

6.10 Using Attribute Maps to Enhance SG Displays ................................................... 287

6.11 Exercises .....................................................................................................................290

7. Processing Text .................................................................................................................... 293

7.1 Cleaning and Processing Text Data ....................................................................... 293

7.2 Starting with Character Functions ......................................................................... 293

7.3 Processing Text .......................................................................................................... 298

7.4 Case Study: Sentiment in State of the Union Addresses .....................................302

7.5 Case Study: Reading Text from a Web Page .........................................................309

7.6 Regular Expressions ................................................................................................. 315

7.7 Case Study (Revisited)—Applying Regular Expressions ................................... 319

7.8 Exercises ..................................................................................................................... 321

8. Programming with Matrices and Vectors ..................................................................... 323

8.1 Defining a Matrix and Subscripting ...................................................................... 323

8.2 Using Diagonal Matrices and Stacking Matrices ................................................. 329

8.3 Using Elementwise Operations, Repeating, and Multiplying Matrices ........... 332

8.4 Importing a Data Set into SAS/IML and Exporting Matrices from

SAS/IML to a Data Set .............................................................................................333

Creating Matrices from SAS Data Sets and Vice Versa ........................................333

8.5 Case Study 1: Monte Carlo Integration to Estimate π ..........................................336

8.6 Case Study 2: Bisection Root Finder ...................................................................... 337

8.7 Case Study 3: Randomization Test Using Matrices Imported from PROC

PLAN ..........................................................................................................................340

8.8 Case Study 4: SAS/IML Module to Implement Monte Carlo Integration

to Estimate π ..............................................................................................................342

8.9 Storing and Loading SAS/IML Modules ..............................................................344

8.10 SAS/IML and R .........................................................................................................345

8.11 Exercises .....................................................................................................................350

References ...................................................................................................................................355

Index ............................................................................................................................................. 357

...
View More

Author(s)

Biography

A. John Bailer, PhD, PStat®, is a University Distinguished Professor and a founding chair of the Department of Statistics and an affiliate member of the Departments of Biology and Sociology and Gerontology as well as the Institute for the Environment and Sustainability at the Miami University in Oxford, Ohio. He is President of the International Statistical Institute (2019–2021). He previously served on the Board of Directors of the American Statistical Association. He is a Fellow of the American Statistical Association, the Society for Risk Analysis, and the American Association for the Advancement of Science. His research has focused on the quantitative risk estimation but has collaborations addressing problems in toxicology, environmental health, and occupational safety. He received the E. Phillips Knox Distinguished Teaching Award in 2018 after previously receiving the Distinguished Teaching Award for Excellence in Graduate Instruction and Mentoring and the College of Arts and Science Distinguished Teaching Award. He is also the co-founder and continuing panelist on the Stats+Stories podcast (www.statsandstories.net).

Reviews

"This book is useful for people who want to learn SAS programing, and assumes the students have knowledge of multiple linear regression and one-way ANOVA models.…The second edition has added a chapter on text processing, and reorganized the chapter order…Some topics that are relevant for the SAS Base and Certifications exams are covered, and a nice feature is the highlighting of programing tips in gray."
~Technometrics

"This is a very complete book for programming SAS in statistical analyses. This second edition offers the possibility to debug some programs and provides new examples and applications, which are very useful. This book is a very useful companion tool for students or beginners in SAS, or for more experienced statisticians who already use SAS for statistical analyses."
~ISCB News