An introduction to EViews programming.
Moderators: EViews Gareth, EViews Jason, EViews Moderator, EViews Matt

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 12239
 Joined: Tue Sep 16, 2008 5:38 pm
An introduction to EViews programming.
Perhaps one of the easiest ways to learn a programming language, such as the EViews programming language, is to see examples of simple programs and work through them line by line. The following posts provide a set of easy to follow programs and a description of what each program does. The programs are split into groups, the first contains example of working with control of execution using FOR loops and IF statements, the second group contains programs using UserInterface Dialogs, and the third group provides example of using string manipulation to work with objects in the workfile.
Although each program is a standalone program, in that it can run by itself, the accompanying program descriptions assume that the user has read the descriptions in the previous programs. Copying each program into EViews will probably let you read it, especially the comment lines, better than reading it here on the forum.
Although each program is a standalone program, in that it can run by itself, the accompanying program descriptions assume that the user has read the descriptions in the previous programs. Copying each program into EViews will probably let you read it, especially the comment lines, better than reading it here on the forum.
Follow us on Twitter @IHSEViews

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 12239
 Joined: Tue Sep 16, 2008 5:38 pm
Re: An introduction to EViews programming.
FOR loops and IF statements
The following program creates some data; a Y variable and 15 X variables, and then regresses Y on a constant and each X in separate equations:
The first step is to create a workfile. The wfcreate command is used to create a quarterly workfile that runs from 1990 to 2010.
Next the data is created. A series, called Y, is created and filled with normally distributed random data using the nrnd function.
We could create the 15 X variables in the same way, one at a time, like this:
series x1=nrnd
series x2=nrnd
series x3=nrnd
…….
series x15=nrnd
However it is easier to use a simple FOR loop to do the creation for us. The loop uses control variable !i to run from 1 to 15. Each time through the loop a new series called x{!i} is created, again using the nrnd function. Note that we surround the control variable, !i, in braces when naming the series. Thus first time through the loop, !i=1, and a series called X1 is created, then !i=2 and a series called X2 is created, and so on until X15 is created. Once the loop is finished there will be 15 X series created in the workfile.
Finally we use a second FOR loop to estimate the equations. This time each iteration of the loop creates a new equation, called eq{!i}, and estimates that equation via ordinary least squares, using the LS command. The dependent variable in each equation is Y, and the independent variables are a constant and x{!i}. Thus the first time through this loop an equation called EQ1 is created, and Y is regressed upon a constant and X1. The second time through the equation is called EQ2 and Y is regressed on a constant and X2. Once the loop is finished there will be 15 equations created, and estimated, in the workfile.
The following program creates some data; a Y variable and 15 X variables, and then regresses Y on a constant and each X in separate equations:
Code: Select all
'create a workfile
wfcreate q 1990 2010
'create a y series
series y=nrnd
'create 15 X series
for !i=1 to 15
series x{!i}=nrnd
next
'run pairwise regressions between Y and each X
for !i=1 to 15
equation eq{!i}.ls y c x{!i}
next
The first step is to create a workfile. The wfcreate command is used to create a quarterly workfile that runs from 1990 to 2010.
Next the data is created. A series, called Y, is created and filled with normally distributed random data using the nrnd function.
We could create the 15 X variables in the same way, one at a time, like this:
series x1=nrnd
series x2=nrnd
series x3=nrnd
…….
series x15=nrnd
However it is easier to use a simple FOR loop to do the creation for us. The loop uses control variable !i to run from 1 to 15. Each time through the loop a new series called x{!i} is created, again using the nrnd function. Note that we surround the control variable, !i, in braces when naming the series. Thus first time through the loop, !i=1, and a series called X1 is created, then !i=2 and a series called X2 is created, and so on until X15 is created. Once the loop is finished there will be 15 X series created in the workfile.
Finally we use a second FOR loop to estimate the equations. This time each iteration of the loop creates a new equation, called eq{!i}, and estimates that equation via ordinary least squares, using the LS command. The dependent variable in each equation is Y, and the independent variables are a constant and x{!i}. Thus the first time through this loop an equation called EQ1 is created, and Y is regressed upon a constant and X1. The second time through the equation is called EQ2 and Y is regressed on a constant and X2. Once the loop is finished there will be 15 equations created, and estimated, in the workfile.
Follow us on Twitter @IHSEViews

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 12239
 Joined: Tue Sep 16, 2008 5:38 pm
