Multiple Program Files
When working with long programs, you may wish to organize your code using multiple files. For example, suppose you have a program file named “Powers.PRG” which contains a set of program lines that you wish to use.
While you may be tempted to string files together using the run command, we caution you that EViews will stop after executing the commands in a run-referenced file. Thus, a program containing the lines
run powers.prg
series x = nrnd
will only execute the commands in the file “Powers.PRG”, and will stop before generating the series X. This behavior is probably not what you intended.
The exec command may be used execute commands in a file in place of the run command. Though exec is quite similar to the run command, there are important differences between the two commands:
• First, exec allows you to write general programs that execute other programs, something that is difficult to do using run, since the run command ends all program execution when processing of the named file is completed. In contrast, once exec processing completes, the calling program will continue to run.
• Second, the default directory for exec is the Add-ins directory (in contrast with both run and include which defaults to using the EViews default directory). Thus, the command
exec myprog1.prg
will run the program file “Myprog1.prg” located in the default Add-ins directory. You may specify files using relative paths in the standard fashion. The command:
exec MyAddIn\myprog2.prg
runs the program “Myprog2.prg” located in the “MyAddIn” subdirectory of the Add-ins directory.
If you wish to run a program that is located in the same directory as the calling program, simply issue a “.\” at the start of the program name:
exec .\myprog2.prg
Alternatively you may use the include keyword to include the contents of a program file in another program file. For example, you can place the line
include powers
at the top of any other program that needs to use the commands in POWERS. include also accepts a full path to the program file, and you may have more than one include statement in a program. For example, the lines,
include c:\programs\powers.prg
include durbin_h
[more lines]
will first execute all of the commands in “C:\Programs\Powers.PRG”, will execute the commands in “Durbin_h.PRG”, and then will execute the remaining lines in the program file.
If you do not provide an absolute path for your include file, EViews will use the location of the executing program file as the base location. In the example above, EViews will search for the “Durbin_h.PRG” file in the directory containing the executing program.
Note that in contrast to using exec to execute another program, include simply inserts the child program into the parent. This insertion is done prior to executing any lines in either program. One important consequence of this behavior is that any program variables that are declared in the parent program will not be available in the child/included program, even if they are declared prior to the include statement.