RATS 11.1
RATS 11.1

Miscellaneous /

Arithmetic Expressions

Home Page

← Previous Next →

The arithmetic expressions in RATS form the basis of the general transformation instruction SET, as well as calculations involving scalar and array variables, which you do primarily with the instruction COMPUTE. RATS expressions are similar to those employed in many programming languages and applications. We give enough detail here to allow you to do almost any standard data transformation.

Constants

You can represent a numeric value using a variety of forms:

with or without decimal points:  5    13.6    -.393

in scientific notation with a suffix of one of the forms: En, E+n, E-n, where n is a non-negative whole number:  2.3E5 (230000)     -.4E-4 (-.00004)

 

In addition, RATS provides the following two constants:

 

          %PI          The constant \(\pi\)

          %NA          The missing value code

 

Arithmetic Operators

RATS supports the following arithmetic operators:

 

+

addition

subtraction or negation

*

multiplication

/

division

^ or **

exponentiation

+=

add and assign (a += b is equivalent to a = a+b)

–=

subtract and assign (a –= b is equivalent to a = a–b)

*=

multiple and assign (a *= b is equivalent to a = a*b)

/=

divide and assign (a /= b is equivalent to a = a/b)

                    

You can use parentheses () to control the order of execution of the operations, and you can nest sets of parentheses if necessary. You cannot use brackets [] or braces {} as substitutes, because these have other meanings in RATS. In the absence of parentheses, operations are done in the following order:

1.Negation ( - used to change sign)

2.Exponentiation  (exception:  -a^b does a^b first)

3.Multiply and divide

4.Add and subtract

5.Logical operators (see below)

 

If two operations have the same precedence, they are done from left to right, so A-B+C is equivalent to (A-B)+C. The one exception to this is ^: A^B^C is the same as A^(B^C). All of this is based upon natural order of operations in algebraic formulas. Just as a+b/c is interpreted as a+(b/c) in algebra, A+B/C is A+(B/C) in RATS.

 

A full listing of all operators, including the ones reserved for matrix calculations, is provided below.

 

Functions