Re: An introduction to EViews programming.
The second program is similar to the first, but rather than regressing Y on each X variable, it regresses each X on every other X, one at a time:
The first two parts have a similar structure to the first program – a workfile is created and 5 X variables are created in a FOR loop.
The last part of the program creates and estimates the equations. This section uses a double loop to run through the Xs twice. The outer loop runs from !i=1 to 4 (the number of Xs minus 1). The second loop runs from !j=!i +1to 5 (the number of Xs). Each equation is named EQ{!i}_{!j}.
Thus the first time through the outer loop, 4 equations are made: EQ1_2, EQ1_3, EQ1_4 and EQ1_5. Those equations regress X1 on a constant and X2, X3, X4 and X5 respectively. The second time through the outer loop, only 3 equations are made: EQ2_3, EQ2_4, EQ2_5. Those equations regress X2 on a constant and X3, X4 and X5 respectively.
Code: Select all
'create a workfile
wfcreate q 1990 2010
'create 5 X series
for !i=1 to 5
series x{!i}=nrnd
next
'run pairwise regressions between each X and every other X
for !i=1 to 4
for !j=!i+1 to 5
equation eq{!i}_{!j}.ls x{!i} c x{!j}
next
next
The first two parts have a similar structure to the first program – a workfile is created and 5 X variables are created in a FOR loop.
The last part of the program creates and estimates the equations. This section uses a double loop to run through the Xs twice. The outer loop runs from !i=1 to 4 (the number of Xs minus 1). The second loop runs from !j=!i +1to 5 (the number of Xs). Each equation is named EQ{!i}_{!j}.
Thus the first time through the outer loop, 4 equations are made: EQ1_2, EQ1_3, EQ1_4 and EQ1_5. Those equations regress X1 on a constant and X2, X3, X4 and X5 respectively. The second time through the outer loop, only 3 equations are made: EQ2_3, EQ2_4, EQ2_5. Those equations regress X2 on a constant and X3, X4 and X5 respectively.
Follow us on Twitter @IHSEViews

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 12239
 Joined: Tue Sep 16, 2008 5:38 pm
Re: An introduction to EViews programming.
Program 3 is almost the same as the second program, above, but rather than creating series via numerical program variables, a string variable is used instead:
In this program, after creating the workfile we create an empty group that will later be used to hold all of the X variables. We name this group XS.
The FOR loop that creates the series loops through the %i string variable which takes the values of “GDP”, “”UNEMP”, “INFL”, “CPI” and finally “M1”. Each iteration of the loop creates a series with a name equal to the current value of %i. The newly created series is then added to the XS group. Thus the first time through the loop a series called GDP is created, and then added to the XS group. The second time through the loop, a variable called UNEMP is created and added to the XS group.
The second FOR loop is a little more complicated than Program 2. The first thing to notice is the use of the @count group data member in the loops. The @count member returns the number of series in a group.
Thus since there are 5 series in the XS group, xs.@count will equal 5, and xs.@count1 will equal 4.
The second element of note in the second loop is the use of the @seriesname(i) data member. This data member returns the name of the ith series in the group. Thus xs.@seriesname(1) would return “GDP”.
The first time through the outer loop, %iname will be equal to “GDP” (since !i = 1, xs.@seriesname(!i) will return the name of the first series in XS). The first time through the inner loop, %jname will be equal to “UNEMP” (since !j=!i+1 = 2, xs.@seriesname(!j) will return the name of the second series). The second time through the inner loop, %jname will equal “INFL”, and so on.
At the end of the outer for loop, we will have a number of equations created:
Code: Select all
'create a workfile
wfcreate q 1990 2010
'create a group which will contain the xs
group xs
'create 5 series
for %i GDP UNEMP INFL CPI M1
series {%i}=nrnd
xs.add {%i}
next
'run pairwise regressions between each series
for !i=1 to xs.@count1
%iname = xs.@seriesname(!i)
for !j=!i+1 to xs.@count
%jname = xs.@seriesname(!j)
equation eq_{%iname}_{%jname}.ls {%iname} c {%jname}
next
next
In this program, after creating the workfile we create an empty group that will later be used to hold all of the X variables. We name this group XS.
The FOR loop that creates the series loops through the %i string variable which takes the values of “GDP”, “”UNEMP”, “INFL”, “CPI” and finally “M1”. Each iteration of the loop creates a series with a name equal to the current value of %i. The newly created series is then added to the XS group. Thus the first time through the loop a series called GDP is created, and then added to the XS group. The second time through the loop, a variable called UNEMP is created and added to the XS group.
The second FOR loop is a little more complicated than Program 2. The first thing to notice is the use of the @count group data member in the loops. The @count member returns the number of series in a group.
Thus since there are 5 series in the XS group, xs.@count will equal 5, and xs.@count1 will equal 4.
The second element of note in the second loop is the use of the @seriesname(i) data member. This data member returns the name of the ith series in the group. Thus xs.@seriesname(1) would return “GDP”.
The first time through the outer loop, %iname will be equal to “GDP” (since !i = 1, xs.@seriesname(!i) will return the name of the first series in XS). The first time through the inner loop, %jname will be equal to “UNEMP” (since !j=!i+1 = 2, xs.@seriesname(!j) will return the name of the second series). The second time through the inner loop, %jname will equal “INFL”, and so on.
At the end of the outer for loop, we will have a number of equations created:
Code: Select all
Value of !i(outer loop) Value of !j(inner loop) Resulting Equation Name
1 2 EQ_GDP_UNEMP
1 3 EQ_GDP_INFL
1 4 EQ_GDP_CPI
1 5 EQ_GDP_M1
2 3 EQ_UNEMP_INFL
2 4 EQ_UNEMP_CPI
2 5 EQ_UNEMP_M1
3 4 EQ_INFL_CPI
3 5 EQ_INFL_M1
4 5 EQ_CPI_M1
Follow us on Twitter @IHSEViews

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 12239
 Joined: Tue Sep 16, 2008 5:38 pm
