CEU MicroData Stata Style Guide
Creators: name: Koren, Miklós
Publisher: name: Zenodo
Year: 2021

description: <strong>Files</strong> Use forward slash in path names Write <code>save "data/worker.dta"</code>, not <code>save "data\worker.dta"</code>. The former works on all three major platforms, the latter only on Windows. Write out file extensions Write <code>save "data/worker.dta"</code> and <code>do "regression.do"</code>, not <code>save "data/worker"</code> or <code>do "regression"</code>. Even though some extensions are appended by Stata by default, it is better to be explicit to help future readers of your code. Put file paths in quotes Write <code>save "data/worker.dta"</code> and <code>do "regression.do"</code>, not <code>save data/worker.dta</code> or <code>do regression</code>. Both are correct, but the first is more readable, as most editors readily highlight strings as separate from programming statements. Use relative path whenever possible Write <code>save "../data/worker.dta"</code>, not <code>save "/Users/koren/Tresorit/research/data/worker.dta</code>. Nobody else will have the same absolute path as you have on your system. Adopt a convention of where you are running scripts from and make paths relative to that location. <strong>Naming</strong> Do not abbreviate commands Use <code>generate ln_wage = ln(wage)</code> and <code>summarize ln_wage, detail</code>, not <code>g ln_wage = ln(wage)</code> or <code>su ln_wage, d</code>. Both will work, because Stata allows you abbreviation, but the former is more readable. Do not abbreviate variable names Use <code>summarize ln_wage, detail</code>, not <code>summarize ln_w, detail</code>. Both will work, because Stata allows you abbreviation, but the latter is very error prone. In fact, you can turn off variable name abbreviation with <code>set varabbrev off, permanent</code>. Use verbose names to the extent possible Use <code>egen mean_male_wage = mean(wage) if gender == "male"</code> , not <code>egen w1 = mean(wage) if gender == "male"</code>. Your variables should be self documenting. Reserve variable labeling to even more verbose explanations, including units: <code>label variable mean_male_wage "Average wage of male workers (2011 HUF)"</code>. Separate name components with underscore Use <code>egen mean_male_wage = mean(wage) if gender == "male"</code> , not <code>egen meanmalewage = mean(wage) if gender == "male"</code> or <code>egen meanMaleWage = mean(wage) if gender == "male"</code>. The former is more readable. Transformations like mean, log should be part of the variable name. Do not put units and time in the variable name Use <code>revenue</code> , not <code>revenue_USD</code> or <code>revenue_2017</code>. Record this information in variable labels, though. You <em>will</em> change your code and your data and you don't want this detail to ruin your entire code. It is ok to use short macro names in short code If you have a <code>foreach</code> loop with a few lines of code, it is fine to use a one-character variable name for indexing: <code>foreach X of variable wage mean_male_wage {</code>. But if you have longer code and <code>X</code> would pop up multiple times, give it a more verbose name. It is ok to use obvious abbreviation in variable names If you are hard pressed against the 32-character limit of variable name length, use abbreviation that will be obvious to everyone seeing the code. Use <code>generate num_customer</code>, not <code>generate number_of_customers_of_the_firm</code> or <code>generate n_cust</code>. <strong>White space</strong> Include a space around all binary operators Use <code>generate ln_wage = ln(wage)</code> and <code>count if gender == "male"</code>, not <code>generate ln_wage=ln(wage)</code> or <code>count if gender=="male"</code>. The former is more readable. Include a space after commas in function calls Use <code>assert inlist(gender, "male", "female")</code> not <code>assert inlist(gender,"male","female")</code>. The former is more readable. Indent code that belongs together <pre><code>foreach X of variable wage mean_male_wage { summarize `X', detail scalar `X'_median = r(p50) }</code></pre> not <pre>foreach X of variable wage mean_male_wage { summarize `X', detail scalar `X'_median = r(p50) }</pre> Each .do file should be shorter than 120 lines Longer scripts are much more difficult to read and understand by others. If your script is longer, break it up into smaller components by creating several .do files and calling them.