Strings and variables together in the FOR loop?

For questions regarding programming in the EViews programming language.

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

mr.interested
Posts: 16
Joined: Wed Jul 14, 2010 5:57 am

Strings and variables together in the FOR loop?

Postby mr.interested » Fri Jul 23, 2010 6:47 am

I wrote a long program performing a number of tests. Previously, I had just a couple of workfiles, so I specified strings/variables at the beginning of the program and changed them each time I ran the program:

Code: Select all

%name = "wf1" <-- here !number = 100 <-- here etc. wfopen {%name} [body of the program] save {%name} close {%name}
Now I have dozens of workfiles, and I don't want to retype their names and specific values each time, since I often re-run the program. I also can't have a program for each workfile because I often update the program, and so I have to have only one program. So, my idea was to create the following loop:

Code: Select all

%name_1 = "wf1" %name_2 = "wf2" %name_3 = "wf3" !number_1 = 100 !number_2 = 50 !number_3 = 60 etc. for !loop=1 to 3 %name = %name_{!loop} %number = %number_{!loop} etc. wfopen {%name} [body of the program] save {%name} close {%name} next
Logically, each time the loop is run, each string and variable should be updated. For instance, in the first run, %name should take the value of %name_{!loop}, which is %name_1, which is wf1. But that simply doesn't work. The following error occurs:
No workfile exists - Use NEW or OPEN to create or load a workfile in "%NAME" = "wf1"
Why does it not work? I also tried, for example, %name = %name_ + {!loop}, %name = {%name_} + {!loop}, %name = "%name_" + "{!loop}", etc., but none of these worked. The very first one (%name = %name_ + {!loop}) makes sense, and it should work. I also tried to start the loop at the very beginning of the program, but that shouldn't make any difference, and it didn't--it didn't work. What do you think the reason is?

EViews Glenn
EViews Developer
Posts: 2682
Joined: Wed Oct 15, 2008 9:17 am

Re: Strings and variables together in the FOR loop?

Postby EViews Glenn » Fri Jul 23, 2010 9:02 am

In EViews 7, the following snippet works as expected (or at least as I expected):

Code: Select all

%temp = "" %name_1 = "wf1" %name_2 = "wf2" %name_3 = "wf3" for !loop=1 to 3 %name = %name_{!loop} %temp = %temp + " " + %name next statusline {%temp}
and produces
wf1 wf2 wf3
on the statusline.

If you are using EViews 6 or earlier, you are being caught by the fact that we don't allow for recursive substitution (where the substitution variable takes the value of a variable which is itself a substitution variable) in those versions. I don't know a particularly easy way around this.

Having said that, I'm not certain from the description of your problem above why you have the loops in this part of the program. If I'm understanding the nature of the task correctly, you could make a subroutine which did all of your work, given a name and an integer, then have a calling program which specifies values to do the work. Then just include the subroutine in any file that you wish to use (that way you can have a program for each workfile or set of workfiles, but they use the common code).

Or you could upgrade to EViews 7 :wink:


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 2 guests