Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Eviews5 / EViews5 / Docs / EViews 5 Command Ref.pdf
Скачиваний:
91
Добавлен:
23.03.2015
Размер:
5.23 Mб
Скачать

88—Chapter 6. EViews Programming

Since most of these two programs are identical, it seems like a lot of typing to make two separate programs. In “Program Arguments” on page 98 we show you a method for handling these two forecasting problems with a single program. First however, we must define the notion of variables that exist solely when running programs.

Program Variables

While you can use programs just to edit, run, and re-run collections of EViews commands, the real power of the programming language comes from the use of program variables and program control statements.

Control 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:

!x

!1

!counter

You need not 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:

!x = 7

!12345 = 0

!counter = 12

!pi = 3.14159

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

!counter = !counter + 1

genr dnorm = 1/sqr(2*!pi)*exp(-1/2*epsilon^2)

scalar stdx = x/sqr(!varx)

smpl 1950q1+!i 1960q4+!i

Control variables do not exist outside of your program and are automatically erased 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.

Program Variables—89

For example, the following commands:

scalar stdx = sqr(!varx)

c(100) = !length

sample years 1960+!z 1990

use the numeric values assigned to the control variables !VARX, !LENGTH, and !Z.

String Variables

A string expression or string is text enclosed in double quotes:

"gross domestic product"

"3.14159"

"ar(1) ar(2) ma(1) ma(2)"

A string variable is a variable whose value is a string of text. String variables, which may only exist during the time that your program is executing, have names that begin with a “%” symbol. String variables are assigned by putting the string variable name on the left of an “=” sign and a string expression on the right. For example, the following lines assign values to string variables:

%value = "value in millions of u.s. dollars" %armas = "ar(1) ar(2) ma(1) ma(2)"

%mysample = " 83m1 96m12" %dep = " hs"

%pi = " 3.14159"

You may use strings variables to help you build up command text, variable names, or other string values. EViews provides a number of operators and functions for manipulating strings; a complete list is provided in “Strings” on page 119 of the User’s Guide.

Once assigned a value, a string variable may appear in any expression in place of the underlying string. When substituted for, the string variable will be replaced by the contents of the string variable, enclosed in double quotes.

Here is a quick example where we use string operations to concatenate the contents of three string variables.

!repeat = 500

%st1 = " draws from the normal"

%st2 = "Cauchy "

%st3 = @str(!repeat) + @left(%st1,16) + %st2 + "distribution"

90—Chapter 6. EViews Programming

In this example %ST3 is set to the value “500 draws from the Cauchy distribution”. Note the spaces before “draws” and after “Cauchy” in the string variable assignments. After string variable substitution, the latter assignment is equivalent to entering

%st3 = "500" + " draws from the " + "Cauchy " + "distribution"

Similarly, the table assignment statement

table1(1,1) = %st3

is equivalent to entering the command

table(1,1) = "500 draws from the Cauchy distribution"

One important usage of string variables is in assigning string values to alpha series. For example, we may have the assignment statement

%z = "Ralph"

alpha full_name = %z + last_name

which is equivalent to the expression

alpha full_name = "Ralph" + last_name

We again emphasize that string variable substitution involves replacing the string variable by its string value contents enclosed in double quotes.

As with any string value, you may convert a string variable containing a number into a number by using the @val function. For example,

%str = ".05"

!level = @val(%str)

creates a control variable !LEVEL=0.05. If the first character of the string is not a numeric character (and is not a plus or a minus sign), @val returns the value “NA”. Any characters to the right of the first non-digit character are ignored. For example,

%date = "04/23/97"

scalar day = @val(@right(%date,5))

scalar month = @val(%date)

creates scalar objects DAY=23 and MONTH=4.

Full details on working with strings are provided in “Strings” on page 119 in the User’s Guide.

Program Variables—91

Replacement Variables

When working with EViews commands, you may wish to use a string variable, not simply to refer to a string value, but as an indirect way of referring to something else, perhaps a command, or a name, or portion of names for one or more underlying items.

Suppose, for example, that we assign the string variable %X the value “GDP”:

%x = "gdp"

We may be interested, not in the actual string value “gdp”, but rather in indirectly referring to an underlying object named “GDP”. For example, the series declaration

