Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Simple and weird error


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: Simple and weird error
Date   Wed, 21 Nov 2012 23:09:25 +0000

This can't be literal code, as there are upper case letters and spaces
that would prevent this from running. Also, the local macro references
should all be of the form `macname' with different opening and closing
single quotation marks.

With a stylistic twist (one -quietly- instead of three) the code can
be corrected and rewritten

quietly foreach varname of varlist `r(varlist)' {
        replace `varname'  = log(`varname')
        gen lr`var' = `varname' - `varname'[_n-1]
        drop `varname'
}

But this wouldn't work either. `var' is the same each time round the
loop: either blank or whatever it was defined as before this code
segment. So, the code would fail second time round the loop.

Hence I give up. Your code as you report couldn't do more than
-generate- one variable, even with several corrections.

I suggest that you post your real code.

Nick

On Wed, Nov 21, 2012 at 10:54 PM, Zhangqunzi <[email protected]> wrote:

> I am trying to calculate the log return lr
> according to the code below:
>
> For each varname of varlist 'r(varlist)'{
> Quiet replace 'varname' =log('varname')
>
> Quiet gen lr'var'='varname'[_n]-'varname'[_n-1]
>
> Quiet drop 'varname'
> }
>
> varname is price1,2,3 etc for each firm.
>
> In the end I found all the log return I get is positive which is clearly wrong for 5000 firms and 30years.
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index