Skip to Main Content

Data Analysis with Stata

Happiness by Census Division

For a serious GIS project, you should not be using Stata (check out CDS's GIS services). That being said, Stata is perfectly capable of producing basic thematic maps. This tutorial walks you through how to produce a map of unhappiness by Census Division (Figure 3).

Image of Figure 3

Generally speaking, to produce a thematic map, you need two ingredients:

  • Data on the theme of interest. This dataset must contain a variable that identifies geographical location. For this tutorial, we need happiness data for 2012 at the Census Division-level. The dataset we use is GSS2012_divisions.dta. You will note that the variable subreg_id identifies Census Divisions. The data are from the General Social Survey.
  • A "shapefile" (a shapefile is actually a set of six files that collectively form a GIS map).  This is basically the digital map on which you will overlay your theme of interest. We will use a shapefile provided by the U.S. Census Bureau.

Step 1

The shapefile that you need is in this zipped file. Download it to your hard drive and unzip it to a folder of your choice, preferably the same one in which the necessary do-files and datasets (see the next steps) will reside.

Step 2

You will need to convert the shapefile to

  1. a Stata-format dataset containing information from the original dBase file that is associated with the shapefile, and
  2. a Stata-format dataset containing geographical coordinates.

You do not perform these conversions manually; use the shp2dta command.*
Refer to for the complete command involved in this step.

*You may first need to install shp2dta off Stata's servers by entering ssc install shp2dta.

Step 3

Merge GSS2012_divisions.dta and the dBase dataset, usdata.dta that was produced by shp2dta (see Step 2A).

Finally, the spmap command ties the unhappiness data to the coordinates data produced by shp2dta (see Step 2B) and generates the map you see in Figure 3.* The basic command is

spmap unhappy using uscoord.dta, id(subreg_id)

Refer to for the complete commands in this step.

*You may first need to install spmap off Stata's servers by entering ssc install spmap.