From our first *Getting Started with Stata* posts, you should be comfortable navigating the windows and menus of Stata. We can now get into programming in Stata with a do-file.

### Why Do-Files?

A do-file is a Stata file that provides a list of commands to run. You can run an entire do-file at once, or you can highlight and run particular lines from the file.

If you set up your do-file correctly, you can just click “run” after opening it. The do-file will set you to the correct directory, open your dataset, do all analyses, and save any graphs or results you want saved.

I’ll start off by saying this: Any analysis you want to run in Stata can be run without a do-file, just using menus and individual commands in the command window. But you still *should* make a do-file for the following reason:

Reproducibility (more…)

In part 3 of this series, we explored the Stata graphics menu. In this post, let’s look at the Stata Statistics menu.

### Statistics Menu

Let’s use the **Statistics** menu to see if price varies by car origin (foreign).

We are testing whether a continuous variable has a different mean for the two categories of a categorical variable. So we should do a 2-sample t-test.

Say we want to use a 90% confidence level, and we have reason to suspect the two groups have unequal variance.

Click **Statistics -> Summaries, tables, and tests -> Classical tests of hypothesis -> t test** (mean-comparison test).

We want a **Two-sample using groups**. Put “price: for *Variable name* and “foreign” for *Group variable name*.

We click the **Unequal variances** button in the **Main** tab to make the variances of the two groups distinct, then change the **Confidence level** to 90%, and press OK.

Stata outputs the following code:

`ttest price, by(foreign) unequal level(90)`

and the following table:

This statistics tab can be used for all sorts of tests and analysis, such as regressions, generalized linear models, variable summaries, z-tests, and much more. Go ahead and look through the menu to get an idea of what’s available.

But now that you know how to use the menus, we’re not going to use them much in the rest of this series.

As a general rule, it is typically better to use do-files for our analysis, and only use the menus for helping to find the right code to put into our do-files.

*by James Harrod*

About the Author: *James Harrod interned at The Analysis Factor in the summer of 2023. He plans to continue into a career as an actuary, and hopes to continue finding interesting ways of educating people about statistics. James is well-versed in R and Stata programming and enjoys teaching the intuition behind common statistical methods. James is a 2023 graduate of the University of Rochester with bachelor’s degrees in Statistics and Economics.*

In part 2 of this series, we got started on the various menus in Stata. This post covers an important menu that you’ll probably use often: the graphics menu.

### What’s in the Graphics menu

The graphics menu provides an impressive variety of options for creating just about any graph you might need.

Take a look at the menu. It includes everything from univariate graphs like bar charts and pie charts to more complex, multivariate plots. Go ahead and explore some of the graphs available in the menu.

A comprehensive resource for a full understanding of the graphics you can do in Stata is the Stata Graphics Reference Manual, which is a free pdf download from the Stata web site. At nearly 800 pages, though, it’s not a quick read (it is excellent, though!).

A much quicker read is the Stata Data Visualization Cheat Sheet. Pages 5 – 6.

Browsing this two-page resource will tell you a lot about what you can do in Stata graphics. This includes not only which kinds of graphs you can create, but how to customize a graph’s appearance, apply themes, and save plots.

But first let’s explore how easy it is to create a simple, but customized plot using only the menus.

### An Example of creating a Scatter plot using menus

To show an example, we’ll use the auto data. If you haven’t loaded up the data in your current session, type the following into your command line

`sysuse auto`

Note that you could also open this data set using the File menu, but this is a command that is so simple, it’s faster to just type it into the command line.

As you’ll see, every time you use the menus, Stata fills in the associated commands for you into the command line

Now say we want to make a scatter plot with price on the y axis, and mpg on the x axis, but only for observations where the gear ratio is less than 3. We want this graph to have red triangles representing points, and we want it to have informative titles:

We click on **Graphics -> Twoway graph.** In the plots window click **Create** and select **Basic plots -> Scatter**.

Choose price as the y variable and mpg as the x variable; don’t press accept yet.

Under **Marker properties** choose Triangle as the symbol and Red as the color. Also notice you can also change the size or opacity of points or mark particular observations.

Click accept, then click accept on the next page.

Under the **if/in** tab, type “`gear_ratio<3`

” so only observations with a gear ratio less than 3 are plotted; click on **Y axis**.

Under the **Y axis** tab type the title “Price”, and under the **X axis** tab type the title “MPG”. Note how you can also change properties of the axis.

In the **Titles** tab type an appropriate title for the graph – I chose “Price and Mileage”.

We don’t want to see a legend so in the **Legend** tab choose “hide legend”.

You can now press ok and should see the following graph:

You’ll also see that Stata put this code put to the console:

`twoway (scatter price mpg, mcolor(red) msymbol(triangle)) if gear_ratio<3, ytitle(`"Price"') xtitle(`"MPG"') title(`"Price and Mileage"') legend(off)`

Now you know how to make this graph and similar ones with syntax as well! If you’re ever having trouble creating a certain chart using code, the graph menu can provide an easier way to select the options you want.

Note: when you make plots in Stata menus, make sure to always make a new plot rather than layering on an old one. If you press create when you already have a plot selected, your new scatterplot will layer on top of the old one.

*by James Harrod*

Getting Started with Stata Tutorial #4: Do-files

#### About the Author:

*James Harrod interned at The Analysis Factor in the summer of 2023. He plans to continue into a career as an actuary, and hopes to continue finding interesting ways of educating people about statistics. James is well-versed in R and Stata programming and enjoys teaching the intuition behind common statistical methods. James is a 2023 graduate of the University of Rochester with bachelor’s degrees in Statistics and Economics.*

A great way to get started with Stata is using its menus.

The first part of this Tutorial Series introduced you to Stata’s windows. You can now begin learning how to use Stata to work with data.

Across the top are 8 tabs: File, Edit, Data, Graphics, Statistics, User, Window, and Help.

We will not go through every option within the Stata menus. Instead, we’ll highlight a few options to get you started. In this article, we’ll start with three of the most useful menus: File, Data, and Help, along with those helpful icons under the menus.

In our next article, we’ll look at two more: Graphics and Statistics.

(more…)

So, you want to get started with Stata?

Good choice!

At The Analysis Factor we recommend first becoming proficient in one statistical software. Then once you’ve progressed up to learning Stage 3 skills, adding a second statistical software. Whether it’s your first, second, or 5th statistical software, Stata has a lot that makes it worth learning.

When I first started using Stata, I remember being confused by the variety of menus and windows, the strange syntax of the code, the way it handled datasets… and what the heck is a do file? (more…)