1 Star 0 Fork 11

dingxiuhao/Stata-Event_Study

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ffindustries.ado 2.32 KB
一键复制 编辑 原始数据 按行查看 历史
*! Date : 2017-09-15
*! version : 0.3
*! Author : Richard Herron
*! Email : richard.c.herron@gmail.com
*! get Fama-French industry data, keep in memory
/*
2017-09-15 v0.3 smarter industries, simpler keep option, error handling
2017-05-05 v0.2 refactor, adds 12-industry download
2016-08-12 v0.1 original
*/
program ffindustries
version 13.1
syntax , clear [ Level(integer 48) KEEPfiles ]
/* test if industry level is valid */
if !inlist(`level', 5, 10, 12, 17, 30, 38, 48, 49) {
display as error ///
"industry levels are 5, 10, 12, 17, 30, 38, 48, and 49"
exit 197
}
/* /1* T/S *1/ */
/* local clear clear */
/* local level 48 */
/* get file */
local cloud "http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Siccodes`level'.zip"
copy "`cloud'" "Siccodes`level'.zip", replace
unzipfile "Siccodes`level'.zip", replace
/* read */
infix ///
int indNumber 1-2 ///
str indShort 4-9 ///
str indLong 11-120 ///
using "Siccodes`level'.txt", `clear'
drop if missing(indNumber) & missing(indShort) & missing(indLong)
/* put long name and SIC range in separate columns */
generate hasSic = regexm(indLong, "^[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]")
generate sicRange = substr(indLong, 1, 9) if hasSic
generate sicName = substr(indLong, 10, .) if hasSic
/* carryforward */
replace indNum = indNum[_n - 1] if missing(indNum)
replace indShort = indShort[_n - 1] if missing(indShort)
bysort indNum : replace indLong = indLong[1] if (_n > 1)
/* expand */
drop if missing(sicRange)
generate sicLow = substr(sicRange, 1, 4)
generate sicHigh = substr(sicRange, 6, 4)
destring sicLow sicHigh, replace
/* create row for every SIC code */
generate N = (sicHigh - sicLow) + 1
expand N
bysort indNumber sicLow : generate sic = sicLow + (_n - 1)
/* code duplicates every industry into "other" */
sort sic indNum
duplicates drop sic, force
sort indNum sic
/* clean up */
drop hasSic sicLow sicHigh N
quietly ds, has(type string)
foreach v of varlist `r(varlist)' {
replace `v' = strtrim( stritrim( `v' ) )
}
format sic %04.0f
compress
/* by default, remove files */
if ("`keepfiles'" == "") {
rm "Siccodes`level'.txt"
rm "Siccodes`level'.zip"
}
end
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/dingxiuhao/Stata-Event_Study.git
git@gitee.com:dingxiuhao/Stata-Event_Study.git
dingxiuhao
Stata-Event_Study
Stata-Event_Study
master

搜索帮助