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]

From |
Matthew White <mwhite@poverty-action.org> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: loop within a loop |

Date |
Thu, 10 Nov 2011 16:24:06 -0500 |

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- Best, Matt On Thu, Nov 10, 2011 at 4:06 PM, Steve Nakoneshny <scnakone@ucalgary.ca> wrote: > 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/ > -- 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/

**Follow-Ups**:**RE: st: loop within a loop***From:*Nick Cox <n.j.cox@durham.ac.uk>

**References**:**st: loop within a loop***From:*Steve Nakoneshny <scnakone@ucalgary.ca>

**Re: st: loop within a loop***From:*Matthew White <mwhite@poverty-action.org>

**Re: st: loop within a loop***From:*Steve Nakoneshny <scnakone@ucalgary.ca>

- Prev by Date:
**Re: st: svy + aweights** - Next by Date:
**Re: st: svy + aweights** - Previous by thread:
**Re: st: loop within a loop** - Next by thread:
**RE: st: loop within a loop** - Index(es):