在 Stata 中提取 EIA API v2 数据

问题描述 投票:0回答:0

我有下面的 Stata 代码,我想将其转换为 .ado 文件,让我为不同的 URL 导入 EIA API 数据。

clear all
jsonio kv, file("https://api.eia.gov/v2/petroleum/cons/prim/data/?frequency=monthly&data[0]=value&facets[series][]=C010000001&facets[series][]=C010010001&start=2019-01&sort[0][column]=period&sort[0][direction]=desc&offset=0&length=5000&api_key=273cebf41acf38547e56cbba6b91765f")

cap drop id key_var
gen id = ustrregexs(2) if ustrregexm(key, "(id_)([0-9]+)(/)(.*)")
gen key_var = ustrregexs(4) if ustrregexm(key, "(id_)([0-9]+)(/)(.*)")
keep if inlist(key_var, "period", "series", "value")
drop key
destring id, replace
reshape wide value, i(id) j(key_var) string
gen date = ym(real(substr(valueperiod, 1,4)),real(substr(valueperiod,6,2)))
format date %tm
destring valuevalue, replace
drop id valueperiod
reshape wide valuevalue, i(date) j(valueseries) string
rename(valuevalue*) *

如何在 Stata 中定义一个 .ado 程序来获取不同 URL 的数据而不重复整个代码?

例如,我想从以下网址获取数据: “https://api.eia.gov/v2/petroleum/cons/prim/data/?frequency=monthly&data[0]=value&facets[series][]=C500000001&facets[series][]=C720000001&start=2019-01&sort[0 ][column]=period&sort[0][direction]=desc&offset=0&length=5000&api_key=273cebf41acf38547e56cbba6b91765f"

任何帮助表示赞赏。

url stata ado
© www.soinside.com 2019 - 2024. All rights reserved.