Gretl Manual: Gnu Regression, Econometrics and Time-series Library | ||
---|---|---|
Prev | Chapter 9. Loop constructs | Next |
The third form of loop construct offered in gretl is an indexed loop, using the internal variable i. You specify starting and ending values for i, which is incremented by one each time round the loop. The syntax looks like this: loop i=1..20. Example 9-4 shows one use of this construct. We have a panel data set, with observations on a number of hospitals for the years 1991 to 2000. We restrict the sample to each of these years in turn and print cross-sectional summary statistics for variables 1 through 4.
Example 9-4. Indexed loop example
open hospitals.gdt loop for i=1991..2000 smpl -r (year=i) summary 1 2 3 4 endloop
The smpl command in the above example illustrates the use of the variable i within a loop. In addition, you can use the expression $i: in this case the value of i will be substituted before the command is evaluated. This enables you to construct strings (for example, variable names) within the loop, as in Example 9-5.
Example 9-5. Second indexed loop example
open bea.dat loop for i=1987..2001 genr V = COMP$i genr TC = GOC$i - PBT$i genr C = TC - V ols PBT$i const TC V endloop
The first time round this loop V will be set to equal COMP1987 and the dependent variable for the ols will be PBT1987, and so on.