RATS 10.1
RATS 10.1

Introduction to RATS /

RATS Programming Language

Home Page

← Previous Next →

You can do quite a bit with the combination of the built-in instructions that we’ve introduced in the Getting Started pages, like LINREG and NLLS, plus the many others like BOXJENK, GARCH and MAXIMIZE that we’ve only mentioned in passing. But the real power of RATS comes from combining the sophisticated and carefully tested calculations of instructions like those with an extensive programming language. That’s what makes possible everything from the hundreds of procedures that are included with RATS to the highly interactive cointegration program CATS, which is entirely written in the RATS language.

 

While you may never get to the point of writing your own procedures, you will almost certainly need to use some of the more basic programming features. The DO and DOFOR loops are relatively simple, and make many tasks much easier. Many of the examples assume you can use these, so we’ll introduce them here. For more information, there is an entire section devoted to the Programming Tools.

 

Loops

RATS offers five different looping instructions:

 

DO

is a standard loop instruction which loops over an integer index. It is like a FORTRAN DO or a BASIC for.

DOFOR

is less standard but often very useful. It loops over a list of items: series, matrices, numbers, etc.

WHILE

loops as long as an expression is true.

UNTIL

loops until an expression is true. It always executes the enclosed instructions at least once (WHILE might not).

LOOP

loops unconditionally. You need to use a BREAK instruction to get out of the loop.

                    

 

A DO loop has the following structure:

 

do index = startvalue,endvalue,increment

   instructions executed for each value taken by index

end do

 

The index is a simple variable name, by convention, short names like I, J and K are often used. The increment is 1 by default, and the ,1 can be left out if it is.

 

 

A DOFOR loop repeatedly executes all of the instructions down to the matching END, with index taking a new value from the list of values on each pass through the loop. This continues until the list is exhausted. While the index of a DO loop must be an integer variable, a DOFOR index can be almost any type of variable.

 

dofor index =  list of values

   instructions executed for each value taken by index

end dofor

 

Examples

do time=1990:1,1992:12

   linreg y time time+47

   # constant y{1}

   set c1 time time = %beta(1)

   set c2 time time = %beta(2)

end do

 

does a “rolling regression”, using 48 data points for each regression. The first regression starts in 1990:1, the second in 1990:2, and so on. Dates in RATS are handled as integer entry numbers, so you can use dates in integer computations and in DO loops. On each trip through the loop, the SET instructions copy estimated coefficient values from %BETA (set by LINREG) into entry TIME of the series C1 and C2. When the loop is complete, these series will contain all 48 pairs of coefficient estimates.

 

 

 

dofor nlag = 1 2 4 6

  instruments constant consgrow{1 to nlag} realret{1 to nlag}

  nlls(inst,noprint,frml=h,optimal) * start *

  cdf(title="Specification Test for "+nlag+" lags") $

     chisqr %jstat 2*nlag-1

end dofor

 

runs a set of estimates using different numbers of lags in an instrument set.

 

Conditional Execution

The IF instruction allows you to control the flow of program execution based on the condition of a logical expression. For example, in the following, “Condition is true” is displayed only if the variable TEST contains the value 1.

 

if test==1

   display "Condition is true"

else

   display "Condition is false"

 

Important note: The IF instruction only evaluates a single conditional expression—it does not include any sort of implicit looping over series or matrix entries. If you want to set values of series or an array based on a condition, you probably want to use the %IF() function, rather than using IF inside a loop. See "Data Transformations" for details.

 

IF-ELSE blocks are usually part of a larger programming structure, like a DO loop or a PROCEDURE. On the rare occasion that you need a stand-alone IF or IF-ELSE, enclose the whole set of lines in { }; for instance,

 

{

if test==1

   display "Condition is true"

else

   display "Condition is false"

}

 

This lets RATS know the limits of the programming code (what we call a compiled section) that needs to be treated as a unit.

 


Copyright © 2025 Thomas A. Doan