Log in / Register
Home arrow Economics arrow Financial Econometrics
< Prev   CONTENTS   Next >

1.4.1. Program Variables

Control variables are variables that you can use in place of numerical values in your EViews programs. Once a control variable is assigned a value, you can use it anywhere in a program that you would normally use a number. The name of a control variable starts with an "!" mark. After the "!", the name should be a legal EViews name of 15 characters or fewer. Examples of control variable names are: !q !1 !time

You do not need to declare control variables before your refer to them, though you must assign them a value before use. Control variables are assigned in the usual way, with the control variable name on the left of an " = " sign and a numerical value or expression on the right. For example:

Once assigned a value, a control variable may appear in an expression. For example:

Control variables are automatically deleted after a program finishes. As a result, control variables are not saved when you save the workfile. You can save the values of control variables by creating new EViews objects which contain the values of the control variable. For example, the following command:

saves the numeric value assigned to the control variables !q into a scalar object number x.

A string variable is a variable whose value is a string of text. A string expression or string is text enclosed in double quotes:

String variables, which only exist during the time that your program is executing, have names that begin with a "%" symbol. The following lines assign values to string variables:

You may use strings variables to build up command text, variable names, or other string values. EViews provides a number of operators and functions for manipulating strings. Once assigned a value, a string variable may appear in any expression in place of the underlying string. Here is a quick example where we use string operations to concatenate the contents of three string variables.

In this example %ST3 is set to the value "USD/GBP cumulative returns". String variables can be assigned to the table object for the output:

which is equivalent to entering the command

You can use a string variable to refer to a command, or a name, or portion of names indirectly. Suppose, for example, that we assign the string variable

If you enclose a string variable in curly braces ("" and "") EViews will replace the expression with the name or name fragment given by the string value. In this context we refer to the expression " %x" as a replacement variable since the string variable %x is replaced in the command line by the name or names of objects to which the string refers. For example, the program line

would be interpreted by EViews as

Changing the contents of %x to "usdjpy" changes the interpretation of the original line to

since the replacement variable uses the name obtained from the new %x.

Program arguments are special string variables that are passed to your program when you run the program. Arguments allow you to change the value of string variables every time you run the program. You may use them in any context where a string variable is appropriate. Program arguments will be named %0, %1, %2, and so on. When you run a program that takes arguments, you will also supply the values for the arguments. If you use the Run button or File/Run, you will see a dialog box where you can type in the values of the arguments. If you use the run command, you should list the arguments consecutively after the name of the program. For example, suppose we have a program named RETS containing a command

To run RETS from the command line with

This program creates a time series returns using the usdgbp exchange rate defined or loaded previously in your workfile.

Alternatively, you can run this program by clicking on the Run button on the program window, or selecting File/Run. In the Run Program dialog box that appears, type the name of the program in the Program name or path field and enter the values of the arguments in the Program arguments field. Any arguments in your program that are not initialized in the run command or Run Program dialog are treated as blanks.

IF Statements

There are many situations where you want to execute commands only if some condition is satisfied. EViews uses IF and ENDIF, or IF, ELSE, and ENDIF statements to indicate the condition to be met and the commands to be executed. An IF statement starts with the if keyword, followed by an expression for the condition, and then the word then. You may use AND/OR statements in the condition, using parentheses to group parts of the statement as necessary. If the expression is TRUE, all of the commands until the matching end if are executed. If the expression is FALSE, all of these commands are skipped. For example:

The FOR Loop

The for loop allows you to repeat a set of commands for different values of a control or string variable. The FOR loop begins with a for statement and ends with a next statement. Any number of commands may appear between these two statements. The syntax of the FOR statement differs depending upon whether it uses control variables or string variables.

FOR Loops with Control Variables To repeat statements for different values of a control variable, the for statement involves setting a control variable equal to an initial value, followed by the word to, and then an ending value. After the ending value you may include the word step followed by a number indicating by how much to change the control variable each time the loop is executed. If you do not include step, the step is assumed to be 1. For example,

The for loop is executed first for the initial value, unless that value is already beyond the terminal value. After it is executed for the initial value, the control variable is incremented by step and EViews compares the variable to the limit. If the limit is passed, execution stops.

One important use of FOR loops with control variables is to change the sample. If you add a control variable to a date in a smpl command, you will get a new date as many observations forward as the current value of the control variable. Here is a FOR loop that gradually increases the size of the sample and computes an average returns:

One other important case where you will use loops with control variables is in accessing elements of a series or matrix objects. For example,

computes the cumulative sum of the elements in the vector vecl and saves it in the vector cusuml. To access an individual element of a series, you will need to use the @elem function and @otod to get the desired element

The @otod function returns the date associated with the observation index (counting from the beginning of the workfile), and the @elem function extracts the series element associated with a given date.

You can nest for loops to contain loops within loops. The entire inner for loop is executed for each successive value of the outer for loop. For example:

