/* Biostat 212: Week 4 Data cleaning part 2 Create Analysis Dataset do-file 1) Run Cleaning do-file on pt1.dta and pt2.dta 2) Append pt1.dta and pt2.dta 3) Merge full pt dataset with doctors dataset */ clear capture log close cd "/Users/crystal/Box/_Biostat212_2021/Week4/Lecture" log using "output/week4lecture.smcl", replace /* Open pt1.dta and clean dataset */ use "data/pt1.dta", clear quietly run "do/cleaning.do" //runs cleaning.do file on pt1.dta data save "data/pt1_clean.dta", replace /* Open pt2.dta and clean dataset */ use "data/pt2.dta", clear quietly run "do/cleaning.do" //runs cleaning.do file on pt2.dta data save "data/pt2_clean.dta", replace /* Open pt1_clean.dta Append pt2_clean.dta */ use "data/pt1_clean.dta", clear append using "data/pt2_clean.dta" /* Check appended datasets */ describe browse *Any variable that is not in both datasets will have missing values tab nmorphine, missing tab dis_date, missing save "data/pts_complete.dta", replace *************************************************************** /* Merge with doctor data using the "docid" variable */ *************************************************************** /*NOTE: class demo L4 Part 4 starts here. make sure you run everything above here BEFORE you start following along*/ *Check to see if we have multiple docid in our patient data isid docid *OR* duplicates report docid *Have multiple docids so need to use m:1 (many to 1) merge m:1 docid using "data/doctors.dta" tab _merge *Drop if observation only was in the "doctors" dataset drop if _merge==2 /* Save final dataset */ save "data/ptsanddocs_final.dta", replace log close ////////////////////////////////////////////// /* Reshape the reshape_example.dta dataset from long to wide */ * Open the reshape example dataset use data/reshape_example.dta, clear /*note: you need to remove quotes from the command when giving filepath. the following will give you an error: use "data/reshape_example.dta", clear */ * Explore dataset to verify it's in "long" format and what variables to use * for reshape command browse * Reshape from long to wide reshape wide sbp, i(ptid) j(visit) browse // check to make sure this worked