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.

212 [ U ] 18 Programming StataAnother useful source of information is c(), documented in [P] creturn:localtoday "‘c(current_date)’"local curdir "‘c(pwd)’"localnewn = c(N)+1c() refers to a prerecorded list of values, which may be used directly in expressions or which maybe quoted and the result substituted anywhere. c(current date) returns today’s date in the form”dd MON yyyy”. Thus the first example stores in macro today that date. c(pwd) returns the currentdirectory, such as C:\data\proj. Thus the second example stores in macro curdir the currentdirectory. c(N) returns the number of observations of the data in memory. Thus the third examplestores in macro newn that number, plus one.Note the use of quotes with c(). We could just as well have coded the first two examples aslocaltoday = c(current_date)local curdir = c(pwd)c() is a Stata function in the same sense that sqrt() is a Stata function. Thus we can use c()directly in expressions. It is a special property of macro expansion, however, that you may use thec() function inside macro-expansion quotes. The same is not true of sqrt().In any case, whenever you need a piece of information, whether it be about the dataset or aboutthe environment, look in [P] macro and [P] creturn. It is likely to be in one place or the other, andsometimes, it is in both. You can obtain the current directory by usingor by usinglocal curdir = c(pwd)local curdir : pwdWhen information is in both, it does not matter which source you use.18.3.7 Macro increment and decrement functionsWe mentioned incrementing macros in [U] 18.3.4 Macros and expressions. The constructcommand that makes reference to ‘i’local ++ioccurs so commonly in Stata programs that it is convenient (and faster when executed) to collapseboth lines of code into one and to increment (or decrement) i at the same time that it is referred to.Stata allows this:while (‘++i’ < 1000) {. . .}while (‘i++’ < 1000) {. . .}while (‘--i’ > 0) {. . .}while (‘i--’ > 0) {. . .}

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

Saved successfully!

Ooh no, something went wrong!