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

st: Question about a loop


From   Cameron Hooper <[email protected]>
To   [email protected]
Subject   st: Question about a loop
Date   Tue, 5 Jul 2005 16:58:59 -0400

Say I have 10 variables named: num1, num2, ..., num10. I want to loop over these variables and create 10 new ones named: op1, op2, ..., op10. The following works:

local x 1
foreach var of varlist num* {
gen op`i' = sum(ret) if inlist(num`i',-1,0,1)
local i = `i' + 1
}

I was originally thinking of accessing each of the num* variable's name, extracting the last digit and appending this to the stub 'op' and thus create a new variable. But I don't know to access a variables name. Can such a thing be done? Can anyone suggest a better way of executing this code?

Thanks,

Cameron

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