Created
January 13, 2023 17:23
-
-
Save bbdaniels/0f122771cc2281e5b23827bd98b03ba1 to your computer and use it in GitHub Desktop.
Rapid import and infill of semi-structured data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
global data "${wd}/ppol_528_GuzmanLidia/data/Data_exp" | |
local data : dir "${data}/xlsx/" files * | |
clear | |
tempfile allData | |
save `allData' , emptyok | |
cap prog drop infill | |
prog def infill | |
syntax anything | |
qui foreach var of varlist `anything' { | |
local theValue = "" | |
forv i = 1/`c(N)' { | |
local nextValue = `var'[`i'] | |
if ("`nextValue'" == "") | ("`nextValue'" == ".") { | |
cap replace `var' = "`theValue'" in `i' | |
cap replace `var' = `theValue' in `i' | |
} | |
else { | |
local theValue = "`nextValue'" | |
} | |
} | |
} | |
end | |
foreach file in `data' { | |
qui import excel using "${data}/xlsx/`file'" , clear cellrange(A10) first | |
qui drop if Chapter == "Total" | |
set trace off | |
qui infill Year Month Province | |
qui destring * , replace | |
di "`file' -- `c(N)'" | |
drop G-K | |
qui compress | |
qui append using `allData' | |
qui save `allData' , replace | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment