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
Count the number of NAs on a series period
Moderators: EViews Gareth, EViews Moderator, EViews Jason, EViews Matt
-
EViews Gareth
- Fe ddaethom, fe welon, fe amcangyfrifon
- Posts: 13584
- Joined: Tue Sep 16, 2008 5:38 pm
Re: Count the number of NAs on a series period
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
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
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
-
EViews Matt
- EViews Developer
- Posts: 583
- Joined: Thu Apr 25, 2013 7:48 pm
Re: Count the number of NAs on a series period
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
Thanks Matt, it works really well.
Who is online
Users browsing this forum: No registered users and 1 guest
