On Mon, Mar 13, 2023 at 7:33 AM Artur T. <atecon(a)posteo.de> wrote:
Hi all,
I obtain an error with the aggregate() function when calling a
user-defined function for aggregation.
The following example works fine, if one groups the data by a single
variable but it fails with a second group-by variable:
<hansl>
set verbose off
clear
open grunfeld -q
function scalar quantile_range (series y)
/* Compute the interquartile range. */
scalar ret = quantile(y, 0.75) - quantile(y, 0.25)
print ret
return ret
end function
# Works
list groupby = firmname
list L = invest value
matrix agg = aggregate(L, groupby, quantile_range)
print agg
# Fails
list groupby = firmname ticker # we add "ticker"
list L = invest value
matrix agg = aggregate(L, groupby, quantile_range)
print agg
</hansl>
The aggregator function has to be coded a bit more defensively. Of the
100 cases of (firmname, ticker) 90 are empty, so it has to be ready to
handle absence of data, as in
if $nobs == 0 || sum(ok(y)) == 0
return NA
endif
Allin