On Wed, 22 Sep 2021, Allin Cottrell wrote:
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>
Thanks, Allin. This is the way I originally used, but then I thought that
maybe it was more efficient to minimise the number of times the sscanf
function is used (maybe I'm wrong though, I didn't time the two
alternatives).
However, I believe that this function could have its place in the extra
package, with a view to possible generalisations whereby one could pass an
extra parameter string to specify unusual format, such as the US
convention "%Y-%d-%m".
Besides, I have another proposal for handling epoch dates, but maybe this
one could be a native function, rather than being included into extra: a
function for retrieving a calendar item (day, month, etc) from an epoch
day. For example:
<hansl>
set verbose off
function series epoch2period(series e, string what)
series iso = isodate(e)
arg = tolower(what)
yr = floor(iso / 10000)
if arg == "y"
return yr
endif
mth = floor((iso - yr * 10000) / 100)
if arg == "m"
return mth
elif arg == "q"
return ceil(mth / 4)
endif
day = iso - yr * 10000 - mth * 100
if arg == "d"
return day
elif arg == "w"
return isoweek(yr, mth, day)
elif arg == "wd"
return weekday(yr, mth, day)
else
printf "%s: conversion not supported\n", what
series ret = NA
return ret
endif
end function
###
# example
###
nulldata 14
setobs 7 2021-09-01
epoch = epochday($obsmajor, $obsminor, $obsmicro)
wd = epoch2period(epoch, "wd")
series m = epoch2period(epoch, "m")
print epoch wd m -o
</hansl>
As usual, comments welcome.
-------------------------------------------------------
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
-------------------------------------------------------