qxd 10/12/2004 5:27 PM Page i FM-Boslaugh.qxd 10/12/2004 12:08 PM Page ii FM-Boslaugh.qxd 10/12/2004 12:08 PM Page iv Copyright © 2005 by Sage Publications, Inc. All rights reserved. No part of this book may be reproduced or utilized in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage and retrieval system, without permission in writing from the publisher. For information: Sage Publications, Inc.
2455 Teller Road Thousand Oaks, California 91320 E-mail: order@sagepub.com Sage Publications Ltd. 1 Oliver’s Yard 55 City Road London EC1Y 1SP United Kingdom Sage Publications India Pvt. B-42, Panchsheel Enclave Post Box 4109 New Delhi 110 017 India Printed in the United States of America Library of Congress Cataloging-in-Publication Data Boslaugh, Sarah. An intermediate guide to SPSS programming: Using syntax for data management / Sarah Boslaugh.
Includes bibliographical references and index. SPSS for Windows. Social sciences—Statistical methods—Computer programs.5′5—dc22 2004014097 04 05 06 07 10 9 8 7 6 5 4 3 2 1 Acquisitions Editor: Lisa Cuevas Shaw Editorial Assistant: Margo Beth Crouppen Production Editor: Melanie Birdsall Copy Editor: Carla Freeman Typesetter: C&M Digitals (P) Ltd. Proofreader: Teresa Herlinger Cover Designer: Michelle Kenny FM-Boslaugh.qxd 10/12/2004 12:08 PM Page v Contents Preface xi Part I: An Introduction to SPSS 1.
What Is SPSS? 3 A Brief History of SPSS 3 SPSS as a High-Level Programming Language 3 SPSS as a Statistical Analysis Package 4 2. Interacting With SPSS 5 The SPSS Session 5 SPSS Windows 6 Basics About SPSS Commands 6 Order of Execution of SPSS Commands 7 Batch Mode and Interactive Mode 8 3. Types of Files in SPSS 9 The Command or Syntax Files 9 The Active or Working Data File 10 The Output Files 10 The Journal Files 12 4. Customizing the SPSS Environment 13 Displaying Current Settings 13 Changing Current Settings 14 Eliminating Page Breaks 14 Increasing Memory Allocation 15 Changing the Default Format for Numeric Variables 15 FM-Boslaugh.qxd 10/12/2004 12:08 PM Page vi Part II: An Introduction to Computer Programming With SPSS 5.
An Introduction to Computer Programming 19 Using Syntax Versus the Menu System 19 The Process of Writing and Testing Syntax 20 Typographical Conventions Used in This Book 21 How Code and Output Are Presented in This Book 21 Some Reasons to Use Syntax 22 Beginning to Learn Syntax 23 Programming Style 25 6. Programming Errors 27 Syntax Errors and Logical Errors 28 The Debugging Process 28 Common SPSS Syntax Errors 28 Finding Logical Errors 30 Changing Default Error and Warning Settings 31 Deciphering SPSS Error and Warning Messages 31 7. Documenting Syntax, Data, and Output Files 33 Using Comments in SPSS Programs 33 Using Comments to Prevent Code From Executing 34 Documenting a Data File 34 Echoing Text in the Output File 35 Using Titles and Subtitles 36 Part III: Reading and Writing Data Files in SPSS 8. Reading Raw Data in SPSS 39 Reading Inline Data 40 Reading External Data 41 The FIXED, FREE, and LIST Formats 42 Specifying the Delimiter Symbol 46 Reading Aggregated Data With DATA LIST 47 Reading Data With Multiple Records Per Case 48 Using FORTRAN-Like Variable Specifications 49 Two Shortcuts for Declaring Variables With Identical Formats 50 Specifying Decimal Values in Data 52 FM-Boslaugh.qxd 10/12/2004 12:08 PM Page vii 9.
Reading SPSS System and Portable Files 55 Reading an SPSS System File 55 Reading an SPSS Portable File 56 Dropping, Reordering, and Renaming Variables 56 10. Reading Data Files Created by Other Programs 59 Reading Microsoft Excel Files 59 Reading Data From Earlier Versions of Excel 60 Reading Data From Later Versions of Excel 61 Using GET TRANSLATE to Read Other Types of Files 62 Reading Data From Database Programs 62 Reading SAS Data Files 62 11. Reading Complex Data Files 65 Reading Mixed Data Files 65 Reading Grouped Data Files 67 Reading Nested Data Files 68 Reading Data in Matrix Format 69 12. Saving Data Files 75 Saving an SPSS System File 75 Saving an SPSS Portable Data File 76 Saving a Data File for Use by Other Programs 76 Saving Text Files 77 Part IV: File Manipulation and Management in SPSS 13.
Inspecting a Data File 81 Determining the Number of Cases in a File 82 Determining What Variables Are in a File 82 Getting More Information About the Variables 83 Checking for Duplicate Cases 84 Looking at Variable Values and Distributions 86 Creating Standardized Scores 88 14. Combining Data Files 91 Adding New Variables to Existing Cases 91 Adding Summary Data to an Individual-Level File 94 Combining Cases From Several Files 95 Updating Values in a File 97 FM-Boslaugh.qxd 10/12/2004 12:08 PM Page viii 15. Data File Management 99 Reordering and Dropping Variables in the Active File 99 Eliminating Duplicate Records 100 Sorting a Data Set 102 Splitting a Data Set 103 Selecting Cases 103 Filtering Cases 104 Weighting Cases 105 16. Restructuring Files 107 The Unit of Analysis 107 Changing File Structure From Univariate to Multivariate 108 Incorporating a Test Condition When Restructuring a Data File 112 Changing File Structure From Multivariate to Univariate 115 Transposing the Rows and Columns of a Data Set 116 17.
Missing Data in SPSS 119 Types of Missing Data 120 System-Missing and User-Missing Data 120 Looking at Missing Data on Individual Variables 122 Looking at the Pattern of User-Missing Data Among Pairs of Variables 123 Looking at the Pattern of Missing Data Across Many Variables 124 Changing the Value of Blanks in Numeric Fields 126 Treatment of Missing Values in SPSS Commands 127 Substituting Values for Missing Data 128 18. Using Random Processes in SPSS 133 The Random-Number Seed 133 Generating Random Distributions 134 Random Selection of Cases 134 Random Group Assignment 136 Random Selection From Multiple Groups 136 FM-Boslaugh.qxd 10/12/2004 12:08 PM Page ix Part V: Variables and Variable Manipulations 19. Variables and Variable Formats 139 String and Numeric Variables 139 System Variables 141 Scratch Variables 141 Input and Output Formats 141 The NUMBER Format 143 The COMMA, DOT, DOLLAR, and PCT Formats 144 20. Variable and Value Labels 147 Rules About Variable Names in SPSS 147 Systems for Naming Variables 148 Adding Variable Labels 149 Adding Value Labels 149 Controlling Whether Labels Are Displayed in Tables 150 Applying the Data Dictionary From a Previous Data Set 151 21.
Recoding and Creating Variables 153 The IF Statement 154 Relational Operators 154 Logical Variables 156 Logical Operators 158 Creating Dummy Variables 160 The RECODE and AUTORECODE Commands 161 Converting Variables From Numeric to String or String to Numeric 164 Counting Occurrences of Values Across Variables 166 Counting the Occurrence of Multiple Values in One Variable 167 Creating a Cumulative Variable 168 22. Numeric Operations and Functions 171 Arithmetic Operations 171 Mathematical and Statistical Functions 173 Missing Values in Numeric Operations and Functions 175 Domain Errors 176 A Substring-Like Technique for Numeric Variables 177 23. String Functions 179 The Substring Function 179 Concatenation 180 FM-Boslaugh.qxd 10/12/2004 12:08 PM Page x Searching for Characters Within a String Variable 182 Adding or Removing Leading or Trailing Characters 183 Finding Character Strings Identified by Delimiters 186 24. Date and Time Variables 189 How Date and Time Variables Are Stored in SPSS 189 An Overview of SPSS Date Formats 190 Reading Dates With Two-Digit Years in the Correct Century 192 Creating Date Variables With Syntax 193 Creating Date Variables From String Variables 193 Extracting Part of a Date Variable 195 Doing Arithmetic With Date Variables 196 Creating a Variable Holding Today’s Date 198 Designating Missing Values for Date Variables 199 Part VI: Other Topics 25.
Automating Tasks Within Your Program 203 Vectors 203 The DO IF Command Structure 205 The DO REPEAT Command Structure 206 The LOOP Command Structure 208 26. A Brief Introduction to the SPSS Macro Language 213 The Parts of a Macro 214 Macros Without Arguments 215 Macros With Arguments 215 Specifying Arguments by Position 217 Macros Using a Flexible Number of Variables 217 Controlling the Macro Language Environment 220 Sources of Further Information About SPSS Macros 221 27. Resources for Learning More About SPSS Syntax 223 Books 223 Web Pages 224 Mailing Lists 225 References 227 Index 229 About the Author 233 FM-Boslaugh.qxd 10/12/2004 12:08 PM Page xi Preface T his book is about using SPSS to manage data. To be more specific, it presents a number of concepts important in data management and demonstrates how to carry out data management tasks using SPSS syntax.
It presupposes no experience with data management, SPSS, or computer pro- gramming, but assumes the reader has the need or the desire to learn about those topics. It further assumes the reader has access to SPSS and to the SPSS Syntax Reference Guide, which is included as a PDF file with the SPSS software. Data management includes everything necessary to prepare data for analysis, including 1. Getting the data into the computer program you will use to analyze it 2.
Screening data for duplicate records, data errors, missing data, and so on 3. Combining and restructuring data files 4. Creating and recoding variables 5. Documenting the procedures performed on the data People who work with data recognize that they often spend more time on data management tasks than they do performing analyses.
Data manage- ment is often neglected in courses that introduce students to data analysis, leaving them unprepared to deal with data management issues when they begin working with real data. This book fills that gap by discussing common issues in data management and presenting techniques to deal with them. These tasks are accomplished using SPSS syntax, but the general principles can be applied using any programming language. This book is also a basic introduction to SPSS and to SPSS syntax.
This aspect will appeal particularly to two groups of people: those who currently use SPSS through the menu system only and those working in other pro- gramming languages who want to learn SPSS. Many important features of SPSS syntax are demonstrated throughout this book, and basic program- ming concepts such as vectors and loops are also introduced as means to accomplish data management tasks. xi FM-Boslaugh.qxd 10/12/2004 12:08 PM Page xii 01-Boslaugh.qxd 10/12/2004 12:09 PM Page 1 Part I An Introduction to SPSS 01-Boslaugh.qxd 10/12/2004 12:09 PM Page 2 01-Boslaugh.qxd 10/12/2004 12:09 PM Page 3 CHAPTER 1 What Is SPSS? A BRIEF HISTORY OF SPSS SPSS is a statistical analysis package produced and sold by the multinational company SPSS Inc. SPSS was developed in the late 1960s by Norman H.
Hadlai Hull, and Dale H. Their purpose was to develop “a software system based on the idea of using statistics to turn raw data into information essential to decision-making” (SPSS Inc., About SPSS, para. Originally, the initials “SPSS” stood for “Statistical Package for the Social Sciences,” but since the market for SPSS is much broader today, SPSS is now simply the name used for the product and company and not an acronym. Because SPSS consists of a large collection of syntax written by different people at different times, terminology is not always consistent between procedures.
Also, because new procedures have been added while older procedures have been retained, there are often multiple ways to achieve the same result. Neither situation is unique to SPSS, but they may be confusing to the beginning programmer. Neither, however, should present serious obstacles to learning SPSS syntax. SPSS AS A HIGH-LEVEL PROGRAMMING LANGUAGE All programming languages serve as an interface between the computer and the human being who wishes to use the computer to do something.
Computer programmers typically speak of four levels or generations of com- puter languages, classified by distance between the syntax written by the programmer and the instructions executed by the computer. The first level is machine code, which is very close to the instructions executed by the 3 01-Boslaugh.qxd 10/12/2004 12:09 PM Page 4 4 An Introduction to SPSS computer, and very difficult for humans to learn. Assembly language is the second level, and general-purpose languages such as C are the third level.