Programming and Data Management for SPSS 16.0 a Guide for SPSS and SAS Users

  • View
    1.627

  • Download
    9

Embed Size (px)

Text of Programming and Data Management for SPSS 16.0 a Guide for SPSS and SAS Users

Programming and Data Management for SPSS 16.0A Guide for SPSS and SAS UsersRaynald Levesque and SPSS Inc.

For more information about SPSS software products, please visit our Web site at http://www.spss.com or contact: SPSS Inc. 233 South Wacker Drive, 11th Floor Chicago, IL 60606-6412 Tel: (312) 651-3000 Fax: (312) 651-3668 SPSS is a registered trademark and the other product names are the trademarks of SPSS Inc. for its proprietary computer software. No material describing such software may be produced or distributed without the written permission of the owners of the trademark and license rights in the software and the copyrights in the published materials. The SOFTWARE and documentation are provided with RESTRICTED RIGHTS. Use, duplication, or disclosure by the Government is subject to restrictions as set forth in subdivision (c) (1) (ii) of The Rights in Technical Data and Computer Software clause at 52.227-7013. Contractor/manufacturer is SPSS Inc., 233 South Wacker Drive, 11th Floor, Chicago, IL 60606-6412. Patent No. 7,023,453 General notice: Other product names mentioned herein are used for identication purposes only and may be trademarks of their respective companies. SAS is a registered trademark of SAS Institute Inc. Python is a registered trademark of the Python Software Foundation. Microsoft, Visual Basic, Visual Studio, Ofce, Access, Excel, Word, PowerPoint, and Windows are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. Apple, Mac, and the Mac logo are trademarks of Apple Computer Inc., registered in the U.S. and other countries. This product uses WinWrap Basic, Copyright 19932007, Polar Engineering and Consulting, http://www.winwrap.com. Programming and Data Management for SPSS 16.0: A Guide for SPSS and SAS Users Copyright 2008 by SPSS Inc. 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 meanselectronic, mechanical, photocopying, recording, or otherwisewithout the prior written permission of the publisher. 1234567890 11 10 09 08

ISBN-13: 978-1-56827-399-0 ISBN-10: 1-56827-399-1

Preface

Experienced data analysts know that a successful analysis or meaningful report often requires more work in acquiring, merging, and transforming data than in specifying the analysis or report itself. SPSS contains powerful tools for accomplishing and automating these tasks. While much of this capability is available through the graphical user interface, many of the most powerful features are available only through command syntaxand you can make the programming features of its command syntax signicantly more powerful by adding the ability to combine it with a full-featured programming language. This book offers many examples of the kinds of things that you can accomplish using SPSS command syntax by itself and in combination with the Python programming language.

For SAS UsersIf you have more experience with SAS than with SPSS for data management, see Chapter 31 for comparisons of the different approaches to handling various types of data management tasks. Quite often, there is not a simple command-for-command relationship between the two programs, although each accomplishes the desired end.

AcknowledgmentsThis book reects the work of many members of the SPSS staff who have contributed examples here and in SPSS Developer Central, as well as that of Raynald Levesque, whose examples formed the backbone of earlier editions and remain important in this edition. We also wish to thank Stephanie Schaller, who provided many sample SAS jobs and helped to dene what the SAS user would want to see, as well as Marsha Hollar and Brian Teasley, the authors of the original chapter SPSS for SAS Programmers.

iii

A Note from Raynald LevesqueIt has been a pleasure to be associated with this project from its inception. I have for many years tried to help SPSS users understand and exploit its full potential. In this context, I am thrilled about the opportunities afforded by the Python integration and invite everyone to visit my site at www.spsstools.net for additional examples. And I want to express my gratitude to my spouse, Nicole Tousignant, for her continued support and understanding.Raynald Levesque

iv

Contents1 Overview 1

Using This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Documentation Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Part I: Data Management 2 Best Practices and Efficiency TipsCreating Command Syntax Files . . . . . . . . Running SPSS Commands . . . . . . . . . . . . Syntax Rules . . . . . . . . . . . . . . . . . . . . . . Customizing the Programming Environment . . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...

55 6 7 8

Working with Command Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

Displaying Commands in the Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Displaying the Status Bar in Command Syntax Windows . . . . . . . . . . . . . 9 Protecting the Original Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Do Not Overwrite Original Variables. . Using Temporary Transformations . . . Using Temporary Variables . . . . . . . . Use EXECUTE Sparingly . . . . . . . . . . . . . . Lag Functions . . . . . . . . . . . . . . . . . . Using $CASENUM to Select Cases. . . MISSING VALUES Command . . . . . . . WRITE and XSAVE Commands . . . . . . Using Comments. . . . . . . . . . . . . . . . . . . . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... .. .. .. .. .. .. .. .. .. 11 11 12 14 14 16 17 17 17

Using SET SEED to Reproduce Random Samples or Values . . . . . . . . . . . . . . 18

v

Divide and Conquer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Using INSERT with a Master Command Syntax File . . . . . . . . . . . . . . . . 20 Defining Global Settings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

3

Getting Data into SPSSInstalling Database Drivers . . . . . Database Wizard . . . . . . . . . . . . . Reading a Single Database Table . Reading Multiple Tables. . . . . . . . Reading Excel Files. . . . . . . . . . . . . . . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...

23.. .. .. .. .. 23 25 25 27 30

Getting Data from Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

Reading a Typical Worksheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Reading Multiple Worksheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Reading Text Data Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Simple Text Data Files . . . . . . . . . . . . . . . Delimited Text Data . . . . . . . . . . . . . . . . . Fixed-Width Text Data . . . . . . . . . . . . . . . Text Data Files with Very Wide Records . . Reading Different Types of Text Data . . . . Reading Complex Text Data Files. . . . . . . . . . . Mixed Files . . . . . . . . . . . . . . Grouped Files . . . . . . . . . . . . Nested (Hierarchical) Files . . Repeating Data . . . . . . . . . . . Reading SAS Data Files . . . . . . . . ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... .. .. .. .. .. .. .. .. .. .. .. 37 38 42 46 47 49 49 50 53 59 60

Reading Stata Data Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Code Page and Unicode Data Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

vi

4

File Operations

66

Using Multiple Data Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Merging Data Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Merging Files with the Same Cases but Different Variables . . . . . . . . Merging Files with the Same Variables but Different Cases . . . . . . . . Updating Data Files by Merging New Values from Transaction Files . . Aggregating Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. .. .. .. 70 74 78 80

Aggregate Summary Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Weighting Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Changing File Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Transposing Cases and Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Cases to Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Variables to Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90

5

Variable and File PropertiesVariable Labels . . . . . . . . . . . . . . . . . . . . Value Labels . . . . . . . . . . . . .