On Tue, 21 Sep 2021, Riccardo (Jack) Lucchetti wrote:
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>
The following is less general, but a more concise way of making the
conversion. (Being more concise than Jack is rarely possible, and of
course concision with loss of generality hardly counts!)
<hansl>
open owid-covid-data.csv
scalar y m d
series ed
loop i=1..$nobs
sscanf(date[i], "%d-%d-%d", y, m, d)
ed[i] = epochday(y, m, d)
endloop
print date ed -o
</hansl>
Allin