Re: An introduction to EViews programming.
Often, rather than requiring the output from each equation, only some of the summary statistics are required. In this case we can store just those summary statistics, rather than store each equation:
Program D stores the Rsquared from each regression into a vector called R2S. The program creates the vector prior to the loop, and sizes it to the number of equations that will be run (10). The program also creates an empty equation prior to the FOR loop. This equation will be reused inside the loop. We also declare a program variable, !rowcounter. This counter will count how many equations we have estimated, and so which row of R2S we are on.
The FOR loop is almost identical to that in Program C. However rather than create a new named equation, we just reestimate the previously declared equation, EQ, just changing the specification of the equation each time through the loop. Then we store the Rsquared of each equation into the vector R2S at row !rowcounter. Note we use the @r2 data member of the equation to retrieve the Rsquared.
Thus the first time through the outer and inner loops, !rowcounter will equal 1, %iname will equal GDP, %jname will equal UNEMP, so that the equation will be specified as GDP C UNEMP, and the Rsquared from that equation will be stored in the first row of the vector R2S.
For a list of other statistics you can store in a similar way from an equation, view the data members section of the Equation section of the Object Reference.
If we want to store the coefficients from each equation, rather than a single summary statistic, we can do so by using a matrix to store the coefficients, rather than a vector:
This program is similar to the first program, where we regress Y against each of 15 X variables, one at a time. However this time the coefficients from each of the 15 regressions are stored in a matrix, called Coefs. Note the command colplace is used to store the column of coefficient estimates into the matrix at the appropriate column number (!i).
Code: Select all
'create a workfile
wfcreate q 1990 2010
'create a group which will contain the xs
group xs
'create 5 series
for %i GDP UNEMP INFL CPI M1
series {%i}=nrnd
xs.add {%i}
next
'create vector to store rsquares
vector(10) r2s
'create empty equation to be used inside the loop
equation eq
''counter of how many equations we have run
!rowcounter=1
'run pairwise regressions between each series
for !i=1 to xs.@count1
%iname = xs.@seriesname(!i)
for !j=!i+1 to xs.@count
%jname = xs.@seriesname(!j)
eq.ls {%iname} c {%jname}
r2s(!rowcounter) = eq.@r2
!rowcounter = !rowcounter+1
next
next
Program D stores the Rsquared from each regression into a vector called R2S. The program creates the vector prior to the loop, and sizes it to the number of equations that will be run (10). The program also creates an empty equation prior to the FOR loop. This equation will be reused inside the loop. We also declare a program variable, !rowcounter. This counter will count how many equations we have estimated, and so which row of R2S we are on.
The FOR loop is almost identical to that in Program C. However rather than create a new named equation, we just reestimate the previously declared equation, EQ, just changing the specification of the equation each time through the loop. Then we store the Rsquared of each equation into the vector R2S at row !rowcounter. Note we use the @r2 data member of the equation to retrieve the Rsquared.
Thus the first time through the outer and inner loops, !rowcounter will equal 1, %iname will equal GDP, %jname will equal UNEMP, so that the equation will be specified as GDP C UNEMP, and the Rsquared from that equation will be stored in the first row of the vector R2S.
For a list of other statistics you can store in a similar way from an equation, view the data members section of the Equation section of the Object Reference.
If we want to store the coefficients from each equation, rather than a single summary statistic, we can do so by using a matrix to store the coefficients, rather than a vector:
Code: Select all
'create a workfile
wfcreate q 1990 2010
'create a y series
series y=nrnd
'create 15 X series
for !i=1 to 15
series x{!i}=nrnd
next
'create matrix to store coefficients. We'll be running 15 regressions (so 15 columns) with two coefficients in each, so two rows
matrix(2,15) coefs
'create empty equation to be used inside the loop
equation eq
'run pairwise regressions between Y and each X
for !i=1 to 15
'estimate equation
eq.ls y c x{!i}
'store coefficients into matrix
colplace(coefs, eq.@coefs, !i)
next
This program is similar to the first program, where we regress Y against each of 15 X variables, one at a time. However this time the coefficients from each of the 15 regressions are stored in a matrix, called Coefs. Note the command colplace is used to store the column of coefficient estimates into the matrix at the appropriate column number (!i).
Follow us on Twitter @IHSEViews

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 12239
 Joined: Tue Sep 16, 2008 5:38 pm
