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?