Page 1 of 1

Group Sum

Posted: Thu Aug 27, 2009 12:15 pm
by caburdick
I'm trying to write a subroutine to compute the sum of the series contained in a group.


subroutine grp_sum( series grpsum, group grp )

smpl @all
grpsum = 0
for !grcount = 1 to grp.@count
grpsum = grpsum + grp(!grcount)
show grpsum
next !grcount

endsub

This subroutine produces an output series equal to zero! The show grpsum statement seems to indicate that the subroutine is doing what I want up until the very end when the series name input into the subroutine call is set to zero! Why is this happeneing?

Modified: 1995Q1 2015Q4 // grpsum = grpsum + grp(47)
Modified: 1995Q1 2015Q4 // grpsum = grpsum + grp(48)
Modified: 1995Q1 2015Q4 // grpsum = grpsum + grp(49)
Modified: 1995Q1 2015Q4 // grpsum = grpsum + grp(50)
Modified:1995Q12015Q4//grpsum=grpsum+grp(51)
Modified: 1995Q1 2015Q4 // pms_lv_us2_s_m1s3 = 0

Re: Group Sum

Posted: Thu Aug 27, 2009 12:43 pm
by EViews Gareth
There is nothing intrinsically wrong with the code, in fact I ran it as is, and it worked fine.

I'll point out that you're probably better off using the Group sum function ;) @rsum

Here's a program showing that your way works, and matches the built-in function:

Code: Select all

create u 100 series x1=nrnd series x2=nrnd series x3=nrnd group xs x1 x2 x3 series sum subroutine grp_sum( series grpsum, group grp ) smpl @all grpsum = 0 for !grcount = 1 to grp.@count grpsum = grpsum + grp(!grcount) show grpsum next !grcount endsub call grp_sum(sum,xs) series sum2 = @rsum(xs) show sum sum2

Re: Group Sum

Posted: Sun Apr 10, 2016 12:17 pm
by dagfinnrime
Hi,

@rsum is of course very nice, but it propagates NAs. Code above can be fixed to deal with NAs.

Code: Select all

'some group grp series grpsum = 0 for !grcount = 1 to grp.@count grpsum = grpsum + @nan(grp(!grcount),0) next
Maybe add a version of group-row functions that don't propagate NAs? Like with moving-stat functions?