Re: An introduction to EViews programming.
This program is similar to the previous one, but rather than recording the Rsquared from each equation, it records the results of a Wald test, testing the (possibly nonsensical) test of whether the coefficient on the constant term is equal to that on the single regressor:
Rather than declaring a vector prior to the loop, this program declares a matrix, walds, with two columns, one for the Wald statistic, and one for the associated pvalue. There are, again, 10 rows since we will be estimating 10 equations.
Unfortunately there is no data member for the results of the Wald test (in fact most procedures and views from objects do not return data members or values). Thus there is not a direct way to retrieve the Wald statistic and the pvalue from the Wald Test. However, the results of the Wald test can be frozen into a table object, then the values can be retrieved from that table.
We use the wald command to perform the Wald test, but precede it with a freeze command to freeze the results of the Wald test into a table. We name that table waldtable.
Having created the table object, waldtable, containing the results, we can access any part of those results by referring to the table cell containing the information we want. The Waldstatistic for this particular Wald test is located in the 6th row ,2nd column of the table. The associated pvalue is located in the 6th row, 4th column.
Since all numbers are stored in string form in tables, we cannot directly store the value of the Wald statistic into the storage matrix. Instead we have to use the @val function to convert the string representation of the number into an actual number that can be stored into the matrix.
Finally, having stored the values from the table object into the matrix, we delete the table, using the d command. This is to prevent a duplicate object error next time we run through the loop and create the table again.
Code: Select all
'create a workfile
wfcreate q 1990 2010
'create a group which will contain the xs
group xs
'create 5 series
for %i GDP UNEMP INFL CPI M1
series {%i}=nrnd
xs.add {%i}
next
'create matrix to store wald statistics and pvalues
matrix(10,2) Walds
'create empty equation to be used inside the loop
equation eq
''counter of how many equations we have run
!rowcounter=1
'run pairwise regressions between each series
for !i=1 to xs.@count1
%iname = xs.@seriesname(!i)
for !j=!i+1 to xs.@count
%jname = xs.@seriesname(!j)
eq.ls {%iname} c {%jname}
freeze(waldtable) eq.wald c(1)=c(2) ' perform wald test and freeze it into a table called WaldTable
walds(!rowcounter,1) = @val(waldtable(6,2)) ' store wald statistic (which is in cell 6,2 of the table)
walds(!rowcounter,2) = @val(waldtable(6,4)) ' store wald pvalue (which is in cell 6,4 of the table)
!rowcounter=!rowcounter+1 ' increment row counter
d waldtable ' delete the table
next
next
Rather than declaring a vector prior to the loop, this program declares a matrix, walds, with two columns, one for the Wald statistic, and one for the associated pvalue. There are, again, 10 rows since we will be estimating 10 equations.
Unfortunately there is no data member for the results of the Wald test (in fact most procedures and views from objects do not return data members or values). Thus there is not a direct way to retrieve the Wald statistic and the pvalue from the Wald Test. However, the results of the Wald test can be frozen into a table object, then the values can be retrieved from that table.
We use the wald command to perform the Wald test, but precede it with a freeze command to freeze the results of the Wald test into a table. We name that table waldtable.
Having created the table object, waldtable, containing the results, we can access any part of those results by referring to the table cell containing the information we want. The Waldstatistic for this particular Wald test is located in the 6th row ,2nd column of the table. The associated pvalue is located in the 6th row, 4th column.
Since all numbers are stored in string form in tables, we cannot directly store the value of the Wald statistic into the storage matrix. Instead we have to use the @val function to convert the string representation of the number into an actual number that can be stored into the matrix.
Finally, having stored the values from the table object into the matrix, we delete the table, using the d command. This is to prevent a duplicate object error next time we run through the loop and create the table again.
Follow us on Twitter @IHSEViews

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 12239
 Joined: Tue Sep 16, 2008 5:38 pm
Re: An introduction to EViews programming.
This program is estimates a number of different equations, each with the same dependent variable, but with different numbers of lags of the dependent variable as regressors. It then selects the number of lags that produces the highest Akaike Information Criterion (AIC). An important aspect of this program is the ability in EViews to specify multiple lags at once by using the form X(i to j), which tells EViews to include all lags of X from the ith lag to the jth.
Note that when comparing models with differing numbers of lags, it is important to ensure the sample (the number of observations) used in each model is the same. Thus the program sets the workfile sample to miss the first !maxlags observations, where !maxlags is the maximum number of lags that will be included in the models.
The program uses a program variable, !aic, to keep track of the lowest AIC value. This value is initialized to a large number (so that the first equation will definitely have an AIC lower than the initial value). The variable !maxlags is used to specify the maximum number of lags that will be compared. The variable !bestlag is used to keep track of the lag associated with the lowest AIC value.
The smpl command is used to change the workfile sample. The sample is specified by a pair of dates (separated by space), or observation numbers. The @first keyword to specifies the first observation of the workfile, and the @last keyword specifies the last. Thus the line smpl @first+!maxlags @last sets the sample to the !maxlags’th plus one observation to the last observation of the workfile . i.e. if !maxlags I set at 12, the sample will be the 13th observation onwards.
The FOR loop runs from 1 to !maxlags, and thus will run 12 times (since !maxlags has been set to 12). Each iteration respecifies the equation EQ to include lags up to !i. Once the equation has been respecified and estimated, an if condition is used to test whether the AIC value from that equation (referenced with the @aic data member) is smaller than the current “best” AIC value stored in !aic. If the current value is smaller, then the current lag is recorded as !bestlag, and the “best” AIC value is reset to the current AIC.
Finally after the loop has finished, the equation is reestimated using a number of lags equal to !bestlag.
Note that when comparing models with differing numbers of lags, it is important to ensure the sample (the number of observations) used in each model is the same. Thus the program sets the workfile sample to miss the first !maxlags observations, where !maxlags is the maximum number of lags that will be included in the models.
Code: Select all
'create a workfile
wfcreate q 1990 2010
'create a variable
series y = nrnd
'create empty equation to be used inside the loop
equation eq
'variable to store the minimum AIC. Initialise it to a large number
!aic = 99999999
'variable saying how many lags to go up to
!maxlags = 12
'Variable to store the "best" number of lags
!bestlag = 0
'set sample to be the !maxlag'th value onwards
smpl @first+!maxlags @last
for !i=1 to !maxlags
eq.ls y c y(1 to !i) 'run regression of Y on a constant and lagged values of itself up to the iTH lag.
if eq.@aic < !aic then
!bestlag = !i 'if this lag specification has the best AIC, then store this lag as !bestlag.
!aic = eq.@aic
endif
next
show eq.ls y c y(1 to !bestlag)
'reset sample
smpl @all
The program uses a program variable, !aic, to keep track of the lowest AIC value. This value is initialized to a large number (so that the first equation will definitely have an AIC lower than the initial value). The variable !maxlags is used to specify the maximum number of lags that will be compared. The variable !bestlag is used to keep track of the lag associated with the lowest AIC value.
The smpl command is used to change the workfile sample. The sample is specified by a pair of dates (separated by space), or observation numbers. The @first keyword to specifies the first observation of the workfile, and the @last keyword specifies the last. Thus the line smpl @first+!maxlags @last sets the sample to the !maxlags’th plus one observation to the last observation of the workfile . i.e. if !maxlags I set at 12, the sample will be the 13th observation onwards.
The FOR loop runs from 1 to !maxlags, and thus will run 12 times (since !maxlags has been set to 12). Each iteration respecifies the equation EQ to include lags up to !i. Once the equation has been respecified and estimated, an if condition is used to test whether the AIC value from that equation (referenced with the @aic data member) is smaller than the current “best” AIC value stored in !aic. If the current value is smaller, then the current lag is recorded as !bestlag, and the “best” AIC value is reset to the current AIC.
Finally after the loop has finished, the equation is reestimated using a number of lags equal to !bestlag.
Follow us on Twitter @IHSEViews

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 12239
 Joined: Tue Sep 16, 2008 5:38 pm
