st: Sifting scalar output from a set of -forvalues- statements

 From "Morris, Stephen" To "'statalist@hsphsun2.harvard.edu '" Subject st: Sifting scalar output from a set of -forvalues- statements Date Sat, 12 Apr 2003 11:28:26 +0100

```Hello,

I have written a small programme where the inputs are two scalars (say i and
j) and the output is a third scalar (k), which is some function of i and j.
The programme contains two -forvalues- statements so that I compute k from
various combinations of i and j. What I am trying to do is to identify the
combination of i and j that yields the minimum value of k. Currently I have
set up the programme so that it displays the values of i, j and k for each
combination of i and j and I have to eyeball the list to find the values of
i and j that yield the minimum value of k. This is fine, though very
cumbersome, since I want to run through thousands of (i,j) combinations. I
wondered if there was any way that I could get Stata to simply report the
combinations of i and j that yield the minimum value of k. I guess this
would mean saving all the scalar values somehow with some kind of unique
identifier, then sorting through them to find the minimum value, then
reporting the corresponding values of i, j and k, but I can't see how to do
this. Any help would be greatly appreciated.

Here is a simple illustration, though the solution here is of course
obvious.

scalar i = 1
scalar j = 1

forvalues i = 1(1)5 {
forvalues j = 1(1)5 {
scalar k = `i' + `j'
di in text %12s `i'  " {c |} " /*
*/ %12.0g `j'  " {c |} " /*
*/as result /*
*/ %12.0g k
}
}

Thanks very much.

Steve Morris
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```