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

Re: st: Advantage of string scalars over macros


From   [email protected] (Alan Riley)
To   [email protected]
Subject   Re: st: Advantage of string scalars over macros
Date   Fri, 02 Jul 2004 11:10:15 -0500

Joseph Coveney ([email protected]) asked about the new string scalars:
> Yesterday's update introduces string scalars.  
> 
> Alright, I'll bite:  what's the advantage of a string scalar over a macro?

The advantage of string scalars is that they do not undergo macro
expansion.  When a macro is expanded, any character sequences in it
that also look like macros are also expanded by Stata.

For example, consider

   . global title = "Gas price per gallon in $US"

vs

   . global title = "Gas price per gallon in \$US"

Anyone who has dealt much with macros knows that to get a macro
expansion character such as a dollar sign into a macro (or into anything
else in Stata), you must escape it with a backslash.  If you did not
do this, Stata would see $US, look for a global macro named 'US', not
find it, and therefore substitute nothing in its place.  The backslash
tells Stata to treat the $ as a real $ and not the global macro
expansion character.

Look at what is in the macro 'title' after each of the above commands:

   . global title = "Gas price per gallon in $US"

   . macro list title
   title:          Gas price per gallon in

vs

   . global title = "Gas price per gallon in \$US"

   . macro list title
   title:          Gas price per gallon in $US


Great, so the second one is what you want to do to be able to
get a dollar sign into your macro named title.  But, what happens
when you later need to use that macro?

   . global title = "Gas price per gallon in \$US"

   . macro list title
   title:          Gas price per gallon in $US

   . display "$title"
   Gas price per gallon in

What happened?  Stata did exactly what you asked it to.  $title was
replaced with the contents of the global macro 'title'.  Any further
macro expansions that could be done, were:

   . display "$title"

becomes

   . display "Gas price per gallon in $US"

which then becomes

   . display "Gas price per gallon in "

resulting in the output above.  This is unfortunate but unavoidable.


String scalars solve this problem:

   . scalar title = "Gas price per gallon in \$US"

   . display title
   Gas price per gallon in $US

Note that we still had to escape the dollar sign with a backslash on
initial input.  But, from that point on, any time we refer to the
scalar 'title' in a string expression, no macro expansion will take
place.


None of Stata's official ado-files take advantage of string scalars yet.
It will take time for us and for users to become accustomed to using them
and to find when it is better to use them vs. macros.  They do take a
different mindset to use than macros, as they may only be used in a
context that accepts a string expression.  Macros, on the other hand,
may be used anywhere at any time--they just expand in place.

Macros have the advantage of being able hold far longer sequences of
characters than string scalars, so scalars should not be used for
things like variable lists.  There is no real need to use them for
variable lists anyway.  The typical use of string scalars would be
for examples such as the one above involving short pieces of text
intended to be output later.


--Alan
([email protected])
*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index