COMPUTE Instruction |
COMPUTE variable = expression
COMPUTE evaluates an expression and stores the result in the variable on the left of the = sign.
COMPUTE can set single entries of data series. However, to set a range of values in a series using a single expression, you need to use the SET instruction.
Parameters
|
variable |
This can be almost any type of variable supported by RATS, except FRML (formula) which must be set using the instruction FRML. It can be an element of a series, but not an entire series itself—use SET, CSET or GSET (depending upon the type of the series) to set multiple elements of a series with a single instruction. |
|
expression |
expression can include any legal combination of arithmetic and matrix operators, functions, numeric values, character literals, and previously defined variables and arrays. If the variable is of a known type, the expression must evaluate to a type which can be converted to variable’s type. |
Description
COMPUTE is one of the most important and frequently used instructions in RATS. Among its uses in typical RATS programs:
•You can do calculations using values such as the Residual Sum of Squares (%RSS variable) produced by instructions like LINREG and NLLS.
•You can set integer values for starting and ending observations, then use these variables as parameters on instructions. You can then easily alter the program to use a different range of entries by changing just a few COMPUTE instructions.
•You can do matrix calculations such as multiplication, addition, inversion and determinants.
•With COMPUTE, you can do just about any numerical computation you could do with a high level programming language such as FORTRAN or C and often much more efficiently, since RATS can work with entire matrices. And it has similar capabilities to well-known “math” packages.
Variables, Data Types, and Type Modifiers
Every variable in RATS will be of a certain data type (REAL, INTEGER, SERIES, etc.).
•If you introduce a new variable with a COMPUTE instruction, you can specify the data type of the variable using a type modifier. A type modifier is simply the name (or three-letter abbreviation) of one of the RATS data types, listed inside brackets ( [ and ] ) before the variable name.
•You can also declare the variable ahead of time using a DECLARE statement.
•If you haven’t declared the variable ahead of time, and you don’t use a type modifier, RATS will determine the data type of the new variable based on the result of the expression. For example, the instruction COMPUTE A = 5.95 would define the variable A as a REAL because 5.95 is a real value. You have to be careful here not to be misunderstood. COMPUTE SUM=0 will make SUM an INTEGER, while COMPUTE SUM=0.0 will make it a REAL.
•When computing an array, you will not, in most cases, need to declare or dimension the array ahead of time, as RATS can usually determine the form of the new variable from the context of the expression. In some cases, however, you may need or want to define the array type using either a DECLARE instruction ahead of time, or a type modifier on the COMPUTE instruction (for example, to declare an array as type SYMMETRIC, rather than letting RATS define it as the more general RECTANGULAR type). Also, a few matrix functions do need a dimensioned target array, as they aren’t functions of other matrices. Examples of this are %RAN and %UNIFORM.
Scalar Calculations
COMPUTE can do almost any kind of scalar calculation. Some examples:
compute ndraws=500
compute fstat = ( (rssr-rssu)/q ) / (rssu/%ndf)
This first sets the (INTEGER) NDRAWS to 500. The second computes an F-statistic using previously set values, and saves the result in the variable FSTAT.
Matrix Calculations
COMPUTE is the primary instruction for matrix calculations. Some examples:
compute c = a*b
sets the array C equal to the product of the arrays A and B. You do not have to declare or dimension C ahead of time.
compute [symmetric] cmominv = inv(%cmom)
computes the inverse of the cross-moment matrix %CMOM and stores the resulting array in the (new) SYMMETRIC array CMOMINV.
Multiple Expressions
You can evaluate multiple expressions with a single COMPUTE instruction. Just separate the expressions with commas. For example:
compute b0=%beta(1) , b1=%beta(2) , b2=%beta(3)
You can also combine different types of expressions in a single COMPUTE:
compute adotb = %dot(a,b), ainv = inv(a), binv = inv(b)
Here, ADOTB is a real variable, while AINV and BINV are arrays.
In-Line Matrices
You can use COMPUTE to construct arrays directly in an expression. For example:
compute [vector] r = ||1.0, 2.0, 3.0||
creates a 3-element VECTOR called R, with elements 1.0, 2.0, and 3.0, while
compute [vector[label]] names = ||"US","Canada","Japan","Mexico"||
creates and sets a 4-element VECTOR of LABELS.
Strings and Labels
To construct complicated strings, it is probably easiest to use DISPLAY with the STORE option. You can, however, set LABEL and STRING variables using either literals or the + operator (which concatenates strings).
compute header = "Cross-correlations"
declare vector[labels] vlabel(10)
do i=1,10
compute vlabel(i) = "series"+i
end do
The second creates a vector containing the labels “series1”, “series2”, and so on.
Setting Series and Array Elements with COMPUTE
You can use COMPUTE to fill individual entries of series and arrays.
compute sales(2009:1) = %na
sets entry 2009:1 of series SALES to the missing value code.
do row=1,%rows(%cmom)
compute %cmom(row,row) = %cmom(row,row)+k
end do row
adds the constant K to each entry on the diagonal of the %CMOM array.
declare vector b(n)
compute fill=0 , b = %const(0.0)
do i=1,n
if a(i)>0
compute fill=fill+1 , b(fill) = a(i)
end do i
copies to the vector B the positive entries of the vector A. At the end of the loop, FILL is equal to the number of entries copied.
You should prefer SET (for data series) or EWISE (for matrices) to looping over a COMPUTE wherever reasonable. Those instructions are faster and shorter. However, there are circumstances in which COMPUTE offers the cleanest coding.
If you are setting a series, please note that RATS will give you an “unrecognizable name” error if the series name first appears in the COMPUTE instruction itself. To avoid this, introduce the series first using a CLEAR instruction:
clear posval
do i=1,435
if y(i)>0.0
compute posval(i)=1.0
else
compute y(i)=posval(i)=0.0
end do i
Note, by the way, that this example could be done (more efficiently) by
set y 1 435 = %max(y,0.0)
set posval 1 435 = y>0.0
Models, Equations and ParmSets
COMPUTE can also be used to construct new MODEL, EQUATION or PARMSET objects from existing MODELS or EQUATIONS. For example:
compute bigmodel = model1 + model2
compute foremodel = foremodel + salesident
compute neweqn = equation1 + 3.0*(equation2 + equation3)
compute fullset = base + constraints
The first line constructs a new model called BIGMODEL from existing models MODEL1 and MODEL2. The second line adds the equation SALESIDENT to the existing model FOREMODEL. The third line constructs NEWEQN as an equation whose right-hand-side is a linear combination of three existing equations. The fourth line creates FULLSET as a combination of the BASE and CONSTRAINTS parmsets.
Copyright © 2026 Thomas A. Doan