Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: more efficient techniques


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: more efficient techniques
Date   Tue, 18 Jul 2006 12:11:25 +0100

I am not sure quite what you are looking for here. 
I note that your code can be compressed to 

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'
     replace hsecostw = valm5/`x' if valm5>=0 & m5b==`i'
     local ++i
}

Quite how you would extend the process to make the code
more general would depend on stuff out of sight in this
posting. 

However, efficiency in terms of Stata code is not much 
of a gain if it takes you longer to understand your own 
code when you revisit it much later. 

An orthogonal tip arises from my affection for alliteration. 
I often code -foreach- loops with code like 

foreach v of var <varlist> { 


} 

or 

foreach n of numlist <numlist> { 
	
} 

and conversely I find that using x has no 
mnemonic associations that make it attractive 
here. 

Nick 
n.j.cox@durham.ac.uk 

> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu
> [mailto:owner-statalist@hsphsun2.harvard.edu]On Behalf Of Michael
> Alexander
> Sent: 18 July 2006 07:43
> To: statalist@hsphsun2.harvard.edu
> Subject: st: more efficient techniques
> 
> 
> 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
> }
> 
> -- 
> Michael Alexander
> Principal Research Fellow
> Australian Institute of Family Studies
> 
> 300 Queen Street 
> Melbourne VIC 3000
> 
> Tel: 03 9214 7841
> Fax: 03 9214 7839
> Mob: 0419 406 078
> Email: Michael.Alexander@aifs.gov.au
> Web-site: www.aifs.gov.au
> 
> *
> *   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/
> 

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index