The book focuses on good programming and coding practice for producing data sets for later analyses, using simulation studies to explore statistical concepts and employing computationally intensive methods. Many of the topics included in the SAS Base Certification exam are included in this book.
Inhaltsverzeichnis
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 <