Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: loop within a loop


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   RE: st: loop within a loop
Date   Fri, 11 Nov 2011 10:48:49 +0000

This thread raises more general issues. Steve got very good advice from Matthew, but two extra comments spring to mind. 

1. The help file for -foreach-, although packed with good detail, may be supplemented with (e.g.) 

SJ-2-2  pr0005  . . . . . .  Speaking Stata:  How to face lists with fortitude
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q2/02   SJ 2(2):202--222                                 (no commands)
        demonstrates the usefulness of for, foreach, forvalues, and
        local macros for interactive (non programming) tasks

which is freely available, and available for free, from the Stata Journal website. 

2. An overarching fact is that working this way Steve is limited to whatever -egen- functions are available for rowwise statistics, or can be cloned from those. For many, many statistical problems he would be better off after -reshape long-. 

Nick 
[email protected] 

Matthew White

Yes, though you'll want to reverse the list to get the order of the
variables the same as before:

foreach stat in mean max min {

Alternatively:

-start code-

local y v1_ v2_ v3_ v4_ ... v16_
loc stats min max mean

foreach x of local y {
	foreach stat of loc stats {
            egen `x'`stat' = row`stat'(`x'A - `x'C)
	    order `stat', before(`x'C)
	}
	order `x'C, before(`x'`:word 1 of `stats'')
}

- end code-

On Thu, Nov 10, 2011 at 4:06 PM, Steve Nakoneshny <[email protected]> wrote:

> Thank you for the quick response! Despite -help foreach- and the two Stata Press books in front of me, I just wasn't getting it.
>
> After modifying my do file as per you suggestion, I realized I could also incorporate the -order- command into the nested loop thus:
>
> -start code-
>
> foreach x of local y    {
>        foreach stat in min max mean {
>                egen `x'`stat' = row`stat'(`x'A - `x'C)
>                order `x'`stat', after(`x'C)
>        }
> }
>
> -end code-

On 2011-11-10, at 1:49 PM, Matthew White wrote:

>> -egen `x'`a' = row`a'(`x'A - `x'C)- is exactly along the lines you
>> want. You could try:
>>
>> -start code-
>>
>> local y v1_ v2_ v3_ v4_ ... v16_
>> local i 0
>>
>> foreach x of local y {
>>       foreach stat in min max mean {
>>           egen `x'`stat' = row`stat'(`x'A - `x'C)
>>       }
>>     order `x'min `x'max `x'mean, after(`x'C)
>> }
>>
>> - end code-
>>
>> Nothing beats reading the help file: -help foreach-.

On Thu, Nov 10, 2011 at 3:41 PM, Steve Nakoneshny <[email protected]> wrote:

>>> I'm working with a dataset of biomarker data. This dataset contains triplicate scores on 16 unique biomarker measures for a total of 48 variables. Each measure has a unique name (obviously) and each scoring of these appends a letter to the end of the varname like this: v1_A v1_B v1_C ... v16_A v16_B v16_C.
>>>
>>> Wanting to calculate new variables for min max and mean scores and then order these results relative to each biomarker measure, I thought I would write a loop to do so rather than write ~64 lines of code.
>>>
>>> This is what I did:
>>>
>>> -start code-
>>>
>>> local y v1_ v2_ v3_ v4_ ... v16_
>>> local i 0
>>>
>>> foreach x of local y    {
>>>        local ++i
>>>                egen `x'min = rowmin(`x'A - `x'C)
>>>                egen `x'max = rowmax(`x'A - `x'C)
>>>                egen `x'mean = rowmean(`x'A - `x'C)
>>>                order `x'min `x'max `x'mean, after(`x'C)
>>> }
>>>
>>> - end code-
>>>
>>> As far as I can tell, the code executes exactly as I wanted and I now have my new variables created and ordered as planned. What is bugging me though is thinking that I could combine the -egen- commands into a loop and nest it into the larger loop.
>>>
>>> I suspect that I would first have to create -local a min max mean- and rewrite the -egen- command as -egen `x'`a' = row`a'(`x'A - `x'C)- (and then delete the two superfluous -egen- commands). However, that in and of itself will not be sufficient and I don't know enough to provide the appropriate syntax to have the loop execute correctly. I am still relatively new to using loops and would appreciate some suggestions.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index