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

st: efficiency of coding


From   "NEYMOTIN, FLORENCE" <[email protected]>
To   [email protected]
Subject   st: efficiency of coding
Date   Sun, 17 Jul 2005 13:50:50 -0700

I have two questions for you:

(1) I have a piece of code which I think is probably pretty inefficient and I was wondering whether there is a more efficient alternative. I want to have the log of the median of w conditional on an indicator being set on (denoted as I0, I1, or I2) saved in a scalar variable. I did this by creating a variable denoting the median and then just setting using the first entry of this temporary variable as the scalar and then using that next value to get the log. I think this is probably really roundabout and not taking advantage of stata's capabilities. thanks! Right now, the code looks like:

----------------------------------------------------------------
forvalues i=0(1)2 {

tempvar temp`i'
egen `temp`i''=median(`w') if `I`i''
gsort -`I`i''

*note, the reason this works is because this will always be the first observation after doing the sort, and locals get the first value of variable..TRICKY..

local tempweird=`temp`i''

*THIS IS THE MAIN DEFINITION LINE RIGHT HERE
scalar lw`i'=log(`tempweird')


drop `temp`i''
local drop tempweird
}

----------------------------------------------------------------

(2) If I have two matrices A and B, where A is 15x3, and B is 15x1, and I want to make the third column of A EQUAL to the B matrix, is there a quick way to do this? Right now, I have it replacing value by value in the matrix but I think this is probably slower than it could be.
The code now looks like:

forvalues i=1(1)15{
matrix A[`i' , 3]= B[`i',1]
}
----------------------------------------------------------------
Thanks for your help!
Florence
*
* 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