For anyone who has become frustrated when using macros in STATA this is a short guide. The problem in mind is writing code to loop through different filenames and paths in do files and programs, but am sure it has other applications. Those with a programming background may find this obvious, but I certainly didn't...
Macros
Macros are strings that are stored in memory under a designated label. Macros can also be numbers, but STATA likes to store them in string format. They can be used as numbers, but are stored as strings.
Macros are of 2 types,
locals and
globals.
Local Macros are local in the sense that they are not retained beyond the do file or stata program in which they are declared.
To set a local macro:
local x = "a string"
The quotes are important here. They are not necessary when there are no spaces or other awkward characters (such as ") present. Details on quotation use in stata can be found
here.
When we wish to use the macro we refer to it using STATA's special quote marks `x'.
Typing `x' is exactly the same as typing whatever you have set `x' to.
e.g. typing:
display `x' results in an error as if display astring had been typed
display "`x'" displays astring, just as if display "astring" had been typed.
Numbers are treated in a slightly different manner, numbers stored as macros can be used as numbers or strings. e.g.
local y = 2
display `y'
2
display "`y'"
2
display "`y'"+1 21
display `y'+13
Global macros are retained in memory until expressly dropped or overwritten. Global macros are typically used for filepaths and similar static variables. They are used slightly differently:
global x = "abcdef" (quotes not necessary here)
display "$x"
abcdef
The dollar sign signifies the use of the macro. Again we need to use quotation marks to signal its use as a string when it is displayed/used. Otherwise they are used in a pretty similar manner.
Concatenation (combining of strings).
This is often done in loops, for example when outputting simulation results or reading in concurrent datafiles; file1, file2 etc.
Local macros are easily combined when we remember that calling the macro is exactly the same as typing the string stored.
local x="a string"
local y="is born"
local z = "`x' `y'" di "`z'"
a string is born
Other text strings can be easily incorporated into the new macro:
local q = "`x' vest `y'"
di "`q'"
a string vest is born
Global macros can be combined in much the same way:
global filepath "C:\Documents and Settings\username\examples\"
global filename "testdata"
global suffix ".dta"
display "$filepath$filename$suffix"
C:\Documents and Settings\username\examples\testdata.dta
Something that you can't do quite as easily with global macros is the incorporation of a text string. e.g.
global filename "testfile"
global suffix ".dta"
display $filename1$suffix
.dta
Here STATA can't find $filename1, we need to use quote marks to separate the three strings:
display "$filename""1""$suffix"
testfile1.dta
We can combine local with global macros. This is useful when we wish to loop through filenames
forvalues x = 0(1)10 {
use "$filepath$filename`x'$suffix",clear
}
Hope this was helpful.