Command Reference : EViews Programming : Program Basics
  
Program Basics
 
What is a Program?
Creating a Program
Program Formatting
Saving a Program
Snapshots for Programs
Viewing Snapshots for Programs
Saving the Command Window
Encrypting a Program
Opening a Program
Executing a Program
The Run Program Dialog
The Run and Exec Commands
External Automation Tools
Stopping a Program
Running Part of a Program
What is a Program?
A program is simply a text file containing EViews commands. It is not an EViews object in a workfile. It exists as a file on your computer hard disk, generally with a “.PRG” extension.
Creating a Program
To create a new program, click File/New/Program. You will see a standard text editing window where you can type in the lines of the program. You may also open the program window by typing program in the command window, followed by an optional program name. For example
program firstprg
opens a program window named “FIRSTPRG”. Program names should follow standard EViews rules for file names.
Program Formatting
As noted earlier, an EViews program is a text file, consisting of one or more lines of text. Generally, each line of a program corresponds to a single EViews command, so you may enter the text for each command and terminate the line by pressing the ENTER key.
If a program line is longer than the current program window, EViews will, by default, autowrap the text of the line. Autowrapping alters the appearance of the program line by displaying it on multiple lines, but does not change the contents of the line. While resizing the window will change the autowrap position, the text remains unchanged and is still contained in a single line. You may turn off autowrapping in programs via Options/General Options/Programs and deselecting the Enable word wrap check box, or by clicking the Wrap +/- button on the program window.
When autowrapping is turned off via the option menu, you may elect to show program line numbers in your program window by selecting Display line numbers. You may then right-click anywhere in your program and select Go To Line... to jump directly to a specific line number.
If you desire greater control over the appearance of your lines, you can manually break long lines using the ENTER key, and then use the underscore continuation character “_” as the last character on the line to join the multiple lines. For example, the three separate lines of text
equation eq1.ls _
y x c _
ar(1) ar(2)
are equivalent to the single line
equation eq1.ls y x c ar(1) ar(2)
formed by joining the lines at the continuation character. We emphasize that the “_” must be the very last character in the line.
The apostrophe “'” is the comment character in programs. You may place the comment character anywhere in a line to treat all remaining characters in the line as a comment which will be ignored when executing the program command.
equation eq1.ls y x c ar(1) ar(2) ’ this is a comment
A block of lines may be commented or uncommented in the EViews program file editor by highlighting the lines, right-mouse clicking, and selecting Comment Selection or Uncomment Selection.
You can instruct EViews to automatically format your program by selecting the lines to which you wish to apply formatting, right-mouse clicking and selecting Format Selection. Automatic formatting will clean up the text in the selection, and will highlight the structure of the program by indenting lines of code inside loops, if conditions and subroutines, etc. You should find that automatic formatting makes your programs easier to read and edit.
You may elect to have EViews automatically indent your program as you type by changing the Indent option in the main EViews options menu (Options/General Options/Programs).
Saving a Program
After you have created and edited your program, you will probably want to save it. Press the Save or SaveAs button on the program window toolbar. When saved, the program will have the extension “.PRG”.
If saving your program will overwrite an existing program file and you have instructed EViews to make backup copies (“Programs”), EViews will automatically create the backup. The backup copy will have the same name as the file, but with the first character in the extension changed to “~”.
Snapshots for Programs
Managing snapshots for an EViews program is similar to workfiles (“Snapshot Backups”). The program window has a Snapshot button to perform a manual snapshot.
Viewing Snapshots for Programs
To view available snapshots for a program, right-click in the program window and select Snapshots…
Clicking on a previous snapshot will display a text compare view that highlights any differences between the two versions.
You can also right-click any snapshot and select Open… (or simply double-click the node) to open the snapshot in its own program window.
Note that automatic snapshots are suspended when an EViews program is actively running.
Saving the Command Window
One convenient method of creating a program is to execute several commands using the EViews command window and then save the history of those commands to a program file. Click in the command window then select File/Save As... from the main EViews menu. EViews will prompt you to save the command log as a text file. Simply save the file with the “.PRG” extension.
You may then edit the program file and save the edited version in the usual fashion.
Encrypting a Program
EViews offers you the option of encrypting a program file so that you may distribute it to others in a form where they may not view the original text. Encrypted files may be opened and the program lines may be executed, but the source lines may not be viewed. To encrypt a program file simply click on the Encrypt button on the program window.
EViews will create an untitled program containing the contents of the original program, but with only the visible text “Encrypted program”. You may save this encrypted program in the usual fashion using Save or SaveAs.
Note that once a program is encrypted it may not be unencrypted; encryption should not be viewed as a method of password protecting a program. You should always keep a separate copy of the original source program. To use an encrypted program, simply open it and run the program in the usual fashion.
Opening a Program
To load a program file previously saved on disk, click on File/Open/Program..., navigate to the appropriate directory, and click on the desired name. You may also drag an EViews program file onto the EViews window to open it. Alternatively, from the command line, you may type the keyword open followed by the full program name, including the file extension “.PRG”. By default, EViews will look for the program in the default directory, but you may include the full path to the file and enclosed the entire name in quotations, if desired. For example, the commands:
open mysp500.prg
open "c:\mywork is here\eviews\myhouse.prg"
open the file “Mysp500.PRG” in the default EViews directory, and “Myhouse.PRG located in the directory “c:\mywork is here\eviews.
Executing a Program
Executing a program is the process of running all of the commands in a program file.
Note that EViews commands can be executed in two distinct ways. When you enter and run, line by line, a series of commands in the command window, we say that you are working in interactive mode. Alternatively, you can type all of the commands in a program and execute or run them collectively as a batch of commands. When you run the commands from a program file, we say that you are in (non-interactive) program mode or batch mode.
There are several ways to execute an EViews program:
The easiest method is by pushing the Run button on an open program window and entering settings in the Run Program dialog.
Alternately, you may use the run or exec command to execute a program.
You may use external automation tools to run EViews and execute a program from outside of the EViews application environment.
You may select a set of subset of lines to execute and run the selected lines.
The Run Program Dialog
To run a program from a dialog, click on the Run button on the program window. The Run dialog opens.
The default run options are taken from the global settings (“Programs”), but may be overridden on a one-time basis using the dialog settings or command options. For purposes of discussion, we will assume that the global options are set to their out-of-the-box values.
The Program name or path edit field will show the name and in some cases, path, of the program you are running. You may enter a file specification to instruct EViews to look for a particular program.
The Program arguments edit field is used to define special string variables that will be passed to your program when it is running. See “Program Arguments” for details.
The Runtime errors section allows you to modify the behavior of the program when it encounters an error. By default, when EViews encounters an error, it will immediately terminate the program and display a message. If you enter a number into the Maximum errors before halting field, EViews will, if possible, continue to execute the program until the maximum number of errors is reached. If it encounters a serious error that makes it impossible to continue, EViews will halt the program even if the maximum number of errors is not reached. See “Execution Errors”.
If the Compatibility section checkbox labeled Version 4 compatible variable substitution and program boolean comparisons is selected, EViews will use the variable substitution behavior found in EViews 4 and earlier. To support the use of alpha series, EViews 5 and subsequent versions altered the way that % substitution variables are evaluated in expressions. To return to EViews 4 compatible rules for substitution, you may either use this checkbox or include a “MODE VER4” statement in your program. See “Version 4 Compatibility Notes” and “Program Modes” for additional discussion.
Lastly, you may select the Save options as default checkbox to update your global options with the specified settings. Alternately, you may change the global options from the Options/General Options.../Programs/General dialog.
The Run and Exec Commands
You may use the run command to execute a program from the EViews command window. Simply enter the keyword run along with any options, followed by a program file specification and any arguments (see run).
You may run a program by entering the run command, followed by the name of the program file:
run mysp500
or, using an explicit path,
run c:\eviews\myprog arg1 arg2 arg3
Note that use of the “.PRG” extension is not required since EViews will automatically append one to your specification.
The default run options will be taken from the global settings (“Programs” ), but may be overridden using the command options. For example, you may use the “v” or “verbose”options to run the program in verbose mode, and the “q” or “quiet” options to run the program in quiet mode. If you include a number as an option, EViews will use that number to indicate the maximum number of errors encountered before execution is halted:
run(v, 500) mysp500
or
run(q, ver4) progarg
Alternatively, you may modify your program to include statements for quiet or verbose mode, and statements to specify version compatibility. For example, to return to EViews 4 compatible rules for substitution, you may use the “ver4” option or include a “MODE VER4” statement in your program. See “Version 4 Compatibility Notes” and “Program Modes” for additional discussion.
You may provide a list of program arguments after the name of the program. These arguments will be passed on to the program as %0, %1 etc. See “Program Arguments” for more details.
Program options may be passed on to the program by entering them, surrounded by parenthesis immediately after the name of the program. See “Program Options” for details.
For example:
run myprog(opt1, opt2, opt3=k) arg0 arg1 arg1
will run the program MYPROG passing on the options OPT1, OPT2, OPT3=k as options, and ARG0, ARG1 and ARG1 as arguments (%0, %1 and %2 respectively).
You may have launch EViews and run a program automatically on startup by choosing File/Run from the menu bar of the Windows Program Manager or Start/Run in Windows and then typing “eviews”, followed by the name of the program and the values of any arguments. If the program has as its last line the command exit, EViews will close following the execution of the program.
The exec command is similar to the run command. It can also be used to execute a program file. The main differences between run and exec commands are the default directory they will run from, and the behavior when returning from executing a program. For more details see “Multiple Program Files”.
External Automation Tools
Lastly, you may use external automation tools to run EViews and execute a program from outside of the EViews application environment. In particular, EViews may be used as a COM Automation server so that an external program or script may launch and control EViews programmatically. See “EViews COM Automation Server” and the EViews COM Automation Server whitepaper, available from our website www.eviews.com/download/download.html, for additional discussion.
Stopping a Program
Pressing the ESC or F1 keys halts execution of a program. It may take a few seconds for EViews to respond to the halt command.
Programs will also stop when they encounter a stop command, when they reach the maximum number of errors, when they encounter a non-recoverable error in a program statement, or when they finish processing a file that has been executed via a run statement.
If you include the exit keyword in your program, the EViews application will close.
Running Part of a Program
You may choose to only run part of your program by highlight the lines you wish to run, then right-clicking and selecting Run Selected. EViews will then execute only the selected line of code as a new program.
You should note that there are potential pitfalls when using Run Selected. The first is that no arguments or options can be passed into the selected lines of code. If the selected lines rely on program arguments or options, they will fail to execute properly. Similarly, any program variables (“Program Variables”) declared outside of the selected lines may not be initialized properly when the selected lines are run. Finally, any subroutines declared outside of the selected lines of code cannot be called, since they do not exist in the selected code.
Alternately, you may use add the stop command to your EViews program to halt execution at a particular place in the program file.