Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

st: RE: Simple and weird error

 From "Sarah Edgington" To Subject st: RE: Simple and weird error Date Wed, 21 Nov 2012 15:22:34 -0800

```First off, lr`var' is going to evaluate as just lr since `var' hasn't been
defined.  You'll end up with a single variable lr that consists just of the
comparisons from the last variable in r(varlist).

One thing I find useful to do when I don't understand why something isn't
working is to keep some intermediate variables and then actually look at the
data.  Another thing I sometimes find useful when I'm having trouble is to
make sure an operation does what I want it to do using a single variable
before I write out the loop.

So I might try something like:
gen logprice1=log(price1)
gen lrprice1=logprice1-logprice1[_n-1]
list price1 logprice1 lrprice1 in 1/10

That way you can see exactly what's going on and make sure it's what you
want.

It's entirely possible to get all positive results for that operation for a
given variable if your data is sorted on that variable.   Without knowing
anything about your data it's hard to tell if that's the source of your
problem.
This calculation will compare a row of data to the row before it with no
regard for whether that's a sensible comparison or not.  So make sure you've
sorted your data in a way that makes sense first.

Hope that helps.

-Sarah

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Zhangqunzi
Sent: Wednesday, November 21, 2012 2:55 PM
To: Stats
Subject: st: Simple and weird error

Dear All,

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/

*
*   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/
```