| |
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
Re: st: more efficient techniques
I think any more general solution (Nick has pointed at the best
particular solution here) would require naming your variables
in a consistent and iterative manner. For instance, if you
suffixed your valm* and m* variables appropriately, you could
code:
foreach s in numlist 1 2 3 4 {
local i=1
foreach x of numlist 1 2 12 52 {
replace hsecostw = valm`s'/`x' if valm`s'>=0 & m`s'==`i'
local ++i
}
}
This would not only improve clarity & efficiency, but would likely
be less error prone.
HTH,
Jeph
Michael Alexander wrote:
Hi Statalist
In the interests of learning some more efficient programming techniques,
I am wondering anyone can suggest a way to improve the following code.
Ideally, I would like to do away with the second -foreach- statement,
which is just a repeat of the first with arguments changed (valm5 for
valm3 and m5b for m4). In this case it was no big deal to just copy the
first statement and amend its contents, but if I had 20 occurences then
I would obviously want to automate the process. I assume a solution
would have to do with using an additional -foreach- statement but not
quite sure how this would work when I have two arguments that need to
change on each loop through.
Thanks in anticipation
gen hsecostw=0 if inlist(m1, 1, 6, 7, 8)
local i=1
foreach x of numlist 1 2 12 52 {
replace hsecostw = valm3/`x' if valm3>=0 & m4==`i'
local ++i
}
local i=1
foreach x of numlist 1 2 12 52 {
replace hsecostw = valm5/`x' if valm5>=0 & m5b==`i'
local ++i
}
*
* 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/