RATS 11.1
RATS 11.1

DO index = startvalue,endvalue[,increment]

  instructions executed for each value taken by index

END

 

Loops until matching END with index taking values beginning with startvalue, being changed by increment on each pass until:

 

index > endvalue if increment is positive, or

index < endvalue if increment is negative

 

index must be an INTEGER type. Default value for increment is 1.

 

Note that the body of the loop will not execute the loop a single time if startvalue is greater than endvalue (or less than endvalue for negative increment).

Examples of Syntax

do step=0,6            STEP=0,1,2,..,6

do count=5,0,-1        COUNT=5,4,...,0

do j=1979:1,enddata,4         J=1979:1,1980:1 (with quarterly data),... as long as JJENDDATA

Examples

The indenting that we use in these examples is not necessary, but makes the code easier to follow (you can use Edit>Indent Lines to do this). We strongly recommend that you get used to indenting the content of your loops.

 

RATS ignores any text (on the same line) after an END DO, so you can add comments to indicate which loop is “ended” by each END DO.
 

This loops over lag lengths from 0 to 24, running regressions of various lengths.

 

cmom

# constant shortrate{0 to 24} longrate

report(action=define,hlabels=||"Lags","Akaike","Schwarz"||,title="Distributed Lag IC")

do maxlag=0,24

   linreg(cmom,noprint) longrate

   # constant shortrate{0 to maxlag}

   compute akaike =log(%rss/%nobs)+%nreg*2.0/%nobs

   compute schwarz=log(%rss/%nobs)+%nreg*log(%nobs)/%nobs

   report(row=new,atcol=1) maxlag akaike schwarz

end do


 

This loops backwards from the final entry of an estimation (%REGEND()) to BSTART.

 

compute bestlmstat=0.0

set lmstats = %na

do time=%regend(),bstart,-1

   compute gsum=gsum+%xt(dd,time)

   if time<=bend {

      compute lmstat=%qform(%xx,gsum)*%nobs/(%regend()-time+1)

      compute lmstats(time)=lmstat

      if lmstat>bestlmstat

         compute bestlmstat=lmstat,bestbreak=time

   }

end do time

graph

# lmstats


 

This does triple nested loops over I, J and the number of lags (L).

 

do i=1,nvar

   do j=1,nvar

      do l=1,lags

         compute minnmean((j-1)*lags+l,i)=(i==j.and.l==1)

         compute minnprec((j-1)*lags+l,i)=olssee(j)/olssee(i)*%if(i==j,1.0/tight,1.0/(other*tight))

      end do l

   end do j

   compute minnmean(ncoef,i)=0.0,minnprec(ncoef,i)=0.0

end do i

Notes

You should never change the value of the index variable within the loop. RATS determines the number of trips that it will make through the loop when it first executes the DO instruction. This will not be affected by changes you make to the index. If you need a more flexible loop form, use WHILE or UNTIL.
 

The value of the index when the loop is exited is the value it had on the last pass through, not the value that causes the loop to terminate.
 

Avoid using the reserved variable T as a loop index, because it is reserved for use by SET and any instruction which uses a FRML (such as NLLS or MAXIMIZE) as these can change the value of T.

 

The instruction DOFOR is similar to DO, but loops over a list of values, and can have a loop index which isn’t an integer.

 

The function %DO can be used to put “looping” subcalculations into a larger calculation. For instance,

 

frml archpart  = v=0.0, %do(i, 1, p, v=v+b(i)*u{i}^2), v

 

will compute (for a value of T) a sum involving P lags of the series U.

 


Copyright © 2026 Thomas A. Doan