admin 发表于 2015-11-19 19:40:53

一些Stata Tips汇总

Stata Tips:
1. Don’t use the Stata do-file editor. Use Notepad++ or Crimson Editor instead, and add the
requisite code to run do-files and selected parts of do-files
2. Don’t use the tempting semi-colon delimiter. It doesn’t work so well in Mata. Make
liberal use of /// instead if a line of code is too long. One important exception to this is
when defining very long local or global macros. In that case you want to switch over to
semicolon delimiting so you can split up the macro (see #3)
3. Don’t let lines of code get too long. This makes it hard to read, both on the screen and
especially when printed. Keep your code less than 85 characters per line, using ///
when you have to wrap to the next line
4. If you’re doing a foreach or forvalues loop, indent all the material between the brackets,
then put a little comment after the closing bracket to tell the reader (i.e. your 6-monthsfrom-now-can’t-read-your-own-code self) what just ended. Same for if statements and so
on. (even after a quietly { } block)
5. Make liberal use of short programs and loops rather than repeating blocks of code with
only minor changes. However…
6. Make your do-files readable. Try to make sections that use macros reasonably and don’t
over-abbreviate commands. Yes, it’s cool that s can substitute for save but this makes
code hard to read.
7. Write an introductory section in which you say what the do-file does (in fact, an outline
can be very helpful), where it fits into the overall project (i.e. “This do-file,
descriptive.do, follows data-cleanup.do and is followed by regressions.do); and what the
key inputs and outputs are
8. Make liberal use of comments! Again, the ideal is that someone who has never seen your
project before could pick up your do-file and know what is going on, both in the bigpicture sense as well as what each section of code is doing.
9. If you’re going to use a global macro, use the ${…} expander instead of just $.... .
This is much easier to read, especially when nesting.
10. Don’t use the = sign when defining macros! If so your macro will get truncated at ~255 (I
think) characters which can mess you up.For example:
qui ls
local MyVariables `r(varlist)’
will put all your variables into the MyVariables local macro, but
qui ls
local MyVariables = “`r(varlist)’”
will cut off after 255 characters
原文地址:http://econweb.umd.edu/~guiteras/Stata Tips.pdf

页: [1]
查看完整版本: 一些Stata Tips汇总