Angle of custom markers in Stata graphs

In Stata it is possible to add custom markers to graphs. This feature, besides labeling points, can also be used to show direction of change via arrows or other custom markers.

In order to demonstrate this, let’s generate a simple graph in Stata using a straight line:


gen x = .    
gen y = .

replace x = 0 in 1
replace x = 10 in 2

replace y = 0 in 1
replace y = 10 in 2

twoway (line y x)

This gives us the following graph:

Continue reading

Stata: Extracting information from variable labels and names

This post discusses information contained in variable names of labels that might be useful for batch processing graphs. If one is processing a large dataset, then a good practice would be to have homogenized names for variables, for example v1, v2, v3, etc. so one can loop through them easily. Additionally, it is also good practice to store categorical information in value labels.

For example, a variable shows up as v1 with values 1, 2, 3, 4, 5 might be something

lab var v1 "Some description of the variable"

lab de v1lab 1 "category 1 description" 2 "category 2 description" 3 "category 3 description" etc

lab val v1 v1lab

The following two code blocks show how one can “pick” this information from the stored information and plug it in a graph for example:

Using value labels of a variable in graphs:

levelsof v1, local(lclname)

foreach x of local lclname {
    local t : label varlab `x'
    twoway line yvar xvar if v1==`x', ///

Using variable names in graphs

foreach x of varlist v1-v100 {
     local t : var label `x'
     twoway line v1 time, ///

NOTE: mind the spaces in locals. The semi-colon has to have a space.

Stata – Exporting dates in filenames

A key feature of writing long do-files is to export various outputs, logs, excel files, etc. at the end of your run. More often than not, it is useful to capture changes over time as data analysis progresses. It is therefore useful to export files and version control them with dates. A good practice is to save them in the format:


for example:


Continue reading

Stata to Latex – Part 1: Exporting customized tables

Anyone writing an empirical paper eventually has to deal with the issue of exporting Stata outputs to a Latex file. While standard statistics and regressions are easy to handle, as soon as customized outputs are required, it quickly becomes messy, requiring significant head scratching. Making tables by hand is not really advisable, since one is likely to tweak the outputs several times. In this entry, we look at a simple example of stacking standard errors from different regression outputs which have the same beta coefficient but different significance levels. The main aim is to create a compact table where multiple regression results can be neatly displayed using the custom “estout” package in Stata.

Continue reading

Automating EUROSTAT in Stata – Part 1: Downloading files

Eurostat is a great database covering various socio-economic, environmental, and regional indicators in Europe. On the other hand, navigating the database can be daunting; similar variables exist across various different datasets, several subsets of meta datasets exist, and some datasets contain derived variables. Given these combinations, it is not unusual to extract and collate information from different dataset and combine them in one database for analysis. Unless you are a database specialist who can automate this using softwares like R, Python, Java, SQL, etc., you will spend a lot of time checking for new versions, downloading files, unzipping them, putting them together, and fixing them before you are anywhere near analysis.

This three part guide gives a step-by-step process to automating the downloading, extracting, and cleaning of Eurostat datasets such that they end up in a user-friendly format that is ready for analysis.

Continue reading