series %x = 300.2

will generate an error since the substituted expression is

series "gdp" = 300.2

and the series declaration series expects the name of a series, not a string value. In this circumstance, we would like to replace the string variable with the underlying name held in the string.

If you enclose a string variable in curly braces (“{“ and “}”) EViews will replace the expression with the name, names, 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

equation eq1.ls {%x} c {%x}(-1)

would be interpreted by EViews as

equation eq1.ls gdp c gdp(-1)

Changing the contents of %X to “M1” changes the interpretation of the original line to

equation eq1.ls m1 c m1(-1)

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

Similarly, when trying to find the number of valid (non-missing) observations in a series named INCOME, you may use the @obs function along with the name of the series:

@obs(income)

If you wish to use a string variable %VAR to refer to the INCOME series, you must use the replacement variable in the @OBS function, as in

%var = "income"

92—Chapter 6. EViews Programming

@obs({%var})

since you wish to refer indirectly to the object named in %VAR. Note that the expression

@obs(%var)

will return an error since @OBS requires a series or matrix object name as an argument.

Any string variable may be used as the basis of a replacement variable. Simply form your string using one or more string operations

%object

= "group"

%space = " "

%reg1 =

"gender"

%reg2 =

"income"

%reg3 =

"age"

%regs =

%reg1 + %space + %reg2 + %space + %reg3

then enclose the string variable in braces. In the expression,

{%object} g1 {%regs}

EViews will substitute the names found in %OBJECT and %REGS so that the resulting command is

group g1 gender income age

It is worth noting that replacement variables may be used as building blocks to form object names. For example, the commands

%b = "2"

%c = "temp"

series z{%b}

matrix(2, 2) x{%b}

vector(3) x_{%c}_y

declare a series named Z2, a 2 × 2 matrix named X2, and a vector named X_TEMP_Y.

Up until now we have focused on replacement variables formed from string variables. Note however, that control variables may also be used as replacement variables. For example, the commands

!i = 1

series y{!x} = nrnd

!j = 0

series y{!j}{!i} = nrnd

Program Variables—93

are equivalent to

series y1 = nrnd

series y01 = nrnd

and will create two series Y1 and Y01 that contain a set of (pseudo-)random draws from a standard normal distribution. Note that in cases where there is no possibility of ambiguity, EViews will treat a control variable as a replacement variable, even if the braces are not provided. For example:

!x = 3

series y!x = 3

will generate the series Y3 containing the value 3.

Replacement variables provide you with great flexibility in naming objects in your programs. We caution, however, that unless you take some care, they may cause considerable confusion. We suggest, for example, that you avoid using the same base names to refer to different objects. Consider the following program:

' possibly confusing commands (avoid)

!a = 1

series x{!a}

!a = 2

matrix x{!a}

In this small code snippet it is easy to see that X1 is the series and X2 is the matrix. But in a more complicated program, where the control variable assignment !A=1 may be separated from the declaration by many program lines, it may be difficult to tell at a glance what kind of object X1 represents. A better approach might be to use different names for different kinds of variables:

!a = 1

series ser{!a}

!a = 2

matrix mat{!a}

so that the replacement variable names are more apparent from casual examination of the program.

Version 5 Compatibility Notes

While the underlying concepts behind string and replacement variables have not been changed since the first version EViews, beginning in EViews 5 there are three important changes in the implementation of these concepts. In addition, there has been an important

94—Chapter 6. EViews Programming

change in the handling of boolean comparisons involving numeric NA values, and blank string values.

String vs. Replacement Variables

First, the use of contextual information to distinguish between the use of string and replacement variables has been eliminated.

Previously, the underlying notion that the expression “%X” refers exclusively to the string variable %X while the expression “{%X}” refers to the corresponding replacement variable was modified slightly to fit the context in which the expression was used. In earlier versions of EViews, the string variable expression “%X” was treated as a string variable in cases where a string was expected, but was treated as a replacement variable in other settings.

For example, suppose that we have the string variables:

%y = "cons"

%x = "income"

When used in settings where a string is expected, all versions of EViews treat %X and %Y as string variables. Thus, in table assignment, the command,

table1(2, 3) = %x + " " + %y

