The Data Detective's Toolkit: Cutting-Edge Techniques and SAS Macros to Clean, Prepare, and Manage Data
By Kim Chantala
()
About this ebook
Have you ever wished there was an easy way to reduce your workload and improve the quality of your data? The Data Detective’s Toolkit: Cutting-Edge Techniques and SAS Macros to Clean, Prepare, and Manage Data will help you automate many of the labor-intensive tasks needed to turn raw data into high-quality, analysis-ready data. You will find the right tools and techniques in this book to reduce the amount of time needed to clean, edit, validate, and document your data. These tools include SAS macros as well as ingenious ways of using SAS procedures and functions.
The innovative logic built into the book’s macro programs enables you to monitor the quality of your data using information from the formats and labels created for the variables in your data set. The book explains how to harmonize data sets that need to be combined and automate data cleaning tasks to detect errors in data including out-of-range values, inconsistent flow through skip paths, missing data, no variation in values for a variable, and duplicates. By the end of this book, you will be able to automatically produce codebooks, crosswalks, and data catalogs.
Kim Chantala
Kim Chantala is a Programmer Analyst in the Research Computing Division at RTI International with over 25 years of experience in managing and analyzing research data. Before joining RTI International, she was a data analyst at the University of North Carolina at Chapel Hill. In addition to providing data management and analytical services at the University, she taught workshops on analyzing survey data, focusing on the problems of sample weights and design effects. Kim believes that the real challenge in data analysis is bridging the gap between raw or acquired data and data that is ready to analyze. This inspired her to develop computerized data management tools revolutionizing the way data is prepared, allowing users to improve the quality of their data while lowering the cost of data preparation. Kim earned a BS in Engineering Physics from the Colorado School of Mines and an MS in Biometrics from the University of Colorado.
Related to The Data Detective's Toolkit
Related ebooks
SAS Visual Analytics for SAS Viya Rating: 0 out of 5 stars0 ratingsSmart Data Discovery Using SAS Viya: Powerful Techniques for Deeper Insights Rating: 0 out of 5 stars0 ratingsPractical and Efficient SAS Programming: The Insider's Guide Rating: 0 out of 5 stars0 ratingsBiostatistics by Example Using SAS Studio Rating: 0 out of 5 stars0 ratingsAdvanced SQL with SAS Rating: 0 out of 5 stars0 ratingsPROC SQL: Beyond the Basics Using SAS, Third Edition Rating: 0 out of 5 stars0 ratingsThe SAS Programmer's PROC REPORT Handbook: ODS Companion Rating: 0 out of 5 stars0 ratingsSegmentation Analytics with SAS Viya: An Approach to Clustering and Visualization Rating: 0 out of 5 stars0 ratingsEnd-to-End Data Science with SAS: A Hands-On Programming Guide Rating: 0 out of 5 stars0 ratingsCody's Data Cleaning Techniques Using SAS, Third Edition Rating: 5 out of 5 stars5/5Applied Data Mining for Forecasting Using SAS Rating: 0 out of 5 stars0 ratingsSAS Programming with Medicare Administrative Data Rating: 5 out of 5 stars5/5Exercises and Projects for The Little SAS Book, Sixth Edition Rating: 0 out of 5 stars0 ratingsInsightful Data Visualization with SAS Viya Rating: 0 out of 5 stars0 ratingsAdvanced Analytics with Transact-SQL: Exploring Hidden Patterns and Rules in Your Data Rating: 0 out of 5 stars0 ratingsPredictive Modeling with SAS Enterprise Miner: Practical Solutions for Business Applications, Third Edition Rating: 0 out of 5 stars0 ratingsBusiness Analytics Using SAS Enterprise Guide and SAS Enterprise Miner: A Beginner's Guide Rating: 0 out of 5 stars0 ratingsSAS Certified Professional Prep Guide: Advanced Programming Using SAS 9.4 Rating: 1 out of 5 stars1/5Categorical Data Analysis Using SAS, Third Edition Rating: 0 out of 5 stars0 ratingsSAS Statistics by Example Rating: 5 out of 5 stars5/5Getting Started with SAS Programming: Using SAS Studio in the Cloud Rating: 0 out of 5 stars0 ratingsFundamentals of Programming in SAS: A Case Studies Approach Rating: 0 out of 5 stars0 ratingsMachine Learning with SAS Viya Rating: 0 out of 5 stars0 ratingsThe SAS Programmer's PROC REPORT Handbook: Basic to Advanced Reporting Techniques Rating: 0 out of 5 stars0 ratingsThe Little SAS Book: A Primer, Sixth Edition Rating: 5 out of 5 stars5/5Learning SAS by Example: A Programmer's Guide, Second Edition Rating: 3 out of 5 stars3/5Text Mining and Analysis: Practical Methods, Examples, and Case Studies Using SAS Rating: 0 out of 5 stars0 ratingsDatabase Management for Business Leaders: Building and Using Data Solutions That Work for You Rating: 0 out of 5 stars0 ratings
Applications & Software For You
Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Adobe Illustrator: A Complete Course and Compendium of Features Rating: 0 out of 5 stars0 ratingsSound Design for Filmmakers: Film School Sound Rating: 5 out of 5 stars5/5How to Create Cpn Numbers the Right way: A Step by Step Guide to Creating cpn Numbers Legally Rating: 4 out of 5 stars4/5Logic Pro X For Dummies Rating: 0 out of 5 stars0 ratingsExcel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5GarageBand For Dummies Rating: 5 out of 5 stars5/5The Best Hacking Tricks for Beginners Rating: 4 out of 5 stars4/5Six Figure Blogging In 3 Months Rating: 4 out of 5 stars4/5Canon EOS Rebel T3/1100D For Dummies Rating: 5 out of 5 stars5/5Canon EOS Rebel T7/2000D For Dummies Rating: 0 out of 5 stars0 ratingsGarageBand Basics: The Complete Guide to GarageBand: Music Rating: 0 out of 5 stars0 ratingsData Science and Big Data Analytics: Discovering, Analyzing, Visualizing and Presenting Data Rating: 0 out of 5 stars0 ratingsBlender 3D Basics Beginner's Guide Second Edition Rating: 5 out of 5 stars5/5OneNote Recipes: Elegant Techniques for Problem Solving and Visual Thinking Rating: 5 out of 5 stars5/5Tableau Your Data!: Fast and Easy Visual Analysis with Tableau Software Rating: 4 out of 5 stars4/5Synthesizer Cookbook: How to Use Filters: Sound Design for Beginners, #2 Rating: 3 out of 5 stars3/5The Most Concise Step-By-Step Guide To ChatGPT Ever Rating: 3 out of 5 stars3/5Blender 3D By Example Rating: 4 out of 5 stars4/5Adobe Premiere Pro: A Complete Course and Compendium of Features Rating: 0 out of 5 stars0 ratings80 Ways to Use ChatGPT in the Classroom Rating: 5 out of 5 stars5/5Digital Filmmaking for Beginners A Practical Guide to Video Production Rating: 0 out of 5 stars0 ratingsSmartphone Photography Rating: 0 out of 5 stars0 ratingsMastering ChatGPT Rating: 0 out of 5 stars0 ratingsThe Little SAS Book: A Primer, Sixth Edition Rating: 5 out of 5 stars5/5
Reviews for The Data Detective's Toolkit
0 ratings0 reviews
Book preview
The Data Detective's Toolkit - Kim Chantala
Chapter 1: Advantages of Using the Data Detective’s Toolkit
Introduction
You will find the right data tools in this book for creating project data that is ready for exploration and analysis. Using these tools will reduce the amount of time needed to clean, edit, validate, and document your data. Advantages of using the techniques in this book include:
● Accomplishing more while doing less by automating and modernizing the typical data preparation activities
● Beginning at the end by creating research-ready data sets and documentation early in the project with continual updates and improvements throughout collection and preparation
● Keeping the sponsor or lead research investigators engaged by providing codebooks, crosswalks, and data catalogs for review early in the project, thus including them as part of quality control surveillance for the data
This book includes a set of SAS macro programs that automate many of the labor-intensive tasks that you perform during data preparation. Using these macro programs will help guard against compromising quality control and documentation efforts due to rigid project budgets and timelines. You will be able to automate producing codebooks, crosswalks, and data catalogs. Innovative logic built into these macro programs computerizes monitoring the quality of your data using information from the formats and labels created for the variables in your data set. You will receive concise reports identifying invalid data – such as out of range values, missing data, redundant, or contradictory data.
You only need to create a SAS data set with labels and formats assigned to each variable to use these macro programs. It could not be easier or faster to create data that you can trust. The SAS macro programs accompanying this book are available at no charge and can be downloaded from the author page for this book at support.sas.com/chantala.
In the following chapters, you will learn how to use these macro programs to make your job easier and create higher quality data. This chapter introduces you to the macro programs accompanying this book and highlights how they can help solve many of the problems that you face in data preparation.
An Overview of the Data Detective’s Toolkit
Data preparation is a heroic task, often with inconsistencies and anomalies in raw data that you must resolve to make the data usable. Your job will include:
● Investigating unexpected or missing values
● Resolving conflicting information across variables
● Mitigating incorrect flow through skip patterns
● Examining incomplete data
● Combining multiple data sets with different attributes
● Documenting changes in data collection methods or instruments during collection
Reconciling these issues requires careful investigation and alleviation during data cleaning and preparation. Rapid advancement in software for both data collection and analysis has encouraged more complex data to be collected. This has caused greater challenges for you as the programmer responsible for turning it into high-quality, research-friendly data. Advances in software to help you solve these issues has progressed at a slower pace than advances in software for analysis or collecting data. This lag in development of computerized tools for data preparation has motivated the development of the macro programs included with this book.
These macro programs have been developed to help you work more efficiently when preparing data and automate much of the tedious work in identifying and correcting problems in your data. Table 1-1 lists the macro programs provided with this book and what they will do for you.
Table 1-1: List of Macro Programs in the Data Detective’s Toolkit
The only requirement for using these data tools is creating SAS data sets with formats and labels assigned to each variable. Once you have the SAS data set created, you will only need a simple line of SAS code to invoke each of the data tools. The first three macro programs create useful documentation for your data sets. You can create them at the beginning of the project and benefit by having them available for everyone in your team.
%TK_codebook
The first tool, %TK_codebook, creates a codebook. This macro uses one statement requiring only that you provide the name and location of your SAS data set, the library for the formats assigned to the variables, and a name for your codebook as shown below:
%TK_codebook(lib=work,
file1=test,
fmtlib=library,
cb_type=XLSX,
cb_file=&WorkFolder./Test_CodeBook.xlsx ,
var_order=internal,
cb_output = my_codebook,
cb_size=BRIEF,
organization = One record per CASEID,
include_warn=YES);
It could not be easier to create a codebook for your data set. But the best feature is yet to come! %TK_codebook will also examine each variable and print informative reports about potential
problems. Using information from the label and format assigned to each variable, the %TK_codebook macro warns your data team about variables having the following problems:
● Values missing from the assigned format
● Out of range values
● Missing labels
● No assigned format
● Having 100% missing values
● No variation in the response value
For each variable automatically examined, you would have to write several SAS statements and examine multiple tables to figure out which variables need further examination. If your data set has 1000 variables, you will write SAS statements to create over 2000 tables, examine each table manually to identify problems, then summarize the problems that need investigation. With the reports from %TK_codebook, you are presented with a concise summary of only those variables needing close examination and why they need examination. You will spend your time correcting problems rather than writing repetitive SAS code and examining piles of SAS output. Chapter 3 teaches you how to use %TK_codebook to create a codebook and potential problem reports. These reports identify variables having the problems listed earlier in this section. Chapter 4 teaches you how to customize your codebook in both appearance and adding additional information about variables to the data used to create a codebook.
%TK_inventory
A catalog of all the SAS data sets for your project can be created at any time during the data life cycle with %TK_inventory by simply providing the full path name of the folder where the data sets reside:
libname SAS_data /Data_Detective/Book/SAS_Datasets
;
%TK_inventory(libref=SAS_data);
For each data set in the folder associated with libref SAS_data, %TK_inventory will provide information about the following characteristics:
● Data set name
● Data set label
● Creation date
● Number of observations
● Number of variables
This catalog provides a concise summary of the data sets and where they are located, providing an ideal document for communicating a listing of available data. It makes it easier for you and your team to track the progression of developing your data sets. Chapter 5 teaches you how to use the %TK_inventory macro tool.
%TK_xwalk
The %TK_xwalk tool creates a data crosswalk to help you identify equivalent variables in multiple data sets as well as differences in the attributes of variables having the same name in more than one data set. Again, you only need to use one short statement with a list of data files for %TK_xwalk to create your crosswalk.
%TK_xwalk(SetList = SAS_Data.studya SAS_Data.demog SAS_Data.health);
This statement creates a mapping of variables across two or more distinct data sets. Reviewing the crosswalk will help you identify variables used to merge the data as well as avoid truncating values when merging or concatenating data sets. You will learn to use %TK_xwalk in Chapter 5.
%TK_find_dups
You will need to examine each data set verifying that variables uniquely identifying an observation occur only on one observation. You will need to do this on every data set that is created, possibly each time changes are made to program creating your data set. With just a few strokes of the keyboard %TK_find_dups will easily do this for you:
%TK_find_dups(dataset=work.STUDY, one_rec_per=CASEID*WAVE,
up_output=STUDY_DUPS);
The output from %TK_find_dups includes the following:
● Table showing the number of observations having identical values of the unique identification variables (CASEID*WAVE)
● Table showing the values of the identification variables that are duplicated across observations.
● Output data set with values of duplicated identification variables that you can use to extract the duplicated observations from your data set.
Chapter 6 teaches you how to use %TK_find_dups.
%TK_harmony
The %TK_harmony macro can identify possible problems with merging or concatenating two data sets. It is very simple to use, requiring only one statement providing the names of the data sets being harmonized, and nicknames for each data set used in the harmony report created by the %TK_harmony.
%TK_harmony(set1= SAS_data.demography_a1,
set1_id=Web,
set2= SAS_data.demography_a2,
set2_id=Paper,
out=harmony_results);
%TK_harmony compares the two data sets and creates a report with the following information:
● Variables unique to each set
● Variables with the same name having different labels
● Variables with the same name having different data types or lengths
You will learn to use the %TK_harmony macro and the output tables in Chapter 6.
%TK_skip_edit
Skip patterns are used in data collection to ensure that only relevant questions are asked each person participating in the survey. For example, your study might have a set of questions that are asked only of female participants. Male participants would have missing values for all of these questions.
The %TK_skip_edit macro can be used to validate skip patterns as follows:
● Validate that a variable follows the expected pattern of nonmissing/missing values when the variable is part of the skip pattern logic
● Handle special recoding to correct inconsistencies in skip patterns and help users understand why a variable is missing
For example, suppose question PG1 asks women the number of pregnancies they have had in their lifetime. This would not be asked if the participant was male. Question DEM2 in the survey asks each participant their sex (1=female, 2=male). %TK_skip_edit uses this information to examine this skip pattern for you and change the value of PG1 to missing if a male responded to that question. You only need to set up a format identifying the values of a variable that cause a SKIP, and then pass this information to TK_skip_edit:
proc format;
value SKIP2f 2=’2=SKIP’;
run;
%TK_skip_edit(check_var = PG1,
skip_vars = DEM2,
skip_fmts = DEM2 skip2f.);
%TK_skip_edit produces an annotated table reporting results from analyzing data flow through the skip pattern and any edits that were made to the data to resolve inconsistencies in the data flow. You will learn more about skip patterns and how to use the %TK_skip_edit macro in Chapter 7.
%TK_max_length
SAS prints the following message in your log file to warn you that there is a mismatch in the storage length of variables in the data sets being combined in a DATA step:
WARNING: Multiple lengths were specified for the variable VAR_NAME by input data set(s). This can cause truncation of data.
When you see this message, it means that the values stored in VAR_NAME were possibly truncated when the data sets were combined with a MERGE or SET statement. To prevent this from happening, you can use the %TK_max_length macro to create a macro variable named &MAX_LENGTHS that contains information about the variables common to two data sets but have different storage lengths. This list includes the name and the longest defined length of each variable. Macro variable &MAX_LENGTHS can be used in the LENGTH statement in the DATA step to prevent truncation of data values when two data sets are combined. The SAS statements below show how easy it is to use %TK_max_length and a LENGTH statement to prevent truncating data values:
%TK_max_length(set1=My_Data.teleform_data, set2=My_data.web_data);
data survey_v2;
length &max_lengths;
set My_Data.teleform_data My_Data.web_data;
run;
You will learn more about using the %TK_max_length in Chapter 2.
Summary
This chapter explained the benefits of using this book for data cleaning, preparation, and management. Using these macro programs reduces the time needed to prepare data that you can trust. You will automate creating documentation for your data by easily creating codebooks, crosswalks, and data catalogs with just a few strokes on the keyboard. The way you clean data will be modernized enabling you to easily to detect, investigate, and correct inaccurate data values in your data set.
The strength of using these macro programs to automate cleaning data and creating documentation lies in their general applicability and simplicity of use. The only requirement for you to use them is having a SAS data set with labels and formats assigned to the variables.
You will use these tools in every stage of the life cycle of your data. Read Appendix A to understand more about the data life cycle. You will read about the common activities in every stage of the data life cycle, learning how your data flows through each stage from inception of the idea to acquire your data through archival at project end. You will find useful checklists showing recommended tasks for cleaning, using, distributing, and archiving your data.
Chapter 2: The Data Detective’s Toolkit and SAS
Introduction
In this chapter you will learn SAS programming features needed to understand the examples in this book and to automate data cleaning and report generation using the SAS macros from this book. You will discover:
● How to prepare a SAS data set with embedded metadata needed by the SAS macro programs from the Data Detective’s Toolkit
● Fundamental concepts of the SAS macro programming language needed to run the macro SAS programs and customize reports
● How to use the Output Delivery System to obtain data sets from SAS procedures and to create reports or files in the Microsoft Excel, Microsoft Word, or Adobe Reader format.
Preparing Your SAS Data Set
One of the most beneficial features of SAS is the facility to store useful information with each variable in a SAS data set. This type of information about a variable or data set is called metadata. Metadata is data about other data. SAS also automatically stores helpful information (metadata) about the data set at the time it is created or when the metadata of a SAS data set is changed. The SAS macro programs in the Data Detective’s Toolkit use this metadata to create codebooks, crosswalks, and master data set lists. This metadata is also used to automate data cleaning, error detection, and quality control.
This section provides instruction on adding metadata to a SAS data set so that you get the most benefit from using the Data Detective’s Toolkit when you prepare your data set. You create this metadata by using SAS statements to easily add text descriptions to variables, their values and data sets.
Types of Metadata
The metadata stored with your SAS data set and used by the Data Detective’s Toolkit can be classified into three categories as listed below:
● Descriptive metadata describing the meaning and values of your variables
● Structural metadata describing the structure of your data set such as number of observations and number of variables
● Administrative data describing attributes of a data set when it was created, including information such as date created, file type, protection, and data set label
Having this information included as part of the data makes each SAS data set self-contained and self-documenting. This section describes how you can use SAS to create three types of descriptive labels that can be assigned to the following:
● The SAS data set (Administrative metadata)
● Each variable in the data set (Descriptive metadata)
● The data values in those variables (Descriptive metadata)
Nearly all the structural and administrative metadata is created by SAS when the data set is created, but it can also be added, updated, or changed after the data set is created.
Using SAS to add Metadata to Your Data Set
It is easy to create a data set with the metadata needed to automate data cleaning and report generation with macro programs from the Data Detective’s Toolkit. After the overview describing the flow of the program in Example 2-1, you will find instructions on storing metadata with each variable by creating and storing formats and labels with your own data sets.
Example 2-1: Adding Metadata to your SAS data set
Program 2-1 is an example of a program preparing a data set to be used with macros from the Data Detective’s Toolkit.
Program 2-1: Program to Add Formats and Labels to a SAS Data Set
/* DEFINE FOLDER TO WRITE SAS DATA SET*/
libname My_Data /Data_Detective/Book/SAS_Datasets
;
/* STEP 1) Create formats to define meaning of values for each variable*/
proc format;
value $anytext
=Missing (blank)
other=Data present
;
value $showall default = 40
=Missing (blank)
;
value race 1