RATS provides many useful functions (over 300). Functions accept zero or more arguments and return a value or set of values. Function arguments must be enclosed within parentheses () and there should not be a space between the function name and the “(“. Some of the more important ones:

 

LOG(x)

natural log (\(\log_e\))

EXP(x)

\(e^x\)

SQRT(x)

square root

ABS(x)

absolute value

SIN(x)

sine (of x in radians)

COS(x)

cosine (of x in radians)

%IF(x,y,z)

is y if x is non-zero and z if x is zero

%VALID(x)

is 0 if x is “missing” and 1 otherwise

                    

See "Functions" for a complete list of available functions.

 

Examples

 

Expression

RATS Code

\(b^2-4ac\)

b^2-4*a*c

\(\frac{1}{{1 + y^2 }}\)

1/(1+y^2)

\(2^{-c}\)

2^-c

\(a-bc^d\)

a-b*c^d

\(log(1+w^2)\)

log(1+w^2)

\(e^{-|u|}\)

exp(-abs(u))

 

Logical and Relational Operators

Logical operators have many uses in RATS, one of which is creation of dummy variables. They code true/false statements, returning a one for true and zero for false. These are shown below (“A” and “B” can be any expression or number.)

 

Expression

Translates to the Statement

A==B or  A.EQ.B

A is EQual to B

A<>B or  A.NE.B

A is Not Equal to B

A>B  or  A.GT.B

A is Greater Than B

A>=B or  A.GE.B

A is Greater than or Equal to B

A<B  or  A.LT.B

A is Less Than B

A<=B or  A.LE.B

A is Less than or Equal to B

.NOT.A

A is NOT non-zero (that is, A is false)

A.AND.B

Both A AND B are non-zero (true)

A.OR.B

Either A OR B or both are non-zero (true)

 

The logical operators are lowest in order of precedence, so A+B==C*D is done as (A+B)==(C*D).

The first six (the relational operators) take precedence over the last three, which are listed above in order of precedence:

          A.OR.B.AND.C.OR.D   is done as   A.OR.(B.AND.C).OR.D

When testing for equality, be sure to use “==” (two equals), not “=” (just one)!!!

 

Examples

Y>3.0\(= \left\{ {\begin{array}{*{20}c}1 & {{\text{if }}Y > 3}  \\0 & {{\text{if }}Y \le 3}  \\\end{array}} \right.\)

 

T>=10.and.T<=20\(= \left\{ {\begin{array}{*{20}c}1 \hfill & {{\text{if }}T \ge 10\,\,{\text{and}}\,\,T \le 20} \hfill  \\0 \hfill & {{\text{otherwise}}} \hfill  \\\end{array}} \right.\)

 

 

Integer vs Real Numbers

RATS distinguishes between integer and real (floating point) numbers. Integers are whole numbers which serve primarily as entry numbers, loop indices and subscripts. For instance, the date 1970:1 is processed as an integer, and subscripts such as T-1 are integer expressions. A constant typed without a decimal point is considered to be an integer: 100.0 is real, 100, 1, 2 and 3 are integer.

 

When an operation mixes an integer with a real, the integer is converted to its equivalent as a real number before the operation is completed. For instance, if any one of A, B or C is real, (A+B+C)/3 is the same as (A+B+C)/3.0.

Division of two integers results in the integer quotient with no fraction, so 10/3 is 3 and 1/2 is 0. This may or may not be what you intend, so you need to be careful in such situations.

If you need to force RATS to convert an integer to a real in a situation where it would not be done automatically, use the function FLOAT, for instance FLOAT(T). Similarly, you can convert reals to integers with the function FIX. FIX truncates any remainder. If you want to round first and then convert to integer, do something like FIX(%ROUND(X,-1)) which rounds to the nearest 10 and converts to an integer.

 

Missing Values

Operations involving a missing value produce a missing value as a result, except for equality and inequality (== and <>). Because missing values propagate automatically, you don’t have to worry about making special tests to insure that an expression only operates on valid numbers.

 

An invalid operation of any form will also produce the missing value code as a value. Examples are SQRT(-1.0) and 1/0. RATS does not distinguish between illegal operations (such as SQRT(-1.0)) and operations which could be assigned a “value” of infinity, such as 1./0. Within an expression, you can represent a missing value only as %NA; not as NA or . or any of the alternatives that can be used on data files.

 

Referencing Series Elements

You can access a particular entry of a series by listing the entry or element in parentheses immediately after the variable name. For series, you would use an expression of the form “seriesname(entry)”, such as: FOODPROD(5) or DISPINC(1939:1). As we’ve already seen, in a SET instruction you reference the current value of a series (the value at the entry being computed) as seriesname alone, and its lag as seriesname{lag}.
 


 

Full Listing of Operators

 

This is a complete listing of the operators (including those that are limited to matrix calculations) with their corresponding precedence levels.

 

Precedence

Operator

Description

1

-

Negation ( prefixing number or expression)

2

** or ^

Exponentiation (scalars or matrices)

2

.^

Elementwise exponentiation (matrix by scalar)

3               

.* , ./

Elementwise multiplication (.*), division (./)

4

+ , -

Addition(+), subtraction(- between expressions)

4

.+ , .-

Elementwise addition(.+), subtraction (.-)

5

>= or .ge.

Is greater than or equal to

5

> or .gt.

Is greater than

5

<= or .le.

Is less than or equal to

5

< or .lt

Is less than

5

== or .eq.

Is equal to (note distinction between the ==  and = operators)

5

!= or <> or .ne.

Is not equal to

6

.not.

Logical negation

7

.and.

Logical “and”

8

.or.

Logical “or”

9

~

Horizontal concatenation of two arrays

9

~~

Vertical concatenation of two arrays

9

~\

Diagonal concatenation of two arrays

10

=

Assignment (copy object on right to object on left)

10

*=

Multiply and assign (a*=b same as a=a*b)

10

/=

Divide and assign (a/=b same as a=a/b)

10

+=

Add and assign (a+=b same as a=a+b)

10

–=

Subtract and assign (a–=b same as a=a–b)

Operator Precedence

“Precedence” is the order in which RATS evaluates the operators in an expression. For example, consider the following instruction.

 

compute c = 100–5*5

 

Multiplication has a higher precedence than subtraction, so RATS first multiplies 5 times 5. Then, it subtracts this result from 100, returning a final result of 75.
 

When operators have the same precedence (the same value in the Precedence column in the table above), RATS evaluates them from left to right, with two exceptions:

The exponentiation operators (** , ^ , .^) are evaluated from right to left. For example, RATS will handle A^B^C as A^(B^C)

In the absence of parentheses, RATS will evaluate the rightmost = sign (assignment operator) first, then move left.

Exponentiation takes precedence over negation in expressions of the form –A^B. For example, –A^2 is interpreted as –(A^2), rather than (–A)^2.

 

Using Parentheses

You can use parentheses to control the order in which RATS evaluates an expression. RATS will evaluate an expression contained in parentheses first, then move to operations outside the parentheses. If an expression includes nested parentheses (one set inside another), RATS evaluates the expression in the innermost set of parentheses first, then moves outward. For example:
 

1+2*3+4*5     = 27

(1+2)*3+4*5   = 29

1+((2*3)+4)*5 = 51


 

 


Copyright © 2026 Thomas A. Doan