Blockwise copy & paste in Stata

Simple method to clockwise edit text in do-files

I’m not sure whether it is was always there or whether it just came with an update, but finally I have found out how to blockwise copy & paste in the Stata do-file editor. What I used to do with other text editors (mostly with WinEdt), is now possible with Stata’s built-in editor. Continue reading “Blockwise copy & paste in Stata”

Overlaying histograms in Stata

For analysing data and comparing distributions, I often want to overlay two histograms. Without further options, however, one distribution usually overlays the other and makes comparisons cumbersome. It is possible to set a few options to make the figure look nice. Continue reading “Overlaying histograms in Stata”

Formatted numbers in figure (sub)headers

There is a very nice and simple trick to include numbers (such as: number of observations, R2s or any other number that can be saved to a local) in Stata figures. While including numbers can be simply done with a local, the trick is to have them nicely formatted. I.e., to include commas in a larger number, or to have decimals rounded. Continue reading “Formatted numbers in figure (sub)headers”

Data storage type matters

Despite most sources tell that the storage type in stata should not matter, it is worth checking whether this is the case for your dataset. I just came across a situation where two identically constructed datasets (one stored in default type (float) and one stored in double) generated different output. Also before that i encountered a problem with person identifiers in the GSOEP if using the default data storage. If your dataset is not huge (with the GSOEP it still works quite ok) it might be worth to take the safe side and use

 set type double 

before you assemble your data set. This saves the data in the most precise way stata offers.

Preamble when switching between OS X and Windows

A problem when working on one and the same project on different platforms (here: Windows and Mac/OS X) is that path-names differ. There are two straightforward solutions to this:

1) When defining a number of different path (e.g. one path where data is stored, one where results/output is stored), it is handy to define the paths as globals and to add an “if” condition. The platform can be detected by the local `c(os)’: Continue reading “Preamble when switching between OS X and Windows”

Stacked graphs in Stata

For plotting the relative importance of (e.g.) input shares, it can be useful to stack them in a figure. In my specific problem, I wanted to show the development of low-, medium-, and high-skilled labour inputs, relative to all labour inputs over time. Since the three shares obviously sum up to 1, I wanted to have a figure like this: Continue reading “Stacked graphs in Stata”

Manipulating variable labels

Occasionally we want to use variable labels in loops or we want to apply them to other labels. a very simple local syntax function for that is local localname: variable label varname.

As an example, suppose that I have three variables (x, y, z) with their respective labels. I want to create three other variables with their mean and I want to label those variables using the labels from x, y and z. I can type:

global varlist1 x y x

foreach var in $varlist1 {
local label`var': variable label `var'
egen mean_`var' = mean(`var')
label variable mean`var' "Mean of `label`var' ' "
}

The same idea is also useful for graphs and other commands