Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

Re: st: How to put max and min values in a loop

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: How to put max and min values in a loop Date Thu, 28 Mar 2013 00:55:29 +0000

```Try this:

gen wanted = 0

foreach z of numlist 2/120 {

foreach x in x1 x2 x3 x4 x5{

foreach j of numlist 10 15 20 25 30 35 40 45 50 55{

replace wanted = Period == `z' & Sector == `j'

bysort wanted (group_`x' ) : ///
replace wanted = wanted & inlist(group_`x', group_`x'[1], group_`x'[_N])

tab group_`x' if wanted
qui ttest Return if  wanted, by(group_`x') unequal
display "`x'"   "Period "  "`z'"  "Sector "  "`j'" `r(p_l)'  `r(p)'  `r(p_u)'
}
}
}

On Wed, Mar 27, 2013 at 8:59 PM, Xixi Lin <winnielxx@gmail.com> wrote:

> You are right, the min() and max() seems to be not working. My
> original code is like:
>    foreach z of numlist 2/120 {
>    foreach x in x1 x2 x3 x4 x5{
>    foreach j of numlist 10 15 20 25 30 35 40 45 50 55{
>       qui ttest Return if inlist(group_`x',1,10) & Period==`z' &
> Sector==`j', by(group_`x') unequal
>       display "`x'"   "Period "  "`z'"  "Sector "  "`j'" `r(p_l)'
> `r(p)'  `r(p_u)'
>       }
>    }
> }
>
> however, for some independent variables, group 10 is not available,
> and I wanna use 9 instead, that is why I try to use max() instead of
> specifying 10 or 9 or even 8 . Do you know how to change the code
> which will ttest the first group and the last group?

> On Wed, Mar 27, 2013 at 10:23 AM, Nick Cox <njcoxstata@gmail.com> wrote:
>> This is likely to be wrong for several reasons.
>>
>> -min()- and -max()- in this context can only be Stata functions and
>> each requires two or more arguments. This is not quite explicit in the
>> help.
>>
>> Also, even when legal, statements including -min()- or -max()- will
>> only evaluate within observations.
>>
>> You assume that we know what you want to do, which is not true.
>> However, we can guess that you want minimum and maximum of some
>> variable, in which case -summarize- first will give you r(min) and
>> r(max), which you can use within -inrange()-. -summarize, meanonly- is
>> sufficient if min and max are all you need.
>>
>> But, but, but: it is vacuous that any variable lies between its
>> minimum and maximum, so there is no point to doing this unless the
>> calculation of -summarize- refers to something different, e.g. a
>> different subset or a different variable.
>>
>> Nick
>>
>> On Wed, Mar 27, 2013 at 2:11 PM, Xixi Lin <winnielxx@gmail.com> wrote:
>>
>>> I am trying to use inlist(var, min(var), max(var)) in a loop; however,
>>> min(var) and max(var) seems to be wrong codes, does anyone know how to
>>> code it in a right way?
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```