I was playing with the nice COVID international data set available at
https://github.com/owid/covid-19-data/tree/master/public/data
The csv file contains a "date" column, that gretl reads as a
string-valued series. In order to turn it into proper dates, I ended up
writing this little function that turns a string valued series holding
dates into a numeric string holding the epoch day. The requisite is that
the string is organised as year-month-day, with an arbitrary delimiter
that must be passed as the optional second parameter to the function
(default: dash).
<hansl>
function series str2epoch(series s, string delimiter[null])
strings datestrs = strvals(s)
scalar n = nelem(datestrs)
fmt = "%d-%d-%d"
if exists(delimiter)
fmt = strsub(fmt, "-", delimiter)
endif
scalar y m d
matrix ymd = seq(1,n)' ~ zeros(n, 3)
loop i = 1 .. n
s_i = datestrs[i]
sscanf(s_i, fmt, y, m, d)
ymd[i,2:] = {y,m,d}
endloop
series s_y = replace(s, ymd[,1], ymd[,2])
series s_m = replace(s, ymd[,1], ymd[,3])
series s_d = replace(s, ymd[,1], ymd[,4])
return epochday(s_y, s_m, s_d)
end function
</hansl>
I'm posting it here in case it's useful to someone, but I'd also like to
ask you people if you think this as valuable enough to be included in the
extra package.
-------------------------------------------------------
Riccardo (Jack) Lucchetti
Dipartimento di Scienze Economiche e Sociali (DiSES)
Università Politecnica delle Marche
(formerly known as Università di Ancona)
r.lucchetti(a)univpm.it
http://www2.econ.univpm.it/servizi/hpp/lucchetti
-------------------------------------------------------