RATS 10.1
RATS 10.1

<Root level>

Programming Tools

Home Page

← Previous Next →

While not the longest chapter in the book, this chapter may be the most important. It’s the programming tools described here that give rats its tremendous power. Even if you don’t use these yourself, you are an indirect consumer every time you use a procedure written by someone in the RATS community.

 

RATS provides an environment in which a user can, in effect, design his or her own instructions to do statistics in the manner desired. With the more advanced techniques, it’s possible to create a fully menu-driven application.

Loops, Conditional Blocks, and Procedures

RATS offers several tools for program control. You can

execute instructions only if a certain condition holds (or fails to hold) using IF and ELSE.

loop over a range of values or a list with DO or DOFOR.

execute a loop under the control of a condition using WHILE or UNTIL.

write entire subprograms with PROCEDURE and FUNCTION.

use the { and } symbols (or the equivalent keywords BEGIN and END) to define a “block” of instructions. Usually, these are used to execute a block of several instructions following a conditional statement when that conditional statement is itself part of a compiled section (such as a loop or procedure).

 

Procedures offer so much power, and are so useful, that we have put a discussion of their special properties in a separate set of pages.

 

Compile Mode

Ordinarily, RATS executes instructions in order, processing one instruction completely before going on to the next. However, loops, conditional blocks, procedures (and the { and } symbols) put RATS into compile mode: RATS reads in and interprets an entire block of instructions. Rather than executing them immediately, it converts them into an internal code and stores this code. When RATS reaches an END instruction, signalling the end of the structure, it begins executing the compiled code (except for a PROCEDURE or FUNCTION, which are executed only on command).

 

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.

 

With any of the loops, you can use NEXT to skip directly to the next pass or BREAK to break out of the loop entirely. Note that, if you have nested loops a NEXT or BREAK applies only to the loop in which the instruction is located. DO, DOFOR and LOOP have a somewhat different setup from WHILE and UNTIL. They all loop over the instructions down to a matching END. Some examples follow.

 

do rend=1999:1,2017:12

   linreg y 1970:1 rend

   # constant y{1}

end do

 

does regressions over a changing range of entries. All the regressions have the same starting date (1970:1), while the ending periods range from 1999:1 through 2017:12. This takes advantage of the fact that dates in RATS are handled as integer entry numbers.
 

 

dofor i = realgnp to realbfi

   set(scratch) i = log(i{0}/i{1})

end dofor

 

does a percent change calculation for series from REALGNP to REALBFI. We use I{0} rather than just I because I (an INTEGER variable) is perfectly legal by itself in an expression. Including the lag notation tells RATS to interpret I as a series.

 

WHILE (and UNTIL) loop only over the next instruction or block of instructions. When you want the conditional loop to include more than one instruction, you must enclose them within braces { and }. This will update both count and total as long as total is less than 100.

 

{

   if docount==1 {

      compute  total=0.0, count=0

      while  total<100.0 {

         compute count=count+1

         compute total=total+series(count)

      }

   }

}

 

IF, WHILE and UNTIL statements need to be part of a larger compiled section, which is why everything above is enclosed in an outer set of { and }. It never hurts to use extra enclosing braces if you’re not sure how instructions will be grouped.

 

It’s a very good idea to indent lines to show the flow of execution as is done here. The menu operations Edit—Indenting—Indent Lines and Edit—Indenting—Prettify are the easiest way to do this. The default level of indenting is 3 spaces, which is what we used in all the RATS examples.

 


Copyright © 2025 Thomas A. Doan