之前發(fā)文介紹了《如何把國(guó)家統(tǒng)計(jì)局地區(qū)分省年度數(shù)據(jù)快速整理成省級(jí)面板數(shù)據(jù)》。雖然能實(shí)現(xiàn)快速把國(guó)家統(tǒng)計(jì)局地區(qū)分省年度數(shù)據(jù)快速整理成省級(jí)面板數(shù)據(jù)驾锰,但是實(shí)際操作中仍然需要修改一些指令。下面的代碼能夠一鍵實(shí)現(xiàn)把國(guó)家統(tǒng)計(jì)局地區(qū)分省年度數(shù)據(jù)快速整理成省級(jí)面板數(shù)據(jù)。
進(jìn)入國(guó)家統(tǒng)計(jì)局→統(tǒng)計(jì)數(shù)據(jù)(數(shù)據(jù)從查詢)→地區(qū)數(shù)據(jù)(分省年度數(shù)據(jù))下載分省數(shù)據(jù)到文件夾。
數(shù)據(jù)
運(yùn)行一下以下代碼能:
cd D:\科研數(shù)據(jù)\統(tǒng)計(jì)局?jǐn)?shù)據(jù)\新建文件夾 //數(shù)據(jù)所在文件夾
local files: dir "." files "*.xls",respectcase
local f =1
foreach t in `files' {
local b = subinstr("`t'",".xls","",.)
import excel using "`t'", clear
local i = substr(A[2],10,.)
drop A
ds
foreach j in `r(varlist)'{
replace `j'="a`f'"+substr(`j',1,4) in 4
}
foreach l in `r(varlist)'{
local k =`l'[4]
rename `l' `k'
}
keep in 5/35
gen id = _n
reshape long a`f', i(id) j(year)
label variable a`f' `"`i'"'
destring a`f',replace
save "`b'", replace
local f=`f'+1
}
*合并數(shù)據(jù)
local files: dir "." files "*.dta",respectcase
dis `files'
foreach j in `files' {
cap merge 1:1 id year using "`j'"
cap drop _merge
}
gen 省份=""
replace 省份 = "北京市" if id==1
replace 省份 = "天津市" if id==2
replace 省份 = "河北省" if id==3
replace 省份 = "山西省" if id==4
replace 省份 = "內(nèi)蒙古自治區(qū)" if id==5
replace 省份 = "遼寧省" if id==6
replace 省份 = "吉林省" if id==7
replace 省份 = "黑龍江省" if id==8
replace 省份 = "上海市" if id==9
replace 省份 = "江蘇省" if id==10
replace 省份 = "浙江省" if id==11
replace 省份 = "安徽省" if id==12
replace 省份 = "福建省" if id==13
replace 省份 = "江西省" if id==14
replace 省份 = "山東省" if id==15
replace 省份 = "河南省" if id==16
replace 省份 = "湖北省" if id==17
replace 省份 = "湖南省" if id==18
replace 省份 = "廣東省" if id==19
replace 省份 = "廣西壯族自治區(qū)" if id==20
replace 省份 = "海南省" if id==21
replace 省份 = "重慶市" if id==22
replace 省份 = "四川省" if id==23
replace 省份 = "貴州省" if id==24
replace 省份 = "云南省" if id==25
replace 省份 = "西藏自治區(qū)" if id==26
replace 省份 = "陜西省" if id==27
replace 省份 = "甘肅省" if id==28
replace 省份 = "青海省" if id==29
replace 省份 = "寧夏回族自治區(qū)" if id==30
replace 省份 = "新疆維吾爾自治區(qū)" if id==31
order 省份 id year
*刪除單變量數(shù)據(jù)
foreach j in `files' {
erase "`j'"
}
一鍵實(shí)現(xiàn)16個(gè)省級(jí)宏觀指標(biāo)的面板數(shù)據(jù)。
省級(jí)面板數(shù)據(jù)