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 |
Nick Cox <n.j.cox@durham.ac.uk> |

To |
"'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu> |

Subject |
RE: st: construct a loop |

Date |
Mon, 21 Feb 2011 19:01:30 +0000 |

The help for -egen, group()- tells you about its -label- option. You really could have looked at that first! foreach v in var1 var2 var3 { egen group=group(`v') sum group, meanonly forval i = 1/`r(max)' { di "`v': `: label (group) `i''" stmh hivstatus if group ==`i' } drop group } is my guess at the other question. See -help extended_fcn- for the -label- stuff. Nick n.j.cox@durham.ac.uk Nikolaos Pandis Dear Nick, Thank you. ******************** egen group=group(age_cat gender) sum group ,meanonly foreach i of num 1/`r(max)' { stmh hivstatus if group ==`i' } ************************** I have added another variable and I get 6 combinations since age_cat has 3 levels (0,1,2) and gender 2 (0,1). Should I assume that the sequence of the combinations is: age_cat:gender 00 01 10 11 20 21? Could we label them? Also, what if I do not want the combinations but rather to run the same code below for a number of variables separately. ******************** egen group=group(var1) sum group ,meanonly foreach i of num 1/`r(max)' { stmh hivstatus if group ==`i' } egen group=group(var2) sum group ,meanonly foreach i of num 1/`r(max)' { stmh hivstatus if group ==`i' } egen group=group(var3) sum group ,meanonly foreach i of num 1/`r(max)' { stmh hivstatus if group ==`i' } ect ************************** How would I be able to consolidate the above 3 pieces of code into 1? Many thanks, Nick --- On Mon, 2/21/11, Nick Cox <n.j.cox@durham.ac.uk> wrote: > From: Nick Cox <n.j.cox@durham.ac.uk> > Subject: RE: st: construct a loop > To: "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu> > Date: Monday, February 21, 2011, 8:12 PM > This too is covered in the FAQ just > cited. > > "It extends easily to the apparently much more difficult > problem of going through all the distinct combinations of > two or more variables, which is, in fact, only a little more > difficult. > . egen group = group(varlist) > > Now the code is exactly the same as before. That is, > instead of one varname, you just need to spell out a varlist > in the argument to egen." > > Nick > n.j.cox@durham.ac.uk > > > Nikolaos Pandis > > > Thank you. I came up with the following code: > > ********************* > egen group=group(age_cat) > > sum group ,meanonly > > foreach i of num 1/`r(max)' { > stmh hivstatus if group ==`i' > } > **************** > > How could I make this to do the same thing for other > variables such as age_cat? > > > --- On Sun, 2/20/11, Nick Cox <njcoxstata@gmail.com> > wrote: > > > Well, your solution then gets more > > complicated. The thread has already > > shown that you can nest loops. Also see > > > > FAQ . . . . . . . . . . Making > > foreach go through all values of a variable > > . . . . . . . . . . . . . . . . > > . . . . . . . . . . . . . . N. J. Cox > > 8/05 Is there a > > way to tell Stata to try all values of a > > > > particular variable in a foreach statement without > > > > specifying them? > > http://www.stata.com/support/faqs/data/foreach.html > > > > > On Sat, Feb 19, 2011 at 8:48 PM, Nikolaos Pandis > <npandis@yahoo.com> > > wrote: > > > > > Thank you all this looks great! > > > > > > Now what if I would like to run the same commands > for > > more than just the varX variable? > > > What if the variables have variable number of > category > > levels? > > > > > > Many thanks, > > > > > > Nick > > > > > > > > > > > > --- On Sat, 2/19/11, Nick Cox <njcoxstata@gmail.com> > > wrote: > > > > > >> From: Nick Cox <njcoxstata@gmail.com> > > >> Subject: Re: st: construct a loop > > >> To: statalist@hsphsun2.harvard.edu > > >> Date: Saturday, February 19, 2011, 9:14 PM > > >> Here is another way to do it. > > >> > > >> foreach p in "1 0" "2 0" "3 0" "2 1" "3 1" "3 > 2" > > { > > >> tokenize "`p'" > > >> stmh varX, c(`1' , `2') > > >> } > > >> > > >> Nick > > >> > > >> On Sat, Feb 19, 2011 at 12:03 PM, Nikolaos > Pandis > > <npandis@yahoo.com> > > >> wrote: > > >> > > >> > I would like to run the following > commands > > using the 4 > > >> level varX: > > >> > stmh varX,c(1,0) > > >> > stmh varX,c(2,0) > > >> > stmh varX,c(3,0) > > >> > stmh varX,c(2,1) > > >> > stmh varX,c(3,1) > > >> > stmh varx,c(3,2) > > >> > > > >> > I would grateful if someone would help > me > > construct a > > >> loop to run all the above commands. * * 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: construct a loop***From:*Nikolaos Pandis <npandis@yahoo.com>

**References**:**RE: st: construct a loop***From:*Nick Cox <n.j.cox@durham.ac.uk>

**RE: st: construct a loop***From:*Nikolaos Pandis <npandis@yahoo.com>

- Prev by Date:
**RE: st: best practice for dates and times** - Next by Date:
**st: Read variable labels** - Previous by thread:
**RE: st: construct a loop** - Next by thread:
**RE: st: construct a loop** - Index(es):