For loop/subroutine question

For questions regarding programming in the EViews programming language.

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

MSU_RA
Posts: 2
Joined: Wed Jan 16, 2013 11:15 am

For loop/subroutine question

Postby MSU_RA » Tue Jan 22, 2013 10:26 am

I am working on a subroutine that will normalise the values of certain variable groups. The code for the subroutine at this point looks like this:

Code: Select all

subroutine grpset(string %grpnum, string %regnum) '******************************************************************************************************************** '* Combines group components, creates test values and runs the grp_norm subroutine * '* No calls * '* String %vt is vt-refernce table name * '******************************************************************************************************************** %gname="grp" + %grpnum + "_" + %regnum ' Calculate percent off of the sum of components to the parent component. Essentially this difference must be zero. 'Create an empty variable to hold the value of the sum of a group's elements series sum=0 'Create a control variable for use in for loops !g=1 'Start a for loop that goes through each element of %gname and adds the values of that element to the sum placeholder for !g=1 to {%gname}.@count 'Add the current element of the FOR loop to the sum of all previous elements until the loop finishes running sum= sum + {%gname}(!g) next 'Creates a series called ptest{%grpnum} that creates a scale representing the difference between the parent group and the sum of all children groups series ptest{%grpnum}_{%regnum} = (r{%regnum}_{%grpnum} - sum)/r{%regnum}_{%grpnum} 'Calls the subroutine "norm_group" for %gname, which then scales down the values of the children, based on the value of "ptest" so that when added together 'the children all equal the parent group call norm_group(@str(%grpnum), @str(%regnum)) 'Reset the "sum" placeholder to 0 so that sum=0 'Essentially a repetition of the previous FOR loop, but this time using the scaled down values of the children elements for !g = 1 to {%gname}.@count sum=sum + {%gname}(!g) next 'Create a series called "test{%grpnum}" that subtracts the sum of the children elements from the parent group. We expect to get a value of ZERO throughout the series '"test" if the preceding steps went well series test{%grpnum}_{%regnum} = (r{%regnum}_{%grpnum} - sum) endsub '********************************************************************************************************************
and the subroutine norm_group looks like this

Code: Select all

*********************************** subroutine norm_group(string %grpnum, string %regnum) '******************************************************************************************************************** '* Labels and sets conversion * '* No calls * '* String %vt is vt-refernce table name * '******************************************************************************************************************** %gname="grp" + %grpnum + "_" + %regnum %cname="ptest" + %grpnum+ "_" + %regnum for !i=1 to {%gname}.@count %sname={%gname}.@seriesname(!i) 'Variable name {%sname}=@round({%sname}*(1+{%cname})) next endsub '************************************************************
It's been working well for the most part, but I'm trying to create a loop that uses this routine to normalise data from a variety of regions. Something like:

Code: Select all

for !i=0 to !i=6 ' Set groups group grp100_{!i} r{!i}_10 r{!i}_95 r{!i}_96 r{!i}_97 group grp10_{!i} r{!i}_101 r{!i}_102 group grp101_{!i} r{!i}_1011 r{!i}_1012 r{!i}_1013 group grp102_{!i} r{!i}_1021 r{!i}_1022 r{!i}_1023 r{!i}_1024 r{!i}_1025 r{!i}_1026 r{!i}_1027 r{!i}_1029 group grp1011_{!i} r{!i}_11 r{!i}_21 group grp1012_{!i} r{!i}_23 group grp1013_{!i} r{!i}_31_33 group grp1021_{!i} r{!i}_42 r{!i}_44_45 r{!i}_48_49 r{!i}_22 group grp1022_{!i} r{!i}_51 group grp1023_{!i} r{!i}_52 r{!i}_53 group grp1024_{!i} r{!i}_54 r{!i}_55 r{!i}_56 group grp1025_{!i} r{!i}_61 r{!i}_62 group grp1026_{!i} r{!i}_71 r{!i}_72 group grp1027_{!i} r{!i}_81 group grp1029_{!i} r{!i}_99 call grpset("10", @str(!i)) call grpset("100", @str(!i)) call grpset("101", @str(!i)) call grpset("102", @str(!i)) call grpset("1011", @str(!i)) call grpset("1012", @str(!i)) call grpset("1013", @str(!i)) next
where !i represents the region numbers. Essentially, I want this to create groups for each region through region six. However, when I try to run the code, I continue to get an error that looks something like:

Syntax error in "FOR !G=1 TO GRP100_3.@COUNT


This always seems to happen for whichever group is second in the list of "call grpset." I don't understand why this is happening, because I expect the program to try and deal with GRP100_0 before it reaches GRP100_3. Does it looks like something is adding numbers to the %regnum variable that I haven't noticed?

I appreciate any advice

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13604
Joined: Tue Sep 16, 2008 5:38 pm

Re: For loop/subroutine question

Postby EViews Gareth » Tue Jan 22, 2013 10:32 am

Could you post the workfile and program?

MSU_RA
Posts: 2
Joined: Wed Jan 16, 2013 11:15 am

Re: For loop/subroutine question

Postby MSU_RA » Tue Jan 22, 2013 11:31 am

Hi all, I've solved my problem by replacing "!i" with "!d"


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 0 guests