* Nested localslocal x1 = 1local x2 = 5local x3 = 10local i = 1local j = 3display`x1'// Stata displays local x1.display`x`i''// Stata first expands `x`i'' to `x1' and then to 1 which is the content of x1.display`x`j''// Stata first expands `x`j'' to `x3' and then to 10 which is the content of x3.display`x'`j'// Stata first expands `x'`j' to `j' because no local x is defined (hence considered empty) and then to 3 which is the content of j.* Nested globalsglobal x1 = 1global x2 = 5global x3 = 10global i = 2display"$x$i"// Stata first expands $x to nothing and then $i to 2.display"${x$i}"// Stata first expands $i to 2 and then $x2 to 5.* Globals followed by textlocal a number
global a number
display"`a'1"// Stata displays number1, as the apostrophe marks the end of the local.display"$a1"// Stata displays nothing because it is looking for the global a1 which does not exist.display"${a}1"// Stata display number1 because the curly brackets mark the end of the global.* Linking macrosglobal varlist var1 var2
global newlist $varlist var3
display"$newlist"// Results in var1 var2 var3.global varlist var1
display"$newlist"// Still results in var1 var2 var3 because the macros are not linked.global varlist var1 var2
global newlist \$varlist var3
display"$newlist"// Results in var1 var2 var3.global varlist var1
display"$newlist"// Results in var1 var3 because Stata first expands varlist and then newlist.