Hélio,
OK so the following works…kind of. I do not believe you can affect the dataset from within
a function. So set up as a time series needs to be handled outside of the functions. It
might be better to return a bundle rather than a series?
Logan
function series get_series_BSL(string dataname)
set echo off
set messages off
string bslurl =
"http://api.bls.gov/publicAPI/v1/timeseries/data/"~dataname
string s = readfile(bslurl)
sprintf q "\""
sprintf sq "\'"
sprintf nl "\n"
s = strsub(s,q,"")
s = strsub(s,"{year:",nl)
s = strsub(s," ","")
s = strsub(s,"[","")
s = strsub(s,"{","")
s = strsub(s,"]","")
s = strsub(s,"}","")
s = strsub(s,"'","")
s = strsub(s,":",",")
s = strsub(s,",period,","")
s = strsub(s,","," ")
s = strsub(s,"seriesID","obs blank blank blank ")
bundle dt
scalar i = 1
string line
scalar lcount = 0
string obs1 = null
string obs2 = null
loop while getline(s, line)
temp = strsplit(line,1)~","~strsplit(line,5)
if i > 2
sprintf ii "%d", i-2
dt.@ii = temp
temp = dt[@ii]
lcount ++
endif
i++
endloop
outfile test.csv --write
printf "%s\n", dt[1]
loop for (j = lcount; j>1; j--) --quiet
printf "%s\n", dt[$j]
endloop
outfile test.csv --close
s = readfile("test.csv")
s = strsub(s,","," ")
i = 1
scalar jj = 1
string sPD = ""
matrix value = zeros(lcount-1,1)
loop while getline(s, line)
if i > 1
obs1 = (i=2) ? strsplit(line,1) : obs1
obs2 = strsplit(line,1)
sPD = (strstr(obs1,"M")!="") ? "M13" :
"Q5"
if strstr(obs2,sPD)=""
value[i-1] = atof(strsplit(line,2))
endif
endif
i = strstr(obs2,sPD)="" ? i+1 : i
jj++
endloop
value = trimr(value,0,jj-i)
print value
pd = (strstr(obs1,"M")!="") ? 12 :
(strstr(obs1,"Q")!="") ? 4 : 1
obs1 = (pd=12) ? strsub(obs1, "M", ":") : strsub(obs1,
"Q", ":")
obs2 = (pd=12) ? strsub(obs2, "M", ":") : strsub(obs2,
"Q", ":")
print obs1
print obs2
setobs pd @obs1 --time-series #no effect outside the function
smpl @obs1 @obs2 #no effect outside the function
series @dataname = value
delete dt
return @dataname
end function
set echo off
set messages off
clear
nulldata 36
test = get_series_BSL("LAUCN040010000000005")
From: gretl-devel-bounces(a)lists.wfu.edu [mailto:gretl-devel-bounces@lists.wfu.edu] On
Behalf Of Hélio Guilherme
Sent: Wednesday, April 23, 2014 4:31 PM
To: Gretl development
Subject: Re: [Gretl-devel] New function for retrieving data from BLS
Hi Logan,
Tried to make a function, but got stuck on append.
function void get_series_BSL(string dataname)
set echo off
set messages off
string bslurl = "http://api.bls.gov/publicAPI/v1/timeseries/data/"~dataname
string s = readfile(bslurl)
sprintf q "\""
sprintf sq "\'"
sprintf nl "\n"
s = strsub(s,q,"")
s = strsub(s,"{year:",nl)
s = strsub(s," ","")
s = strsub(s,"[","")
s = strsub(s,"{","")
s = strsub(s,"]","")
s = strsub(s,"}","")
s = strsub(s,"'","")
s = strsub(s,":",",")
s = strsub(s,",period,","")
s = strsub(s,","," ")
s = strsub(s,"seriesID","obs blank blank blank ")
bundle dt
scalar i = 1
string line
scalar lcount = 0
loop while getline(s, line)
temp = strsplit(line,1)~","~strsplit(line,5)
if i > 2
sprintf ii "%d", i-2
dt.@ii<mailto:dt.@ii> = temp
temp = dt[@ii]
lcount ++
endif
i++
endloop
outfile test.csv --write
printf "%s\n", dt[1]
loop for (j = lcount; j>1; j--)
printf "%s\n", dt[$j]
endloop
outfile test.csv --close
delete dt
append test.csv #It fails here because is inside function
end function
set echo off
set messages off
clear
get_series_BSL("LAUCN040010000000005")
On Wed, Apr 23, 2014 at 8:24 PM, Logan Kelly
<logan.kelly@uwrf.edu<mailto:logan.kelly@uwrf.edu>> wrote:
Ok. As promised, the code is not pretty, but here is a start at parsing a bls api call for
a single series, which is what the current readfile() function can handle. It works but I
can foresee the code being rather fragile.
Logan
clear
set echo off
string s =
readfile("http://api.bls.gov/publicAPI/v1/timeseries/data/LAUCN04001...
sprintf q "\""
sprintf sq "\'"
sprintf nl "\n"
s = strsub(s,q,"")
s = strsub(s,"{year:",nl)
s = strsub(s," ","")
s = strsub(s,"[","")
s = strsub(s,"{","")
s = strsub(s,"]","")
s = strsub(s,"}","")
s = strsub(s,"'","")
s = strsub(s,":",",")
s = strsub(s,",period,","")
s = strsub(s,","," ")
s = strsub(s,"seriesID","obs blank blank blank ")
bundle dt
scalar i = 1
string line
scalar lcount = 0
loop while getline(s, line)
temp = strsplit(line,1)~","~strsplit(line,5)
if i > 2
sprintf ii "%d", i-2
dt.@ii<mailto:dt.@ii> = temp
temp = dt[@ii]
lcount ++
endif
i++
endloop
outfile test.csv --write
printf "%s\n", dt[1]
loop for (j = lcount; j>1; j--)
printf "%s\n", dt[$j]
endloop
outfile test.csv --close
append test.csv
_______________________________________________
Gretl-devel mailing list
Gretl-devel@lists.wfu.edu<mailto:Gretl-devel@lists.wfu.edu>
http://lists.wfu.edu/mailman/listinfo/gretl-devel