Add normal distribution to kernel density plot in Stata

This might be already implemented in the most recent version of Stata, but I just came across the problem that there seems to be no straightforward way to combine a kernel density plot (i.e. kdensity) with a normal distribution of the underlying variable.

Continue reading “Add normal distribution to kernel density plot in Stata”

Information about Stata macros: number of elements and order within locals and globals

Especially when running loops (see our posts here), it can be important to get more information about a macro, such as the length, or its first or last element.

Continue reading “Information about Stata macros: number of elements and order within locals and globals”

Useful string functions in Stata (updated list)

Most often when I search the internet for help on Stata, it is probably when I need to work with string variables (such as names). There are some very good summaries that cover aspects of string variables (e.g., this page). In this post –which will be continuously updated– we present random string functions that we think are extremely useful for Stata users.

Continue reading “Useful string functions in Stata (updated list)”

Format how locals are displayed in Stata

Often I use locals to calculate something (such as the mean of a variable for a specific group) and use this in a loop (say, over groups or over years). If you, for example, want to calculate the share of observations that belong to one group (say: female==1), you could simply write

Continue reading “Format how locals are displayed in Stata”

Piecewise execution of do-files in Stata

Do-files in Stata easily get a bit lengthy. Of course, you can try to shorten do-files and distribute code onto several do-files and have one master file that runs all of the respective sub-do-files (which are included by do dosubfile1.do). Alternatively, you can leave the do-file longish but write your code such that you only run parts of the code at once:

Continue reading “Piecewise execution of do-files in Stata”

Multiple lines commands in Stata

Many Stata commands get easily quite long. Take, for example, a command to generate a figure – possibly the layout definitions are quite lengthy and difficult to read. Stretching commands over several lines makes it much easier to write and read, and less prone to errors. 

Continue reading “Multiple lines commands in Stata”

Add current date to Stata file names (and others)

We wrote earlier about how to add time stamps (current date and/or time) to your Stata figures, e.g. to keep track of work in progress results. A similar strategy can be used when storing log-files, which are typically overwritten with a more recent version of the same log.

Continue reading “Add current date to Stata file names (and others)”

Use of embedded quotation marks within locals in Stata

Proper use of single and double quotation marks is essential when working in Stata, especially when writing loops where locals can be a huge time and memory saver. The use of single and double quotation marks is rather straightforward (using ` and ‘ for single, and ” for double quotation marks). You can rather easily define a local, e.g. based on the average of a variable

Continue reading “Use of embedded quotation marks within locals in Stata”

Supress labels in Stata figures

Supress legend elements in multiple Stata twoway figures

A great feature of Stata figures is that you can mix several plot types in one by using the twoway (...) (...) command. If you have several plots merged into one twoway graph and also want to add a legend, the legend might become a bit crowded. In the following example, legends are specified with the , legend(lab(1 "Scatter 1") lab (4 "Scatter 2")) option because I only wanted to mark the diamonds and circles in the legend:

Continue reading “Supress labels in Stata figures”

Remove blanks from string variables in Stata

Identify and remove blank space in Stata’s string variables

When dealing with string variables in Stata, blanks spaces can make it difficult to identify values. For example, if a variable contains " Arizona", a command that contains an if command such as ... if state="Arizona" won’t detect this observation.

Continue reading “Remove blanks from string variables in Stata”