Agriculture Reference
In-Depth Information
program illustrate looping. The forvalues statement begins the
loop. The format for this command is
forvalues x = #1 ( #d ) #2 {
statements…
}
The first number ( #1 ) is the beginning value for × in the loop. The
second value ( #d ) indicates how much × should be incremented or
decremented, and the final value ( #2 ) is the final value for the loop.
An example is
forvalues x = 1 ( 1 ) 10 {
display `x'
}
This program loops from 1 to 10 in increments of 1 and displays the
incremented value each time the loop is executed. This can be entered
interactively in the Command window to see the results. When incre-
menting from the first value to the last value in units of 1, there is a
shortcut method of entry.
forvalues x = 1/10 {
There are a couple of other criteria required by forvalues loops.
he { brace must appear on the same line as the forvalues com-
mand and the } brace must appear on a line by itself. Frequently,
while programming, particularly when using nested loops, it is easy
to forget to enter the final brace. Stata's do-file editor can check and
let you know which open and closed braces match. To see this, double
click on any brace and the Do-File Editor will indicate its match.
Beginning with
forvalues z = `trt' ( -1 ) 2 {
there are two loops, one nested inside the other. The first loop begins
with the number of treatments (5 with the Strontium.dta dataset)
and loops down to 2. The next statement stores the calculated value
of each mean minus the critical comparison value ( S ) in the macro
`z' where z indicates each comparison, of which there are four with
Search WWH ::




Custom Search