Hi all,
I need to define a program to read a URL from the EIA API with the data provided in json format. I am using the jsonio package from https://wbuchanan.github.io/StataJSON
The below code reads the json data and outputs a stata dataset. Each series that is listed in the URL part (facets[series][]=C010000001&facets[series][]=C010010001) is output in the resulting stata dataset along with the date.
I would like to take this code and define a program to read other data series provided by EIA. I am looking for something like this.
program define read_eia_url
[above code with necessary changes goes here]
end
And read a different URL using the program defined above. For example:
The number of series that are read each time can vary from a single series to more than 10 series.
Any help is appreciated.
I need to define a program to read a URL from the EIA API with the data provided in json format. I am using the jsonio package from https://wbuchanan.github.io/StataJSON
The below code reads the json data and outputs a stata dataset. Each series that is listed in the URL part (facets[series][]=C010000001&facets[series][]=C010010001) is output in the resulting stata dataset along with the date.
Code:
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*) *
program define read_eia_url
[above code with necessary changes goes here]
end
And read a different URL using the program defined above. For example:
Code:
read_eia_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")
Any help is appreciated.
Comment