Advice

Navigating this guide

Throughout the text, various design elements have been added to make the guide easier to use.

  • Variables are coloured in red (ex. cognitive) and commands in blue (ex. codebook) to help you as the reader identify the different parts of code.
  • Pictures can be expanded by clicking on them for easier reading.
  • Blue bolded text can be clicked on and you will be re-directed to that page.

Keep a clear folder and file structure

Keep all your files for the course/project in the same main folder.

Example
Ericsson_Data

Use sub folders to organise the files further.

Example
Ericsson_Data\Lesson1
Ericsson_Data\Lesson2

Save your files under appropriate names.

Example
Ericsson_Data\Lesson1\Exercise1.dta
Ericsson_Data\Lesson1\Exercise1.do

Play it safe

Always keep a copy of the original files.

Example
Ericsson_Data\Original_Files\StataData1.dta

Save intermediate versions of your files.

Example
Ericsson_Data\Lesson1\Exercise1_240629.dta
Ericsson_Data\Lesson1\Exercise1_240630.dta

Pay attention to details

  • Always double-check that you have correctly spelled variable names, values, and labels.
  • Be careful with the placement of dots/periods and commas in the commands.
  • You must always use lower-case letters for your commands.
  • Make sure that you have formatted all comments correctly.
  • Remember that Stata is case-sensitive.

Use do-files

Nowadays, Stata’s interface is quite user-friendly and, if you want, you can do most things through the menus.

This is, however, not what we will practice in this guide; instead, we will learn how to write syntax (i.e. programming).

Syntax is basically how we tell Stata to do what we want it to do.

We write our syntax in a so-called do-file, which is a text editor where it is possible to add comments and commands.

Advantages

The reasons for why we strongly recommend everyone to use do-files are, among other things:

  • It is a way of documenting and archiving everything you have done with the data material.
  • It is easy to repeat parts or all of the analysis.
  • Other people (e.g., supervisors, colleagues) can easily understand what you have done and how you have done it.
  • It saves an enormous amount of time.

Learn about file paths

To make full use of do-files, you need to be able to specify file paths. A file path is a specification of the location of a file on your computer. There are many ways of identifying a file path, of which a few will be outline below.

Note
The following instructions are based on Windows. If you are using a Mac computer, the slashes in the file paths will go in the opposite direction (/ instead of \).
Option 1
  • Open the File Explorer and locate the dataset you want.
  • Right-click on the dataset and choose Properties.
  • In the new window that opens, look for the line that says Location.
  • Highlight the path that is specified here.
  • Right-click, and choose Copy.
  • It could look something like this:
Example
C:\Users\yerik\Stata Guide
  • Paste the path into your do-file.
  • Note that you also need to add the file name (including the file extension) to the path.
  • It could look something like this:
Example
C:\Users\yerik\Stata Guide\StataData1.dta
Option 2
  • Open the File Explorer and locate the dataset you want.
  • In the upper area of the window, there is a search bar.
  • Place the mouse pointer on the right-hand side of the path specification here.
  • Click to highlight the path.
  • Right-click and choose Copy.
  • Paste the path into your do-file.
  • Note that you also need to add the file name (including the file extension) to the path.
Option 3
  • Double-click on the dataset of interest to open it.
  • The file path will be visible in the Results window in Stata.
  • Highlight the file path.
  • Right-click and choose Copy.
  • Paste the path into your do-file.