Re: An introduction to EViews programming.
User Interface and Dialogs
This program creates a workfile and some series, then puts up dialogs asking the user to specify the dependent variable and the regressors to be used in a leastsquares equation. Note that the part of the program that generates the data could be replaced with a command to open up an existing workfile.
Three program variables are used in this program: %dep, %regs and !result. %dep is the string variable that will contain the user’s entry for the dependent variable. To begin we set this equal to an empty string (“”). %regs is used to store the user’s entry for the list of regressors. We set this equal to “c” to begin with. This means that the default setting for the regressor list will be a constant. !result will be used to track the completion of the dialogs. Every dialog returns an integer value depending on whether the user clicked “OK” or “Cancel” (or in some cases “Yes” or “No”). We set this value to 0 to begin with.
Following the declaration of the variables, the first dialog is put up using the @uiedit command . It is an Edit dialog asking the user to “Enter the dependent variable”. The user’s response will be stored in %dep. Following the dialog command, we check whether the value of !result is equal to 1. A 1 indicates that the user pressed cancel on the dialog. If this is the case, the program quits, using the stop command.
The second dialog command is similar to the first, but rather than asking for the dependent variable, it asks the user to “Enter the list of regressors”, and stores that list in %regs. Note that since %regs was set equal to “c” prior to the dialog being put up, the dialog will be prefilled with the constant term. Users can still delete the constant or add extra regrssors.
Finally, having obtained entries for both %dep and %regs, and equation, EQ1 is estimated via least squares with the specified variables.
This program creates a workfile and some series, then puts up dialogs asking the user to specify the dependent variable and the regressors to be used in a leastsquares equation. Note that the part of the program that generates the data could be replaced with a command to open up an existing workfile.
Code: Select all
'Create a workfile and some series
create u 100
series y=nrnd
series x=nrnd
series w=nrnd
'
%dep = "" ' variable to store name of dependent variable
%regs = "c " ' variable to store list of regressors. Default is "c" for a constant.
!result = 0 ' variable that will track the result of dialogs. 1 indicates user hit Cancel. 0 Indicates user hit OK.
!result = @uiedit(%dep,"Enter the dependent variable") ' put up an Edit dialog asking for dependent variable.
if !result=1 then 'if user cancelled, then stop program
stop
endif
!result = @uiedit(%regs,"Enter the list of regressors") ' put up an Edit dialog asking for regressors list.
if !result=1 then 'if user cancelled, then stop program
stop
endif
equation eq1.ls {%dep} {%regs} 'estimate equation.
Three program variables are used in this program: %dep, %regs and !result. %dep is the string variable that will contain the user’s entry for the dependent variable. To begin we set this equal to an empty string (“”). %regs is used to store the user’s entry for the list of regressors. We set this equal to “c” to begin with. This means that the default setting for the regressor list will be a constant. !result will be used to track the completion of the dialogs. Every dialog returns an integer value depending on whether the user clicked “OK” or “Cancel” (or in some cases “Yes” or “No”). We set this value to 0 to begin with.
Following the declaration of the variables, the first dialog is put up using the @uiedit command . It is an Edit dialog asking the user to “Enter the dependent variable”. The user’s response will be stored in %dep. Following the dialog command, we check whether the value of !result is equal to 1. A 1 indicates that the user pressed cancel on the dialog. If this is the case, the program quits, using the stop command.
The second dialog command is similar to the first, but rather than asking for the dependent variable, it asks the user to “Enter the list of regressors”, and stores that list in %regs. Note that since %regs was set equal to “c” prior to the dialog being put up, the dialog will be prefilled with the constant term. Users can still delete the constant or add extra regrssors.
Finally, having obtained entries for both %dep and %regs, and equation, EQ1 is estimated via least squares with the specified variables.
Follow us on Twitter @IHSEViews

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 12239
 Joined: Tue Sep 16, 2008 5:38 pm
