Statalist


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

st: Declare and class field initialisation with an r()-value


From   Sergiy Radyakin <[email protected]>
To   "[email protected]" <[email protected]>
Subject   st: Declare and class field initialisation with an r()-value
Date   Sun, 25 Jan 2009 22:43:30 -0500

Dear All,

is this a bug in Stata?

sysuse auto
.MyObject=.object.new
summarize price
.MyObject Declare double AvPrice1 = r(mean)
.MyObject Declare double AvPrice2 = 6165.256756756757
classutil des .MyObject

Output:
object .MyObject:
   classwide:
        dbstyle      .dbstyle
   .Declare:
        double       .AvPrice1      = .
        double       .AvPrice2      = 6165.256756756757

I expected .MyObject.AvPrice1 to be identical to .MyObject.AvPrice2.
It seems that -Declare- destroys the r()-saved results and causes
incorrect object's field initialization to a missing.
In particular, -Declare- saves to the r() results the macros "key" and
"name", which accidentially could be the ones that the user attempts
to initialize with, e.g. Then the field will be initialised to a
"__key(memory address)" or "newfieldname" correspondingly, which is
not what was intended. The problem is hard to find, since Declare is a
built-in procedure and does not show up in -trace-.

Current workaround seems is to assign the r() result to a temporary
local, then initialize the field.

summarize price
local avprice=r(mean)
.MyObject Declare double AvPrice3 = `avprice'

Or declare the field in one statement and initialize in another, like so:

.MyObject Declare double AvPrice4
summarize price
.MyObject.AvPrice4 = r(mean)

(also submitted to tech-support)

Thank you,
    Sergiy Radyakin
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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