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 213Above we have chosen to illustrate this by using Stata’s while command, but ++ and -- can be usedanyplace in any context, just so long as it is enclosed in macro-substitution quotes.When the ++ or -- appears before the name, the macro is first incremented or decremented, andthen the result is substituted.When the ++ or -- appears after the name, the current value of the macro is substituted and thenthe macro is incremented or decremented.Technical noteDo not use the inline ++ or -- operators when a part of the line might not be executed. Considerif (‘i’==0) local j = ‘k++’versusif (‘i’==0) {local j = ‘k++’}The first will not do what you expect because macros are expanded before the line is interpreted.Thus the first will result in k always being incremented, whereas the second increments k only when‘i’==0.18.3.8 Macro expressionsTypingis equivalent tocommand that makes reference to ‘=exp’local macroname = expcommand that makes reference to ‘macroname’although the former runs faster and is easier to type. When you use ‘=exp’ within some largercommand, exp is evaluated by Stata’s expression evaluator, and the results are inserted as a literalstring into the larger command. Then the command is executed. For example,summarize u4summarize u‘=2+2’summarize u‘=4*(cos(0)==1)’all do the same thing. exp can be any valid Stata expression and thus may include references tovariables, matrices, scalars, or even other macros. In the last case, just remember to enclose thesubmacros in quotes:Also, typingis equivalent toreplace ‘var’ = ‘group’[‘=‘j’+1’]command that makes reference to ‘:extended macro function’local macroname : extended macro functioncommand that makes reference to ‘macroname’

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

Saved successfully!

Ooh no, something went wrong!