Re: An introduction to EViews programming.
The second user interface program builds upon the first by adding an option to set the sample to be used for the equation, and by amalgamating the dialogs into one single dialog:
The new variable, %sample is used to store the user’s entry for the sample. This is defaulted to “@all” to specify the entire workfile range.
The @dialog command is used to create the dialog. This command can be used to build dialogs with more than one control/field. We’ve used it to create a dialog with three Edit boxes. Note that the “edit” keyword is used to specify that a Edit box is required, and each keyword is followed by the string variable used to store the entry, and then the text prompt.
Following the dialog the program checks for the user having selected “Cancel”. If "Cancel" was selected, the program stops, otherwise it sets the sample to the user selected sample, using the smpl command. Finally the equation is estimated and the sample is set back to the entire workfile.
Code: Select all
'Create a workfile and some series
create u 100
series y=nrnd
series x=nrnd
series w=nrnd
'
%dep = "" ' variable to store name of dependent variable
%regs = "c " ' variable to store list of regressors. Default is "c" for a constant.
%sample = "@all" ' variable used to store sample. Default is "@all" for entire workfile.
!result = 0 ' variable that will track the result of dialogs. 1 indicates user hit Cancel. 0 Indicates user hit OK.
!result = @uidialog("edit",%dep,"Enter the dependent variable", "edit", %regs, "Enter the list of regressors", "edit", %sample, "Enter the equation sample") 'put up dialog
if !result=1 then 'if user cancelled, then stop program
stop
endif
smpl {%sample} 'set the sample
equation eq1.ls {%dep} {%regs} 'estimate equation.
smpl @all 'reset the sample back to the entire workfile.
The new variable, %sample is used to store the user’s entry for the sample. This is defaulted to “@all” to specify the entire workfile range.
The @dialog command is used to create the dialog. This command can be used to build dialogs with more than one control/field. We’ve used it to create a dialog with three Edit boxes. Note that the “edit” keyword is used to specify that a Edit box is required, and each keyword is followed by the string variable used to store the entry, and then the text prompt.
Following the dialog the program checks for the user having selected “Cancel”. If "Cancel" was selected, the program stops, otherwise it sets the sample to the user selected sample, using the smpl command. Finally the equation is estimated and the sample is set back to the entire workfile.
Follow us on Twitter @IHSEViews

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 12239
 Joined: Tue Sep 16, 2008 5:38 pm
Re: An introduction to EViews programming.
This program builds upon the previous one by adding options for the covariance matrix calculation used in the estimation. A check box is added to the first dialog, asking whether the user wishes to change the covariance calculation method. If the box is checked, then a second dialog is brought up where the user can choose from a list of covariance methods.
Four new program variables have been introduced: !doCovs, !CovChoice, %CovList and %CovOpt. The first is being used as a binary variable indicating whether the user wishes to view the covariance dialog or not. A value of 0 indicates no, and 1 indicates yes. !CovChoice is used to store the user’s selected covariance method choice. %CovList is used to define the list of possible covariance matrix choices. Note that this variable cannot be changed by the user during the running of the program. %CovOpt is used as the string representation of the user’s covariance matrix choice as an EViews command.
The first dialog has changed from the second program, to include a Check box. Note that the keyword “check” is used to denote the addition of the checkbox. !CovChoice is the variable used to store the value of the check box, and the prompt is given as “View covariance options?”.
Once the first dialog has been put up, the program checks whether the value of !doCovs is equal to 1 or not. A 1 indicates that the user had checked the “View covariance options?” box. If this is the case, then the program puts up a second dialog, this time a List box, using the @uilist command. !CovChoice is used to store which item in the list the user selected. %CovList is used to populate the List box with the choices.
Following the second dialog, the program checks the value of !CovChoice. If !CovChoice is equal to 2, indicating that the user chose the second element of %CovList, “White”, then %CovOpt is set equal to “cov=white”. If !CovChoice is equal to 3, corresponding to “HAC”, then %CovOpt is set equal to “cov=hac”.
Following the setting of %CovOpt, the sample is set, and then the equation is estimated. Note that the equation is estimated with its options set to whatever is in %CovOpt. If the covariance options dialog was never displayed (i.e. the user did not check the Check box corresponding to !doCovs) or if the user selected “Default” in the List box of the second dialog, %CovOpt will be an empty string, and so no options will be applied to the equation. However, if a covariance choice was made, %CovOpt will contain a valid covariance option for the equation.
Code: Select all
'Create a workfile and some series
create u 100
series y=nrnd
series x=nrnd
series w=nrnd
'
%dep = "" ' variable to store name of dependent variable
%regs = "c " ' variable to store list of regressors. Default is "c" for a constant.
%sample = "@all" ' variable used to store sample. Default is "@all" for entire workfile.
!doCovs = 0 ' variable used to store choice of whether to display covariance options
!CovChoice = 0 ' variable specify choice of covariance matrix.
!result = 0 ' variable that will track the result of dialogs. 1 indicates user hit Cancel. 0 Indicates user hit OK.
%CovList = "Default White HAC" ' list of covariance types for use in covariance dialog
%CovOpt = "" ' covariance option string
!result = @uidialog("edit",%dep,"Enter the dependent variable", "edit", %regs, "Enter the list of regressors", "edit", %sample, "Enter the equation sample","check",!doCovs, "View covariance options?") 'put up dialog
if !result=1 then 'if user cancelled, then stop program
stop
endif
if !doCovs =1 then
!result = @uilist(!CovChoice, "Covariance options", %covlist) ' put up cov dialog
if !result=1 then 'if user cancelled, then stop program
stop
endif
if !CovChoice = 2 then
%CovOpt = "cov=white"
else if !CovChoice = 3 then
%CovOpt = "cov=hac"
endif
endif
endif
smpl {%sample} 'set the sample
equation eq1.ls({%CovOpt}) {%dep} {%regs} 'estimate equation.
smpl @all 'reset the sample back to the entire workfile.
Four new program variables have been introduced: !doCovs, !CovChoice, %CovList and %CovOpt. The first is being used as a binary variable indicating whether the user wishes to view the covariance dialog or not. A value of 0 indicates no, and 1 indicates yes. !CovChoice is used to store the user’s selected covariance method choice. %CovList is used to define the list of possible covariance matrix choices. Note that this variable cannot be changed by the user during the running of the program. %CovOpt is used as the string representation of the user’s covariance matrix choice as an EViews command.
The first dialog has changed from the second program, to include a Check box. Note that the keyword “check” is used to denote the addition of the checkbox. !CovChoice is the variable used to store the value of the check box, and the prompt is given as “View covariance options?”.
Once the first dialog has been put up, the program checks whether the value of !doCovs is equal to 1 or not. A 1 indicates that the user had checked the “View covariance options?” box. If this is the case, then the program puts up a second dialog, this time a List box, using the @uilist command. !CovChoice is used to store which item in the list the user selected. %CovList is used to populate the List box with the choices.
Following the second dialog, the program checks the value of !CovChoice. If !CovChoice is equal to 2, indicating that the user chose the second element of %CovList, “White”, then %CovOpt is set equal to “cov=white”. If !CovChoice is equal to 3, corresponding to “HAC”, then %CovOpt is set equal to “cov=hac”.
Following the setting of %CovOpt, the sample is set, and then the equation is estimated. Note that the equation is estimated with its options set to whatever is in %CovOpt. If the covariance options dialog was never displayed (i.e. the user did not check the Check box corresponding to !doCovs) or if the user selected “Default” in the List box of the second dialog, %CovOpt will be an empty string, and so no options will be applied to the equation. However, if a covariance choice was made, %CovOpt will contain a valid covariance option for the equation.
Follow us on Twitter @IHSEViews

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 12239
 Joined: Tue Sep 16, 2008 5:38 pm
