Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: "looping" within a command


From   Devra Golbe <[email protected]>
To   [email protected]
Subject   st: "looping" within a command
Date   Wed, 13 Oct 2004 09:27:02 -0400

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.

Thanks for any insight you can provide.

Devra

______________________________________________________________
Devra L. Golbe, Professor
Dept. of Economics
Hunter College / CUNY
695 Park Ave., NY, NY 10021
voice: 212-772-5408
fax: 212-772-5398 (office)
208-361-5964 (e-fax)

http://econ.hunter.cuny.edu/faculty/golbe

*
* 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/




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