FOR Loops with String Variables When you wish to repeat statements for different values of a string variable, you can use the FOR loop to let a string variable range over a list of string values. Give the name of the string variable followed by the list of values. For example,

creates the returns series of two exchange rates

You can put multiple string variables in the same for statement - EViews will process the strings in sets.

For example:

In this case, the elements of the list are taken in groups of three. The loop is executed two times for the different sample pairs:

The WHILE Loop In some cases, we wish to repeat a series of commands several times, but only while one or more conditions are satisfied. Like the FOR loop, the WHILE loop allows you to repeat commands, but the WHILE loop provides greater flexibility in specifying the required conditions. The WHILE loop begins with a while statement and ends with a wend statement. Any number of commands may appear between the two statements. WHILE loops can be nested. The WHILE statement consists of the while keyword followed by an expression involving a control variable. The expression should have a logical (true or false) value or a numerical value. In the latter case, zero is considered false and any non-zero value is considered true. If the expression is true, the subsequent statements, up to the matching wend, will be executed, and then the procedure is repeated. If the condition is false, EViews will skip the following commands and continue on with the rest of the program following the wend statement. For example:

Unlike a FOR statement, the WHILE statement does not update the control variable used in the test condition. You need to explicitly include a statement inside the loop that changes the control variable, or your loop will never terminate. Use the F1 key to break out of a program which is in an infinite loop.

Subroutines A subroutine is a collection of commands that allows you to perform a given task repeatedly, with minor variations, without actually duplicating the commands. You can also use subroutines from one program to perform the same task in other programs. A subroutine starts with the keyword subroutine followed by the name of the routine and any arguments, and ends with the keyword end sub. Any number of commands can appear in between. The simplest type of subroutine has the following form:

where the keyword subroutine is followed only by the name of the routine. This subroutine has no arguments so that it will behave identically every time it is used. It creates the log-return time series from the existing price levels price.

You can use the return command to force EViews to exit from the subroutine at any time. A common use of return is to exit from the subroutine if an unanticipated error is detected.

To define a subroutine with arguments, you start with subroutine, followed by the subroutine name, a left parenthesis, the arguments separated by commas, and finally a right parenthesis. Each argument is specified by listing a type of EViews object, followed by the name of the argument. Control variables may be passed by the scalar type and string variables by the string type. For example:

This subroutine generalizes the example subroutine RETS. Calling RETS1 will fill the series given by the argument R with the log-returns of frequency LG from the series P. So if you set R equal to RETURNS, P equal to PRICES, and LG equal to 1, you will get the equivalent of the subroutine RETS above.

Subroutine call Your subroutine definitions should be placed, in any order, at the beginning of your program. The subroutines are executed by the program using a call statement. For example:

Execution of this program begins with the load statement. The subroutine definition is executed only at the last line when it is "called". Subroutines may call each other, or even call themselves. Alternatively, you may wish to place frequently used subroutines in a separate program file and use an include statement to insert them at the beginning of your program. If, for example, you put the subroutine lines in the file RETURNS.PRG, then you may put the line:

include returns

at the top of any other program that needs to call RETS or RETS1. You can use the subroutines in these programs as though they were built-in parts of the EViews programming language.

If a subroutine has got arguments, it is executed by using the call keyword call which follows by the name of the subroutine and a list of any argument values you wish to use, enclosed in parentheses and separated by commas. All arguments must be provided in the same order as in the declaration statement. For example:

include retsl

load mywork

fetch z price

series returns

call rets1(returns, price, 3)

Subroutines work with variables and objects that are either global or local. Global variables refer either to objects which exist in the workfile when the subroutine is called, or to the objects that are created in the workfile by a subroutine. Global variables remain in the workfile when the subroutine finishes. A local variable is one that has meaning only within the subroutine. Local variables are deleted from the workfile once a subroutine finishes.

Global objects may be used and updated directly from within the subroutine. If, however, a global object has the same name as an argument in a subroutine, the variable name will refer to the argument and not to the global variable.

Local Subroutines All objects created by a global subroutine will be global and will remain in the workfile upon exit from the subroutine. If you include the word local in the definition of the subroutine, you create a local subroutine. All objects created by a local subroutine will be local and will be removed from the workfile upon exit from the subroutine. Local subroutines are most useful when you wish to write a subroutine which creates many temporary objects that you do not want to keep. You may not use or update global objects directly from within the subroutine. The global objects corresponding to arguments may be used and updated by referring to the arguments. All other objects in the subroutine are local and will be deleted when the subroutine finishes. If you want to save results from a local subroutine, you have to explicitly include them in the arguments.

Local subroutines can call global subroutines and vice versa. The global subroutine will only have access to the global variables, and the local subroutine will only have access to the local variables, unless information is passed between the routines via arguments.

Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
Business & Finance
Computer Science
Language & Literature
Political science