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.7 Temporary objects 22718.7.1 Temporary variablesSay that, in making a calculation, you need to add variables sum y and sum z to the data. Youmight be tempted to code. . .gen sum_y = . . .gen sum_z = . . .. . .but that would be poor because the dataset might already have variables named sum y and sum zin it and you will have to remember to drop the variables before your program concludes. Better is. . .tempvar sum_ygen ‘sum_y’ = . . .tempvar sum_zgen ‘sum_z’ = . . .. . .or. . .tempvar sum_y sum_zgen ‘sum_y’ = . . .gen ‘sum_z’ = . . .. . .It is not necessary to explicitly drop ‘sum y’ and ‘sum z’ when you are finished, although youmay if you wish. Stata will automatically drop any variables with names assigned by tempvar.After issuing the tempvar command, you must refer to the names with the enclosing quotes, whichsignifies macro expansion. Thus, after typing tempvar sum y—the one case where you do not putsingle quotes around the name—refer thereafter to the variable ‘sum y’, with quotes. tempvar doesnot create temporary variables. Instead tempvar creates names that may later be used to create newvariables that will be temporary, and tempvar stores that name in the local macro whose name youprovide.A full description of tempvar can be found in [P] macro.18.7.2 Temporary scalars and matricestempname works just like tempvar. For instance, a piece of your code might readtempname YXX XXinvmatrix accum ‘YXX’ = price weight mpgmatrix ‘XXinv’ = invsym(‘YXX’[2..., 2...])tempname bmatrix ‘b’ = ‘XXinv’*‘YXX’[1..., 1]The above code solves for the coefficients of a regression on price on weight and mpg; see[U] 14 Matrix expressions and [P] matrix for more information on the matrix commands.As with temporary variables, temporary scalars and matrices are automatically dropped at theconclusion of your program.

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

Saved successfully!

Ooh no, something went wrong!