Bookmark and Share

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.


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

Re: st: loop within a loop


From   Steve Nakoneshny <scnakone@ucalgary.ca>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: loop within a loop
Date   Thu, 10 Nov 2011 14:06:00 -0700

Hi Matt,

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-

Thanks again,
Steve Nakoneshny
scnakone@ucalgary.ca




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

> 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 <scnakone@ucalgary.ca> 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
>> scnakone@ucalgary.ca
>> 
>> 
>> 
>> 
>> 
>> *
>> *   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/


*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index