Re: An introduction to EViews programming.
String Manipulation
This program creates some data and some equations, and then uses some of the string manipulation tools to calculate a list of all of the regressors in the equations. Note that the part of the program that creates the data and equations could be replaced by a command to open an existing workfile, which is a more realistic scenario.
The variable %eqnames is used to identify which equations should be parsed for regressors. It is initially set to “*”, which is used to signify all equations in the workfile. A dialog is used to let the user change the specification of %eqnames. The type of dialog is @uidialog rather than @uiedit, since an extra text control is used, giving instructions for the user to “use * as a wildcard”.
Following the dialog, the wildcard is handled. First the @instr command is used to check for the existence of an “*” in %eqnames. @instr returns an integer specifying the location of the “*”. If no “*” exists, then it will return a zero.
If a “*” does exist, then we use the @wlookup command to search the workfile for all equations (done with the “equation” keyword) whose name matches the pattern given by the “*” in %eqnames, and assign the names of those equations back into %eqnames (in alphabetical order).
Following the handling of the wildcard, we create a workfile string variable, allregs that will contain the list of all of the regressors used in the equations listed in %eqnames. Then, a FOR loop is used to loop through all of the equations and find the regressors in each. Note that the @wcount command is used to calculate how many equation names are contained in %eqnames (this function merely does a count of how many elements there are in a space delimited list).
Each iteration of the loop takes a name from the list of equations in %eqnames and puts it into a new variable, %eqname. This is done using the @word command, which returns the ith word from a list. Thus the first time through the loop (if %eqnames just contains an “*”), %eqname will be equal to “WX”, and the second time through it will equal “YW”. Once the equation name has been captured, we obtain a list of the variables used in that equation, using the @wregresors equation data member. Since this returns both the dependent and independent variables, we then use %wmid command to drop the first word from the list of variables (since the first word is always the dependent variable). The result is then appended to allregs.
At the end of the loop allregs will contain the regressors from each equation specified in %eqnames. However if more than one equation contain the same regressor, that regressor will be entered in allregs twice. We use the @wunique command to remove any such duplicates.
This program creates some data and some equations, and then uses some of the string manipulation tools to calculate a list of all of the regressors in the equations. Note that the part of the program that creates the data and equations could be replaced by a command to open an existing workfile, which is a more realistic scenario.
Code: Select all
'Create a workfile and some series
create u 100
series y=nrnd
series x=nrnd
series w=nrnd
'Create some equations
equation yx.ls y c x
equation yw.ls y c w
equation wx.ls w c x
'
%eqnames = "*" ' variable to store name of equations to parse. Wildcards (*) accepted.
'throw up dialog asking for list of equation names
!result = @uidialog("edit",%eqnames,"Enter a space delimited list of equation names", "text"," (or use * as a wildcard)")
if !result=1 then
stop
endif
'check to see whether a wild card was entered in the eqnames list, and process it if it was.
if @instr(%eqnames,"*")>0 then
%eqnames = @wlookup(%eqnames,"equation")
endif
string allregs = ""
for !i = 1 to @wcount(%eqnames)
%eqname = @word(%eqnames,!i) 'get ith equation name
%vars = {%eqname}.@wregressors
%vars = @wmid(%vars,2) 'drop dependent variable
allregs = allregs + " " + %vars
next
'remove duplicates from regressor list
allregs = @wunique(allregs)
The variable %eqnames is used to identify which equations should be parsed for regressors. It is initially set to “*”, which is used to signify all equations in the workfile. A dialog is used to let the user change the specification of %eqnames. The type of dialog is @uidialog rather than @uiedit, since an extra text control is used, giving instructions for the user to “use * as a wildcard”.
Following the dialog, the wildcard is handled. First the @instr command is used to check for the existence of an “*” in %eqnames. @instr returns an integer specifying the location of the “*”. If no “*” exists, then it will return a zero.
If a “*” does exist, then we use the @wlookup command to search the workfile for all equations (done with the “equation” keyword) whose name matches the pattern given by the “*” in %eqnames, and assign the names of those equations back into %eqnames (in alphabetical order).
Following the handling of the wildcard, we create a workfile string variable, allregs that will contain the list of all of the regressors used in the equations listed in %eqnames. Then, a FOR loop is used to loop through all of the equations and find the regressors in each. Note that the @wcount command is used to calculate how many equation names are contained in %eqnames (this function merely does a count of how many elements there are in a space delimited list).
Each iteration of the loop takes a name from the list of equations in %eqnames and puts it into a new variable, %eqname. This is done using the @word command, which returns the ith word from a list. Thus the first time through the loop (if %eqnames just contains an “*”), %eqname will be equal to “WX”, and the second time through it will equal “YW”. Once the equation name has been captured, we obtain a list of the variables used in that equation, using the @wregresors equation data member. Since this returns both the dependent and independent variables, we then use %wmid command to drop the first word from the list of variables (since the first word is always the dependent variable). The result is then appended to allregs.
At the end of the loop allregs will contain the regressors from each equation specified in %eqnames. However if more than one equation contain the same regressor, that regressor will be entered in allregs twice. We use the @wunique command to remove any such duplicates.
Follow us on Twitter @IHSEViews

 Posts: 4
 Joined: Thu Jun 03, 2010 10:57 am
