Skip to content

Instantly share code, notes, and snippets.

@bbdaniels
Created January 13, 2023 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbdaniels/0f122771cc2281e5b23827bd98b03ba1 to your computer and use it in GitHub Desktop.
Save bbdaniels/0f122771cc2281e5b23827bd98b03ba1 to your computer and use it in GitHub Desktop.
Rapid import and infill of semi-structured data
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