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

Subject |
st: RE: Programming advice on generating loops |

Date |
Wed, 2 Apr 2008 16:44:03 +0100 |

Tiago Pereira seconded this question. Neither Le Wang nor Tiago gave any underlying context or details of what else they want to do. Your question, on the face of it, would be answered by writing a program to write a program, given the variables concerned. Writing something to emit scaffolding (i.e. the -forval- loops and the closing braces) would be much easier than writing something that generates code to fit around the scaffolding (any initialisations and wrap-up code, and what goes inside each loop). It would probably be _much_ easier just to write the program directly. I doubt very much, however, that you really need what you are asking for. The key here is that you don't know in advance how many variables you have to do with. That suggests to me that there is no special structure to your problem(s), but just a desire to crunch through all the distinct cross-combinations of a set of variables. If that is so, your problem(s) may well yield to -by:- and/or looping over composite groups. There is technique at FAQ . . . . . . . . . . Making foreach go through all values of a variable 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 For example, egen group = group(varlist) su group, meanonly forval i = 1/`r(max)' { ... } applies regardless of the number of variables in varlist. That said, there have been attempts to set up more general tools in this territory. Those by myself predate or do not supersede the direct use of -forval- or -foreach- (e.g. the -cp- tool in STB-45). I would rather know more than the real problem(s) than try to make further guesses. Nick n.j.cox@durham.ac.uk Le Wang I need to generate loops according to the number of variables (unknown beforehand) in the ado program. For example, if one uses two variables, then I want to do the following loop forvalues i=1/10{ forvalues j=1/32{ ....... } } if one specifies three variables, then the loop looks like this forvalues i=1/10{ forvalues j=1/32{ forvalues k=1/47{ ....... } } } and so on.... How can I do this? Thanks in advance. * * For searches and help try: * http://www.stata.com/support/faqs/res/findit.html * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

**Follow-Ups**:**Re: st: RE: Programming advice on generating loops***From:*"Le Wang" <statauser@gmail.com>

**References**:**st: Programming advice on generating loops***From:*"Le Wang" <statauser@gmail.com>

- Prev by Date:
**Re: st: Stata graph how to?** - Next by Date:
**RE: st: Stata graph how to?** - Previous by thread:
**st: Programming advice on generating loops** - Next by thread:
**Re: st: RE: Programming advice on generating loops** - Index(es):

© Copyright 1996–2015 StataCorp LP | Terms of use | Privacy | Contact us | What's new | Site index |