DOFOR Instruction |
DOFOR index = list of values
instructions executed for each value taken by index
END
Loops until the matching END with index taking listed values in turn 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. This can be very useful in a wide variety of situations. See the examples for suggestions.
Parameters
|
index |
The variable that takes each of the values in the list in turn. This can have any data type, but if not previously declared it will be an INTEGER. |
|
list of values |
The list of values index is to take. These values must be compatible with index. See the rules in the next paragraph. |
List of Values
•If index is an INTEGER, they may be any integer-valued variables or expressions. You can use “n TO m” as shorthand for consecutive integer values.
•If index is a SERIES or an EQUATION, they may be series or equation names, or any integer-valued variables or expressions. Integer values are interpreted as series or equation "handles".
•If index is any other type, they must be variables or expressions which can be converted to the correct type.
With any type of index, you can use an array aggregate of that type in the list of values: for example, you can use VECTOR[INTEGERS] with an INTEGER index. DOFOR will loop over all the elements in that array.
Variables Defined
|
%DOFORPASS |
pass number through the list (starting at 1) (INTEGER) |
Notes
index is used as a “placeholder”: its value at the end of execution of the loop is unchanged from what it was at the beginning.
You can use an INTEGER index even if you intend to loop over a list of series. That is the preferred way of handling a list of series, since you don’t have to create a new series (which DECLARE SERIES will do) solely for the purpose of serving as a loop index. RATS converts a series name into its numerical "handle" and will convert it back to reference a series if necessary. If you need to refer to the series in a SET or FRML instruction, however, explicitly tell RATS that you intend the index to represent a series by using index{0} rather than index alone the way you could with an ordinary series name. See the first example.
Example
spgraph(hfields=3,vfields=4,$
xlabels=||"Levels","1st Difference","12th Difference"||,$
ylabels=||"Interest Rate","Money","Price","Output"||)
dofor y = ffunds lm lp lo
set dy = y{0}-y{1}
set d12y = y{0}-y{12}
graph(row=%doforpass,col=1)
# y
graph(row=%doforpass,col=2)
# dy
graph(row=%doforpass,col=3)
# d12y
end dofor y
spgraph(done)
This loops over four series, creating the 1st and 12th difference for each, then graphing the level, and the two derived series in a 4 x 3 graph matrix, organized by SPGRAPH. The GRAPH instructions use the %DOFORPASS variable to put the graph into the proper slot.
dofor i = canusxsr frausxsr jpnusxsr gbrusxsr
compute base=([series]i)(1996:1)
set i = 100*(i{0}/base - 1.0)
end dofor i
This loops over four series, transforming each to a percentage deviation from the value at 1996:1. The ([SERIES]I) forces I to be treated as a SERIES to get the value of the series at the specific entry of 1996:1.
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 %uzwzu 2*nlag-1
end dofor
This loops over different lag lengths in the instrument set.
set thresh = spread{1}
dec real lower upper
dofor lower = %seqa(-.2,.1,5)
dofor upper = %seqa(1.6,.1,5)
sweep(group=(thresh>=lower)+(thresh>upper))
# spread
# constant spread{1 2}
compute rss=%scalar(%sigma)
if rss<rssbest
compute lowerbest=lower,upperbest=upper,rssbest=rss
end dofor upper
end dofor lower
This does a nested pair of DOFOR's with a lower and upper threshold limit. The %SEQA function is used to generate a VECTOR with a grid of values for each.
compute n=8
dec vect[series] x(n)
dofor [string] s = "jpn" "fra" "sui" "nld" "uk" "bel" "ger" "can"
compute xrate="usx"+s
set x(%doforpass) = 100.0*log(%s(xrate)/%s(xrate){1})
end dofor
This creates a VECT[SERIES] with the returns for eight exchange rates where the original data are series of the form USXJPN, USXFRA, etc.
cal(a) 1970:1
open data bric.xlsx
dofor [string] s = "Brazil" "Russia" "India" "China"
data(format=xlsx,org=columns,top=3,sheet=s) 1970:1 2015:1 import export
disp "Analysis for" s
... analysis of import and export data ...
end dofor s
This is "pseudo-code" for a parallel analysis of four countries' data, where the raw data are in a spreadsheet with a separate sheet for each country (with "IMPORT" and "EXPORT" used as the labels on each).
Copyright © 2025 Thomas A. Doan