07.02.2014 Views

Introduction to Stata 8 - (GRIPS

Introduction to Stata 8 - (GRIPS

Introduction to Stata 8 - (GRIPS

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

15.7. ado-files [U] 20-21, [P] (Programming manual)<br />

An ado-file is a program. Most users will never write programs themselves, but just use<br />

existing programs. If you are a freak, read more in the User's Guide ([U] 20-21) and the<br />

programming manual [P]. Save user-written programs in c:\ado\personal. To see the<br />

locations of all ado-files issue the command sysdir.<br />

The simplest form of an .ado file is a single command or a do-file with a leading program<br />

define command and a terminating end command. There must be a new line after the<br />

terminating end.<br />

Here is an example <strong>to</strong> demonstrate that creating your own commands is not that impossible.<br />

datetime displays date and time<br />

program define datetime<br />

// c:\ado\personal\datetime.ado. Displays date and time.<br />

display " $S_DATE $S_TIME "<br />

end<br />

Just enter datetime in the command window, and the date and time is displayed:<br />

. datetime<br />

9 Feb 2003 16:54:15<br />

Two ado-files useful for the interaction between <strong>Stata</strong> and NoteTab are shown in appendix 3.<br />

foreach and forvalues<br />

[P] foreach; forvalues<br />

These commands are documented in the programming manual, and in the online help (whelp<br />

foreach). Also see the FAQ www.stata.com/support/faqs/data/foreach.html. They enable<br />

you <strong>to</strong> repeat a command for a number of variables or values. The commands can be used not<br />

only in ado-files, but also in do-files and even interactively. Look at the sequence in section<br />

11.1:<br />

foreach Q of varlist q1-q10 {<br />

tabulate `Q' sex<br />

}<br />

Q is a local macro (see [U] 21.3); foreach defines it as a stand-in for the variables q1 <strong>to</strong><br />

q10, and the sequence generates ten tabulate commands. The local macro is in effect<br />

only within the braces {} which must be placed as shown.<br />

When referring <strong>to</strong> the local macro Q it must be enclosed in single quotes: `Q'. In the<br />

manuals single quotes are shown differently; but with a Danish keyboard the opening quote is<br />

` (accent grave), and the ending quote the simple '.<br />

62

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

Saved successfully!

Ooh no, something went wrong!