Page 1 of 1
Count the number of NAs on a series period
Posted: Sun Sep 20, 2020 1:39 pm
by javiersan
Hi, I want to create a series which counts the number of NAs until the next non-NA series value. How can I do this? Thanks
For example:
Series Counter
2 0
4 0
5 0
8 0
na 3
na 2
na 1
9 0
10 0
na 3
na 2
na 1
12 0
Re: Count the number of NAs on a series period
Posted: Sun Sep 20, 2020 9:50 pm
by EViews Gareth
Code: Select all
genr(r) counter = @recode(@date=@max(@date),0,@recode(series<>na,0,counter(1)+1))
Re: Count the number of NAs on a series period
Posted: Mon Sep 21, 2020 6:36 am
by javiersan
Thanks Gareth, this is great. What I want to do is fill in NA values in a series with the previous non-NA value as long as there isn't a block of NAs ahead that is greater to a given threshold (in the case below =10). This is what I come up with, but maybe there is a faster way you could suggest?
genr(r) _counter=@recode(@date=@max(@date),0,@recode({%ser}<>na,0,_counter(1)+1))
series _block=@recode(_counter>_counter(-1),_counter,na)
_block=@recode({%ser}=na and _block=na,_block(-1),_block)
{%ser}=@recode({%ser}=na and _block<10,{%ser}(-1),{%ser})
delete _counter _block
Re: Count the number of NAs on a series period
Posted: Tue Sep 22, 2020 9:54 am
by EViews Matt
Hello,
Code: Select all
genr(r) tmp = @recode(@isna({%ser}), @nan(tmp(1)+1, 1), 0)
tmp = @recode(tmp > 0 and tmp(-1) > 0, tmp(-1), tmp)
{%ser} = @recode(tmp > 0 and tmp < 10, {%ser}(-1), {%ser})
delete tmp
Re: Count the number of NAs on a series period
Posted: Tue Sep 22, 2020 11:54 am
by javiersan
Thanks Matt, it works really well.