11.07.2015 Views

[U] User's Guide

[U] User's Guide

[U] User's Guide

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

[ U ] 18.3 Macros 21518.3.10 Advanced global macro manipulationGlobal macros are rarely used, and when they are used, it is typically for communication betweenprograms. You should never use a global macro where a local macro would suffice.1. Constructions like $x$i are expanded sequentially. If $x contained this and $i 6, then $x$iexpands to this6. If $x was undefined, then $x$i is just 6 because undefined global macros, likeundefined local macros, are treated as containing nothing.2. You can nest macro expansion by including braces, so if $i contains 6, ${x$i} expands to ${x6},which expands to the contents of $x6 (which would be nothing if $x6 is undefined).3. You can mix global and local macros. Assume that local macro j contains 7. Then, ${x‘j’}expands to the contents of $x7.4. You also use braces to force the contents of global macros to run up against the succeeding text.For instance, assume that the macro drive contains “b:”. If drive were a local macro, you couldtype‘drive’myfile.dtato obtain b:myfile.dta. Because drive is a global macro, however, you must type${drive}myfile.dtaYou could not type$drive myfile.dtabecause that would expand to b: myfile.dta. You could not type$drivemyfile.dtabecause that would expand to .dta.5. Because Stata uses $ to mark global-macro expansion, printing a real $ is sometimes tricky.To display the string $22.15 with the display command, you can type display "\$22.15",although you can get away with display "$22.15" because Stata is rather smart. Stata wouldnot be smart about display "$this" if you really wanted to display $this and not the contentsof the macro this. You would have to type display "\$this". Another alternative would beto use the SMCL code for a dollar sign when you wanted to display it: display "{c S|}this";see [P] smcl.6. Real dollar signs can also be placed into the contents of macros, thus postponing substitution.First, let’s understand what happens when we do not postpone substitution; consider the followingdefinitions:global baseset "myvar thatvar"global bigset "$baseset thisvar"$bigset is equivalent to “myvar thatvar thisvar”. Now say that we redefine the macrobaseset:global baseset "myvar thatvar othvar"The definition of bigset has not changed—it is still equivalent to “myvar thatvar thisvar”.It has not changed because bigset used the definition of baseset that was current at the timeit was defined. bigset no longer knows that its contents are supposed to have any relation tobaseset.Instead, let’s assume that we had defined bigset asglobal bigset "\$baseset thisvar"

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!