is equivalent to the expression,

table1(2, 3) = "cons" + " " + "income"

However, when string variables were used in other settings, earlier versions of EViews used the context to determine that the string variable should be treated as a replacement variable; for example, the three commands

equation eq1.ls %y c %x

equation eq1.ls {%y} c {%x}

equation eq1.ls cons c income

were all equivalent. Strictly speaking, the first command should have generated an error since string variable substitution would replace %Y with the double-quote delimited string “cons” and %X with the string “income”, as in

equation eq1.ls "cons" c "income"

Instead, earlier versions of EViews determined that the only valid interpretation of %Y and %X in the first command was as replacement variables so EViews simply substitutes names for %Y and %X.

Similarly, the commands

Program Variables—95

genr %y = %x

genr {%y} = {%x}

genr cons = income

all yielded the same result, since %Y and %X were treated as replacement variables, not as string variables.

This contextual interpretation of string variables was convenient since, as seen from the examples above, it meant that users rarely needed to use braces around string variables. The EViews 5 introduction of alphanumeric series meant, however, that the existing interpretation of string variables was no longer valid. The following example clearly shows the problem:

alpha parent = "mother"

%x = "parent"

alpha temp = %x

Note that in the final assignment statement, the command context alone is not sufficient to determine whether %X should refer to the string variable value “parent” or to the replacement variable (alpha series) PARENT containing the string “mother”.

In the EViews 5 interpretation of string and replacement variables, users must now always use the expression “{%X}” to refer to the substitution variable corresponding to %X so that the final line above resolves to the command

alpha temp = "parent"

To interpret the line as a replacement variable, you must enter

alpha temp = {%x}

which resolves to the command

alpha temp = parent

Under an EViews 4 and earlier interpretation of the final line, the braces would not be necessary since “%X” would be treated as a replacement variable.

String Variables in String Expressions

The second major change in EViews 5 is that all text in a string expression is treated as a literal string. The important implication of this rule is that string variable text is no longer substituted for inside of a string expression.

Consider the assignment statements

%b = "mom!"

%a = "hi %b"

96—Chapter 6. EViews Programming

table(1, 2) = %a

In EViews 4 and earlier, the “%B” text in the string expression was treated as a string variable, not as literal text. Accordingly, the string variable %A contains the text “hi mom!”. One consequence of this approach was that there was no way to get the literal text of the form “%B” into a string using a program.

Beginning in EViews 5, the “%B” in the second string variable assignment is treated as literal text. The string variable %A will contain the text “hi %b”. Obtaining a %A that contains the EViews 4 result is straightforward. Simply move the first string variable %B outside of the string expression, and use the string concatenation operator:

%a = "hi " + %b

assigns the text “hi mom!” to the string variable %A.

Case-Sensitive String Comparison

In previous versions of EViews, program statements could involve string comparisons. For example, you might use an if-statement to compare a string variable to a specific value, or you could use a string comparison to assign a boolean value to a cell in a matrix or to a numeric series. In all of these settings, the string comparisons were performed caselessly, so that the string “Abc” was viewed as equal to “ABC” and “abc”.

The introduction of mixed case alpha series in EViews 5 means that caseless string comparisons are no longer generally supportable. Accordingly, the earlier behavior has been changed so that all EViews 5 string comparisons are now case-senstive.

Programs may be run in version 4 compatibility mode to enable caseless comparisons for element operations. For example, the if-statement comparison:

if (%x = "abc") then

will be performed caselessly in compatibility mode.

Note that compatibility mode does not apply to string comparisons that assign values into an entire EViews numeric or alpha series. Thus, even in compatibility mode, the statement:

series y = (alphaser = "abc")

will be evaluated using case-sensitive comparisons for each value of ALPHASER.

Comparisons Involving NAs/Missing Values

In EViews 4.1 and earlier versions, NA values were always treated as ordinary values for purposes of numeric equality (“=”) and inequality (“<>”) testing. In addition, when performing string comparisons in earlier versions of EViews, empty strings were treated as ordinary blank strings and not as a missing value. In these versions of EViews, the comparison operators (“=” and “<>”) always returned a 0 or a 1.

Соседние файлы в папке Docs