Re: An introduction to EViews programming.
Hello,
I tried to implement below program but kept getting the error message: @uiedit is an illegal or reserved name in "!Result = @uiedit ("", "Enter the Dependent Variable"). Do you have any ideas on how to rectify/circumvent this issue? I'm using Eviews6.
Thanks,
Dan
I tried to implement below program but kept getting the error message: @uiedit is an illegal or reserved name in "!Result = @uiedit ("", "Enter the Dependent Variable"). Do you have any ideas on how to rectify/circumvent this issue? I'm using Eviews6.
Thanks,
Dan
EViews Gareth wrote:User Interface and Dialogs
This program creates a workfile and some series, then puts up dialogs asking the user to specify the dependent variable and the regressors to be used in a leastsquares equation. Note that the part of the program that generates the data could be replaced with a command to open up an existing workfile.Code: Select all
'Create a workfile and some series
create u 100
series y=nrnd
series x=nrnd
series w=nrnd
'
%dep = "" ' variable to store name of dependent variable
%regs = "c " ' variable to store list of regressors. Default is "c" for a constant.
!result = 0 ' variable that will track the result of dialogs. 1 indicates user hit Cancel. 0 Indicates user hit OK.
!result = @uiedit(%dep,"Enter the dependent variable") ' put up an Edit dialog asking for dependent variable.
if !result=1 then 'if user cancelled, then stop program
stop
endif
!result = @uiedit(%regs,"Enter the list of regressors") ' put up an Edit dialog asking for regressors list.
if !result=1 then 'if user cancelled, then stop program
stop
endif
equation eq1.ls {%dep} {%regs} 'estimate equation.
Three program variables are used in this program: %dep, %regs and !result. %dep is the string variable that will contain the user’s entry for the dependent variable. To begin we set this equal to an empty string (“”). %regs is used to store the user’s entry for the list of regressors. We set this equal to “c” to begin with. This means that the default setting for the regressor list will be a constant. !result will be used to track the completion of the dialogs. Every dialog returns an integer value depending on whether the user clicked “OK” or “Cancel” (or in some cases “Yes” or “No”). We set this value to 0 to begin with.
Following the declaration of the variables, the first dialog is put up using the @uiedit command . It is an Edit dialog asking the user to “Enter the dependent variable”. The user’s response will be stored in %dep. Following the dialog command, we check whether the value of !result is equal to 1. A 1 indicates that the user pressed cancel on the dialog. If this is the case, the program quits, using the stop command.
The second dialog command is similar to the first, but rather than asking for the dependent variable, it asks the user to “Enter the list of regressors”, and stores that list in %regs. Note that since %regs was set equal to “c” prior to the dialog being put up, the dialog will be prefilled with the constant term. Users can still delete the constant or add extra regrssors.
Finally, having obtained entries for both %dep and %regs, and equation, EQ1 is estimated via least squares with the specified variables.

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 12239
 Joined: Tue Sep 16, 2008 5:38 pm
An introduction to EViews programming.
Uiedit is not available in ev6
Follow us on Twitter @IHSEViews

 Posts: 11
 Joined: Fri Feb 19, 2010 3:21 am
Re: An introduction to EViews programming.
I'm getting the error message "%VARS is not defined or is an illegal command in %VARS = WV.@WREGRESSORS" when I run the programme in EV7. I cannot find @wregressors in the documentation.

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 12239
 Joined: Tue Sep 16, 2008 5:38 pm
An introduction to EViews programming.
Good catch. I wrote the guide for a beta copy of 7. We changed it to @varlist
Follow us on Twitter @IHSEViews
Who is online
Users browsing this forum: No registered users and 3 guests