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 = 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> 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/LAUCN040010000000005")
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 = 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
http://lists.wfu.edu/mailman/listinfo/gretl-devel