[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Variables, macros, locals, globals

From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: Variables, macros, locals, globals
Date   Thu, 28 Aug 2008 15:08:36 +0100

Some recent threads indicate that even some people trying to write Stata
programs can get very confused on what exactly is a variable, etc. 

At some point it becomes extremely inefficient not to read the help
files and manuals and try to internalise Stata's terminology. Most
importantly, your questions may not make much sense to other people
otherwise. There will be lots of little pleasurable bursts of
understanding as stuff that puzzled you makes sense for the first time.

Here is an attempt to put down some basics, with particular stress on
some common confusions. Contrary to appearances, I won't try to tell the
complete truth, as explaining some minor exceptions or complications
would probably help less than keeping quiet about them. Also, a glossary
could go on and on. In this case, I stopped short of explaining scalars,
or matrices, or ....


1. In Stata, the terminology used by StataCorp counts. Stata has no way
of knowing which terminology you, the user, regard as standard, natural,
obvious, intuitive, correct, familiar, charming, congenial or otherwise

2. On Statalist, rule 1 also applies. We don't necessarily know, and we
are not obliged to care, that you are really or mostly a user or
programmer of some other software or language and thus gravitate
inevitably towards the terminology thereof. 

3. What is true for Stata is not necessarily true for Mata. 


A variable is a column or field in your dataset. It takes on a single
value for each observation in your dataset. Stata's use of the term,
although precise, should not be at odds with its general use within

Confusion: Some people used to programming get accustomed to thinking of
whatever holds a single value as a variable. Thus in lots of languages
you can say something like 

x = 42 

and x is called a variable. Macros and scalars are typically used for
that purpose in Stata. Neither macros nor scalars are correctly called
variables in Stata. 


A macro in Stata is a named object that holds a single string value.
Macros thus have names and contents. 

The string can consist of numeric characters. Thus holding a string "42"
is a way to hold the corresponding number 42. Users of such macros tend
to think of them as containing numeric values. That usually is a
convenient and helpful way to regard such macros, but in no sense is
that intrinsic to their definition. 

Macros can be local or global. Local macros and global macros are often
informally called locals or globals respectively.

Confusion: In some other software the term macro is often used, either
formally or informally, for a program or script that is written
containing a sequence of commands. That is not standard usage in Stata.
On occasion it could be that a Stata macro contains a program or do file
name, but that is as may be. 


Locals are, properly, local macros. 

Local refers both to the command -local- that defines a local macro and
to the fact that such macros are only visible locally, meaning within
the same program, do file, do-file editor contents or interactive
session. Conversely a local macro is not visible within a different
program, do file, do file editor contents or interactive session. Thus
macros may be defined with the same name in different locations; such
macros are completely different beasts. 

Confusion: Common references to locals, without the name macro, should
not lead you to think that locals are a kind of variable. "Local
variable" is a solecism in Stata. 


Globals are, properly, global macros. 

Global refers both to the command -global- that defines a global macro
and to the fact that such macros are visible everywhere, or globally,
meaning within any program, do file, or do-file editor contents and
within an interactive session.  

Confusion: Common references to globals, without the name macro, should
not lead you to think that globals are a kind of variable. "Global
variable" is a solecism in Stata.

*   For searches and help try:

© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index