Bookmark and Share

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.


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

Re: st: RE: Question about scalars


From   Steve Samuels <sjsamuels@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: RE: Question about scalars
Date   Mon, 9 Aug 2010 14:35:53 -0400

Thanks, Nick!

Steve

On Mon, Aug 9, 2010 at 2:05 PM, Nick Cox <n.j.cox@durham.ac.uk> wrote:
> Scalars and variables share the same namespace.
>
> What's more, in case of ambiguity the variable name interpretation
> always trumps the scalar name interpretation.
>
> In the auto data, -d- means the variable -displacement-.
>
> Tempnames solve this for a different reason, that temporary names are
> guaranteed unique.
>
> This is all documented, e.g.
>
> SJ-6-2  dm0021  . Stata tip 31: Scalar or variable? Problem of ambiguous
> names
>        . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  G. I.
> Kolev
>        Q2/06   SJ 6(2):279--280                                 (no
> commands)
>        tips for avoiding abbreviation conflicts with variables
>        when naming scalars
>
> and the manual references it gives, but you are in good company in being
> bit by this.
>
> Nick
> n.j.cox@durham.ac.uk
>
> Steve Samuels
>
> In the code below, I generate a scalar "d" from a regression result;
> it looks okay, but trying to add it to another scalar doesn't work.
> If, however I use a -tempname- , I get the right answer.  Could
> someone explain to me why the first approach doesn't work and if
> there's another approach that doesn't involve a -tempname-?
>
> ******************************
> clear
> scalar drop _all
> scalar a = 10
> scalar b = 20
> scalar c = b + a
> scalar dir
>
> sysuse auto, clear
> reg mpg foreign
> scalar d = _b[foreign]
> scalar e = d + a
> scalar dir a d e // "e" is wrong
>
> tempname dd
> scalar `dd' = _b[foreign]
> scalar e = `dd' +a
> scalar dir a `dd' e // "e" is /okay
> ****************************
>
> *
> *   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/
>



-- 
Steven Samuels
sjsamuels@gmail.com
18 Cantine's Island
Saugerties NY 12477
USA
Voice: 845-246-0774
Fax:    206-202-4783

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