Count the number of NAs on a series period

For questions regarding programming in the EViews programming language.

Moderators: EViews Gareth, EViews Moderator, EViews Jason, EViews Matt

javiersan
Posts: 184
Joined: Mon Jan 19, 2009 8:18 am

Count the number of NAs on a series period

Postby javiersan » Sun Sep 20, 2020 1:39 pm

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

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

Postby EViews Gareth » Sun Sep 20, 2020 9:50 pm

Code: Select all

genr(r) counter = @recode(@date=@max(@date),0,@recode(series<>na,0,counter(1)+1))

javiersan
Posts: 184
Joined: Mon Jan 19, 2009 8:18 am

Re: Count the number of NAs on a series period

Postby javiersan » Mon Sep 21, 2020 6:36 am

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

EViews Matt
EViews Developer
Posts: 583
Joined: Thu Apr 25, 2013 7:48 pm

Re: Count the number of NAs on a series period

Postby EViews Matt » Tue Sep 22, 2020 9:54 am

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

javiersan
Posts: 184
Joined: Mon Jan 19, 2009 8:18 am

Re: Count the number of NAs on a series period

Postby javiersan » Tue Sep 22, 2020 11:54 am

Thanks Matt, it works really well.


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 1 guest