Command Reference : EViews Programming : Program Variables
  
Program Variables
 
Control Variables
String Variables
Replacement Variables
While you can use programs just to run, and re-run collections of EViews commands, the real power of the EViews programming language comes from the use of program variables and program control statements.
Program variables are variables that you may use in place of numeric or string values in your EViews programs. Accordingly, there are two basic types of program variables: control (numeric) variables and string variables.
In the remainder of this section we describe the use of these two types of program variables.
Control Variables
Control variables are program variables that you can use in place of numeric 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.
It is important to note that control variables do not exist outside of your program and are automatically deleted after a program finishes. In this regard, control variables should not be confused with scalar objects as the former 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.
The name of a control variable starts with an “!” character. After the “!”, the name should be a legal EViews name of 23 characters or fewer (leading numbers are allowed). Examples of control variable names are:
!x
!1
!counter
You need not declare control variables, but you must assign them values before use. Control variable values 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
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.
It is important to note that control variables are used in programs via text substitution. Whenever EViews encounters a control variable in a program it substitutes the text value of that variable into the program. One of the implications of the text substitution is that you may lose some numeric accuracy when using a program variable due to the conversion in to and out of a text representation of the number.
A second unintended consequence of text substitution can arise when raising a negative control variable to a power:
!a = -3
!b = !a^2
When evaluating these lines, EViews will substitute the value of !A in the second line, leaving the line:
!b = -3^2
Which it then evaluates as –9 (since the square operator takes precedence over the negation operator), rather than the expected 9.
You should also take care to avoid inadvertent replacement of program variables, as outlined in “Replacement Variables”.
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 program variable is a program variable whose value is a string of text. String variables, which exist only during the time that your program is executing, have names that begin with a “%” symbol. String variables should not be confused with “String Objects” which are objects that exist in a workfile.
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”.
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 simple 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"
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 use for string variables is assigning string values to string objects, string vectors, or 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 the text representation of 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 string cannot be evaluated to a number, @val returns the value “NA”.
String variables are closely related to the concept of a string object ( “String Objects”). A string object is an EViews workfile object that holds a string:
string a = "Hello World"
string b = """First Name"" Middle ""Last Name"""
Unlike string variables, string objects are named objects in the workfile that may exist apart from a running program.
In programming applications, string variables and string objects are sometimes defined using string literal expressions as illustrated above. However, in most settings, string variables and objects are defined using results from string functions, or by manipulation of string lists and string vectors. For example, you could use the @wlookup and @wsplit functions to obtain a set of strings corresponding to the names of all of the series in a workfile. See “Strings” for a full discussion.
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 an object name, or portion of names for one or more items.
Suppose, for example, that we assign the string variable %X the value “GDP”:
%x = "gdp"
We may be interested, however, not in the actual string variable value “gdp”, but rather in an EViews object named “GDP”. Accordingly, we require a method of distinguishing between a string value and the item corresponding to the string value.
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.
Suppose we want to create the series Y and set it equal to the values in the series GDP. Given the string variable %X defined above, the series declaration,
series y = %x
creates a numeric series Y and sets it equal to the string value “gdp”,
series y = "gdp"
which generates an error since the series declaration expects the name of a series or a numeric value, not a string value. In this circumstance, we would like to replace the string value with the name of an object. With the replacement variable “{%X}”, the command
series y = {%x}
is properly interpreted as
series y = gdp
Similarly, the program line using replacement variables,
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 program line to
equation eq1.ls m1 c m1(-1)
since the replacement variable uses the name obtained from the new value of %X.
To take another example, 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"
@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 matrix named X2, and a vector named X_TEMP_Y.
Up to this point, we have focused on replacement variables formed from string variables. However, control variables may also be used to form replacement variables. For example, the commands
!i = 1
series y{!i} = nrnd
!j = 0
series y{!j}{!i} = nrnd
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. Conveniently, 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 = 4
will generate the series Y3 containing the value 4.
While convenient, this loose interpretation of control variables can, however, lead to unexpected results if one is not careful. Take the following program:
!x1 = 10
series y = !x1
This program will create a series equal to the value of !X1 (i.e. 10). However if you were to mis-type the program slightly:
!x = 10
series y = !x1
where the first line has !X rather than !X1, EViews will not generate an error due to the missing !X1 variable, but will instead evaluate the second line by substituting !X into the expression, and evaluating the result as 101.
Replacement variables may be constructed using nested evaluation of replacement variables. Suppose we have the strings:
%str1 = "x"
%name = "%str1"
Then we can declare the series X and fill it with random normals using the command
series {{%name}} = nrnd
After evaluation of the innermost replacement variable, the expression reduces to
series {%str1} = nrnd
and then to
series x = nrnd
when we evaluate the remaining brace. The double braces allow us perform a double replacement in which the string variable used to form a replacement variable is itself obtained as a replacement variable.
The double nested braces need not be adjacent. For example:
%x1 = "x"
%x2 = "y"
scalar x = 10
scalar y = 20
!y = 1
scalar r1 = {%x{!y}}
!y = 2
scalar r2 = {%x{!y}}
First we create two string variables, %X1 and %X2, and three scalar objects, X, Y, and R. First, the control variable is !Y is set to 1 and the replacement variable {!Y} is used to construct the name “%X1” of a string variable. The resulting replacement variable {%X1} refers to the scalar object X. We assign the scalar X value of 10 to the scalar R1. Next, we set !Y to 2, the replacement variable {%X{!Y}} evaluates to Y, and we assign the Y value of 20 to the scalar R2.
Multiple sets of braces may be used to create various string names:
string uslabel = "USA"
string nzlabel = "New Zealand"
%a1 = "US"
%a2 = "NZ"
%b = "LABEL"
!y = 1
string label1 = {%a{!y}}{%b}
!y = 2
string label2 = {%a{!y}}{%b}
First we create two string objects, USLABEL and NZLABEL, which hold the label we wish to assign to each country. Then we create the string variables %A1 and %A2 to hold the country abbreviations. When the control variable !Y=1, {%A{!Y}}{%B} evaluates to the object USLABEL and when !Y=2, {%A{!Y}}{%B} evaluates to the object NZLABEL. Then the string LABEL1 contains “USA” and LABEL2 contains “New Zealand”.
Replacement variables may also be formed using string objects in place of string variables. To use a string object as a replacement variable, simply surround it with curly braces (“{” and “}”). For example,
string LAGGDP = "GDP(-1) GDP(-2) GDP(-4)"
equation eq1.ls GDP C {LAGGDP}
executes the command
equation eq1.ls GDP C GDP(-1) GDP(-2) GDP(-4)
In most respects, there is no difference between using a string object or a string variable in a program. String objects have the advantage that they may be used interactively, outside of programs, while string variables may not. This can be useful when debugging a program; string variables disappear once the program has finished, making it difficult to identify problems in their construction. Note that string objects do, however, go out-of-scope when the active workfile page changes, while string variables are always in scope.
Lastly, while replacement variables provide you with great flexibility in referencing objects in your programs, used carelessly, they can lead to confusion. We suggest, for example, that you avoid using similar 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 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 meaning of replacement variable names are more apparent from casual examination of the program.