[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: "looping" within a command |

Date |
Wed, 13 Oct 2004 15:11:19 +0100 |

I take it from this that you always have a -name- and -id- variable: what differs are the number of -a*-. If you know N in advance you could build up your -infix- call like this: local N = <whatever> local call "str name 124-143 str id 144-152" local j = 217 forval i = 1/`N' { local call "`call' str a`i' `j++'" } then infix `call' using rawdata.txt e.g. ------------------ readin.ado local N = `1' local call "str name 124-143 str id 144-152" local j = 217 forval i = 1/`N' { local call "`call' str a`i' `j++'" } infix `call' using rawdata.txt ------------------ do readin 12 or do readin 24 or whatever. The concatenation can be done by egen answer = concat(a*) irrespective of how many a* there are. Nick n.j.cox@durham.ac.uk Devra Golbe > For want of a better, term, I describe my question as how to > loop inside a > single command. I don't see how to make foreach or > forvalues work for > this kind of problem, nor has browsing the programming > manual, the FAQs, or > Nick Cox's Stata Journal articles on lists turned up a solution. > > The command uses the arguments x1...xN, and I would like to > be able to > treat N as a variable. For instance, I need to read a > fixed-format text > file which contains a number N of string variables; N varies > each time I > run the program. The following infix statement does the job for N=5: > > infix /// > str name 124-143 > /// > str id 144-152 /// > str a1 217 str a2 218 str a3 219 str a4 220 str a5 221 > /// > using rawdata.txt > > The data are further massaged by some statements of the form > > gener str b*=function(a*) > > gen str5 answer1= b1+b2+b3+b4+b5 > > However, N varies from say 25 to 100 (the variables are answers on a > multiple choice test), so I would prefer to use some kind of > structure > which allows me to set N once, and then build up each > command without > typing the full list of arguments x1...xN. > * * 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: "looping" within a command***From:*Devra Golbe <dgolbe@optonline.net>

- Prev by Date:
**Re: st: Combining two variables** - Next by Date:
**Re: st: IVProb** - Previous by thread:
**st: Combining two variables** - Next by thread:
**Re: st: RE: "looping" within a command** - Index(es):

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