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   Matthew White <[email protected]>
To   [email protected]
Subject   Re: st: loop within a loop
Date   Thu, 10 Nov 2011 15:49:38 -0500

Hi Steve,

-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-.

Best,
Matt

On Thu, Nov 10, 2011 at 3:41 PM, Steve Nakoneshny <[email protected]> wrote:
> Dear Statalist,
>
> 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.
>
> Thanks,
> Steve Nakoneshny
> [email protected]
>
>
>
>
>
> *
> *   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/
>



-- 
Matthew White
Data Coordinator
Innovations for Poverty Action
101 Whitney Avenue, New Haven, CT 06510 USA
+1 434-305-9861
www.poverty-action.org

*
*   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