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
Group Sum
Moderators: EViews Gareth, EViews Jason, EViews Moderator, EViews Matt
-
- Fe ddaethom, fe welon, fe amcangyfrifon
- Posts: 13315
- Joined: Tue Sep 16, 2008 5:38 pm
Re: Group Sum
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:
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
Follow us on Twitter @IHSEViews
-
- Posts: 49
- Joined: Sat Oct 11, 2008 9:37 am
- Location: Oslo
- Contact:
Re: Group Sum
Hi,
@rsum is of course very nice, but it propagates NAs. Code above can be fixed to deal with NAs.
Maybe add a version of group-row functions that don't propagate NAs? Like with moving-stat functions?
@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?
Who is online
Users browsing this forum: No registered users and 17 guests