Stata style guide
Overview
Teaching: 0 min
Exercises: 0 minQuestions
How to name variables?
What code style do we use?
Objectives
Use verbose, helpful variable names.
Make your code accessible to others.
Files
Use forward slash in path names
Write
save "data/worker.dta"
, not. The former works on all three major platforms, the latter omnly on Windows.save "data\worker.dta
Write out file extensions
Write
save "data/worker.dta"
anddo "regression.do"
, notorsave "data/worker
. Even though some extensions are appended by Stata by default, it is better to be explicit to help future readers of your code.do "regression"
Put file paths in quotes
Write
save "data/worker.dta"
anddo "regression.do"
, notorsave data/worker.dta
. Both are correct, but the first is more readable, as most editors readily highlight strings as separate from programming statements.do regression
Use relative path whenever possible
Write
save "../data/worker.dta"
, not. 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.save "/Users/koren/Tresorit/research/data/worker.dta
Naming
Do not abbreviate commands
Use
generate ln_wage = ln(wage)
andsummarize ln_wage, detail
, notorg ln_wage = ln(wage)
. Both will work, because Stata allows you abbreviation, but the former is more readable.su ln_wage, d
Do not abbreviate variable names
Use
summarize ln_wage, detail
, not. Both will work, because Stata allows you abbreviation, but the latter is very error prone. In fact, you can turn off variable name abbreviation withsumarize ln_w, detail
set varabbrev off, permanent
.
Use verbose names to the extent possible
Use
egen mean_male_wage = mean(wage) if gender == "male"
, not. Your variables should be self documenting. Reserve variable labeling to even more verbose explanations, including units:egen w1 = mean(wage) if gender == "male"
label variable mean_male_wage "Average wage of male workers (2011 HUF)"
.
Separate name components with underscore
Use
egen mean_male_wage = mean(wage) if gender == "male"
, notoregen meanmalewage = mean(wage) if gender == "male"
. The former is more readable. Transformations like mean, log should be part of the variable name.egen meanMaleWage = mean(wage) if gender == "male"
Do not put units and time in the variable name
Use
revenue
, notorrevenue_USD
. Record this information in variable labels, though. You will change your code and your data and you don’t want this detail to ruin your entire code.revenue_2017
It is ok to use short macro names in short code
If you have a
foreach
loop with a few lines of code, it is fine to use a one-character variable name for indexing:foreach X of variable wage mean_male_wage {
. But if you have longer code andX
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
generate num_customer
, notorgenerate number_of_customers_of_the_firm
.generate n_cust
White space
Include a space around all binary operators
Use
generate ln_wage = ln(wage)
andcount if gender == "male"
, notorgenerate ln_wage=ln(wage)
. The former is more readable.count if gender=="male"
Include a space after commas in function calls
Use
assert inlist(gender, "male", "female")
not. The former is more readable.assert inlist(gender,"male","female")
Indent code that belongs together
foreach X of variable wage mean_male_wage { summarize `X', detail scalar `X'_median = r(p50) }
not
foreach X of variable wage mean_male_wage { summarize `X', detail scalar `X'_median = r(p50) }
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.
Key Points