On Tue, 28 Apr 2020, Riccardo (Jack) Lucchetti wrote:
On Tue, 28 Apr 2020, F.R.Costa wrote:
> I guys,hope you're all well,
>
> I have a very quick question. I am working with stock market data. Let's
> say I have price observations for stock X. I want to exclude stock X from
> my data if it doesn't have at least 24 consecutive observations. Is there
> any simple command able to retrieve a consecutive count of observations.
> $nobs would give a total count, but is there a way to take account of this?
You may find this example useful:
<hansl>
set verbose off
set seed 123
nulldata 60
setobs 5 2020-01-01
# construct an artificially "gappy" series
x = uniform() < 0.8 ? normal() : NA
# now compute the streaks
series streak = ok(x[1]) # initialise
series streak = ok(x) ? streak(-1) + 1 : 0 # compute the actual streaks
printf "longest streak = %d\n", max(streak)
print x streak -o
</hansl>
Filipe, Jack beat me to it, with a more concise and elegant
version than I was writing, but just in case it's helpful
here's another approach.
<hansl>
set verbose off
nulldata 700
setobs 12 1960:01
series x = normal()
x = abs(x) > 2.4 ? NA : x
summary x --simple
maxconsec = 0
loop i = 1..$nobs --quiet
consec = 0
loop j = i..$nobs --quiet
if ok(x[j])
consec++
else
break
endif
endloop
if consec > maxconsec
maxconsec = consec
endif
endloop
printf "x has a max of %d consecutive observations\n", maxconsec
</hansl>
Allin