1 Star 0 Fork 4

huangleiabcde/readWind

forked from 连享会/readWind 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
readWind.ado 2.49 KB
一键复制 编辑 原始数据 按行查看 历史
zhbsis 提交于 2019-06-22 19:20 . Rename readwind.ado to readWind.ado
*===============================================================================
* Program: readWind.ado
* Purpose: Reading data download from the wind into stata in a cleaning format
* Version: 2.0 (2019/04/13)
* Author: Devin Chu
* Website: http://www.github.com/zhbsis/readWind
*===============================================================================
capture program drop readWind
program define readWind
version 14.0
syntax namelist(min=1), key(str) timeType(str) t0(str) tn(str) [type(str) sheet(integer 1) encoding(str) tostring]
//1 readFile
local n=wordcount("`namelist'")
readFile, key(`key') type(`type') sheet(`sheet') encoding(`encoding') `tostring'
//2 tokenize
local var ""
foreach tempVar of varlist _all{
local var "`var' `tempVar'" //
}
tokenize `var' //
local first `1' `2' //
macro shift 2 //
local rest `*' //
/*rename stkcd comp*/
rename (`first') (stkcd comp)
local i=1
foreach var in `rest'{
rename `var' x`i'
local i=`i'+1
}
fastreshape long x, i(stkcd) j(time)
//3 if function
gen date=.
gen var=.
if("`timeType'"=="y"){
local t0=`t0'
local tn=`tn'
local dif = `tn'-`t0'+1
}
if("`timeType'"=="q"){
local t0=q(`t0')
local tn=q(`tn')
local dif = `tn'-`t0'+1
}
if("`timeType'"=="m"){
local t0=m(`t0')
local tn=m(`tn')
local dif = `tn'-`t0'+1
}
if("`timeType'"=="d"){
local t0=date("`t0'","YMD")
local tn=date("`tn'","YMD")
local dif = `tn'-`t0'+1
}
//4 main function
forvalues i=1(1)`n'{
local low = 1+(`i'-1)*`dif'
local up = `dif'+(`i'-1)*`dif'
local j=`i'-1
qui bys stkcd: replace date = `t0' + _n - 1 - `j'*`dif' if time <= `up' & time >= `low'
qui replace var=`i' if time <= `up' & time >= `low'
}
//5 format time index
if("`timeType'"=="q"){
format date %tq
}
if("`timeType'"=="m"){
format date %tm
}
if("`timeType'"=="d"){
format date %td
}
//6 reshape and cleaning
qui drop if stkcd=="" | stkcd=="数据来源:Wind"
qui gen stkcd_date=stkcd+"_"+string(date)
qui fastreshape wide x, i(stkcd_date) j(var)
qui drop time stkcd_date
qui order stkcd comp date
//7 rename
local i=1
foreach var in `namelist'{
qui rename x`i' `var'
local i=`i'+1
qui label var `var' ""
}
end
*cd "K:\readWind\"
*readWind ROA roe, key(ROA) timeType(q) t0(1995q1) tn(2017q4)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/huangleiabcde/readWind.git
[email protected]:huangleiabcde/readWind.git
huangleiabcde
readWind
readWind
master

搜索帮助