Calling subroutines within a subroutine
Posted: Mon May 25, 2009 2:59 am
by dagfinnrime
Hi,
I would like to call different subroutines within a subroutine.
Example:
' Lots of subroutines, like sub1 to subN
subroutine sub1
series var{%x} = ....
endsub
subroutine all(string %sub)
for .....
call {%sub}
next
endsub
call all("sub1")
call all("sub2")
etc.
This doesn't work. I have tried with defining a new macro-variable within the All-subroutine that equals %sub, but that doesn't work either.
Any suggestions?
Thanks in advance,
Dagfinn
Re: Calling subroutines within a subroutine
Posted: Mon May 25, 2009 11:28 pm
by dagfinnrime
Hi,
let me be more precise:
I need to create lots of similar variables: var1nor var1swe var1USA var1ger etc. I have to create lots of such sets of variables, and they have different definitions.
So I create a table called codes with entries NOR SWE USA GER etc.
I have N different codes, and M different types of variables. I create a subroutine for each type of variable (no argument for routine).
subroutine sub1
series var1{%code} = ....
endsub
subroutine sub2
series var2{%code} = .... ' a different definition
endsub
....
subroutine subM ....
I want to call these subroutines from within a general one that takes the name of the subroutine as an argument, so that I can select which subroutine (and hence, variables) to call.
subroutine CreateVariables(string %dagfinnsub)
%dagfinn = %dagfinnsub
for !j = 1 to 100 ' Say N is equal to 100
%code = names(1,!j)
call {%dagfinn}
next
endsub
Passing the %code from the CreateVariables subroutine to the inner subroutine works fine if I e.g. write "call sub1" in CreateVariables.
I get the following error-message when running "call CreateVariables("sub1")":
{%dagfinn} is not a defined subroutine in "CALL SUB1"
Thanks for any suggestions,
Dagfinn
Re: Calling subroutines within a subroutine
Posted: Tue May 26, 2009 8:31 am
by EViews Gareth
I don't think you can use a program variable to define which subroutine you want to call, unfortunately.
An immediate, although burdensome, work-around would be to have your master sub-routine take in a string, and then use if conditions to call the appropriate sub. Something like:
Code: Select all
subroutine master(string %subname)
if %subname = "sub1" then
call sub1
endif
if %subname = "sub2" then
call sub2
endif
endsub
Or something along those lines.