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

st: Brackets: ok for macros, very bad for scalars


From   D H <hiorlow@sbcglobal.net>
To   statalist@hsphsun2.harvard.edu
Subject   st: Brackets: ok for macros, very bad for scalars
Date   Wed, 2 Nov 2005 11:18:51 -0800 (PST)

correct?) [Stata 8.2]
 
To my surprise, using brackets to indicate observation
number works with local and global macros, but not
with the scalar command.
 
As an example, consider the following dataset:
 
. list
 
     +------------------------+
     | year   unemp    gdpgap |
     |------------------------|
  1. | 1986     6.6   -.00888 |
  2. | 1987     5.7    .00569 |
  3. | 1988     5.3    .01402 |
  4. | 1989     5.4    .01267 |
  5. | 1990     6.3   -.01081 |
     |------------------------|
  6. | 1991     7.3   -.02822 |
  7. | 1992     7.4   -.01411 |
  8. | 1993     6.5   -.01542 |
<snip>
 
. global yy=unemp[5]
 
. di $yy
6.3000002
 
So far so good (ignoring the problem with the float
datatype).  Now try the same with a scalar command:
 
. scalar y=unemp[5]
 
. di y
1986
 
Here, Stata inexplicably appears to insert the upper
left hand observation from the dataset, which happens
to correspond to "year" rather than "unemp".
 
The following will work properly though:
 
. scalar z=unemp in 5
 
. di z
6.3000002
 
Any comments?  
 
What if I want to make a scalar a function of two
different observations?  (I suppose in the latter case
I could grab the observations with a local macro, then
insert the values into the scalar, but then I would
lose any precision or speed advantages of the scalar
method).  
 
As the fine manual notes, macros provide up to 13
significant digits in scientific notation, while
scalars provide more.  Macros are also somewhat
slower.  If neither of these factors is especially
important, perhaps the casual programmer might want to
stick with macros.

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