11.07.2015 Views

[U] User's Guide

[U] User's Guide

[U] User's Guide

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

226 [ U ] 18 Programming Statagettoken binds on double quotes whenever a (simple or compound) double quote is encounteredat the beginning of emname3. Specifying parse(‘"" "’) ensures that double-quoted strings areisolated.quote specifies that double quotes not be removed from the source in defining the token. Forinstance, in parsing “"this is" a test”, the next token is “this is” if quote is not specified andis “"this is"” if quote is specified.match() specifies that parentheses and square brackets be matched in defining tokens. The outsidelevel of parentheses or brackets is stripped. In parsing “(2+3)/2”, the next token is “2+3” if match()is specified. In practice, match() might be used with expressions, but it is more likely to be usedto isolate bound varlists and time-series varlists.18.5 Scalars and matricesIn addition to macros, scalars and matrices are provided for programmers; see [U] 14 Matrixexpressions, [P] scalar and [P] matrix.As far as scalar calculations go, you can use macros or scalars. Remember, macros can holdnumbers. Stata’s scalars are, however, slightly faster and are a little more accurate than macros. Thespeed issue is so slight as to be nearly immeasurable. Macros are accurate to a minimum of 12decimal digits, and scalars are accurate to roughly 16 decimal digits. Which you use makes littledifference except in iterative calculations.Stata also has a serious matrix programming language called Mata, which is the subject of anothermanual. Mata can be used to write subroutines that are called by Stata programs. See the MataReference Manual, and in particular, [M-1] ado.18.6 Temporarily destroying the data in memoryIt is sometimes necessary to modify the data in memory to accomplish a particular task. A wellbehavedprogram, however, ensures that the user’s data are always restored. The preserve commandmakes this easy:code before the data need changingpreservecode that changes data freelyWhen you give the preserve command, Stata makes a copy of the user’s data on disk. Whenyour program terminates—no matter how—Stata restores the data and erases the temporary file; see[P] preserve.18.7 Temporary objectsIf you write a substantial program, it will invariably require the use of temporary variables in thedata, or temporary scalars, matrices, or files. Temporary objects are necessary while the program ismaking its calculations, and once the program completes they are discarded.Stata provides three commands to create temporary objects: tempvar creates names for variablesin the dataset, tempname creates names for scalars and matrices, and tempfile creates names forfiles. All are described in [P] macro, and all have the same syntax:{ tempvar | tempname | tempfile } macname [macname . . .]The commands create local macros containing names you may use.

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

Saved successfully!

Ooh no, something went wrong!