Click to compare with Python equivalents
| command | [action] |
|---|---|
| 4.1. use data | [ loads a Stata format data set (.dta) ] |
| 4.2. import delim filename | [ loads a text delimited data file] |
| 4.3. import excel filename, variable name row | [ loads an Excel data file and sets the row to used as variable names (often first) ] |
| command | [action] |
|---|---|
| 4.1. import pandas as pd | [ imports pandas library as pd] |
| 4.2. df = pd.read_csv('file.csv') | [ loads csv into a dataframe "df"] |
| 4.3. df = pd.read_stata('file.dta') | [ loads stata data into a dataframe "df"] |
| 4.4. df = pd.read_excel('file.xlsx') | [ loads excel data into a dataframe "df"] |
| 4.5. df | [ View the first 5 rows of df ] |
Now that you know how to use Stata the next step is loading data. We will focus on the 3 most common types of data you will encounter.
• Stata formatted data (.dta files)
• Text delimited files (commonly .csv files)
• Microsoft Excel files (.xlsx files)
Of course you can open many other types of files, but the above 3 are the most likely for you to encounter in the beginning.
The most difficult part of loading data is understanding your working directory and the path to the data. In order for your do-file to work on different machines it is important to use relative paths to open files. Let's download some data files into your working directory. Type:
    cp
This will copy the data file on your computer in your working directory. You can check your working directory in Stata with the command pwd and view available files with the command ls.
The easiest file to open is a Stata formatted .dta file. You simply use the use command and the filename. You do not need to type .dta as that is the default extension. You can open files online or on your computer
    use
If we want to open the coma delimited file (.csv), we use the import delim command and the full filename.
clear command ***     clear
    import delim
Now that you know how to use python the next step is loading data. We will focus on the 3 most common types of data you will encounter using the pandas library.
• Stata formatted data (.dta files)
• Text delimited files (commonly .csv files)
• Microsoft Excel files (.xlsx files)
Of course you can open many other types of files, but the above 3 are the most likely for you to encounter in the beginning.
The most difficult part of loading data is understanding your working directory and the path to the data. In order for your do-file to work on different machines it is important to use relative paths to open files. Let's download some data files into your working directory. Type into your terminal using bash (not python):
    curl -O
This will copy the data file on your computer in your working directory. You can check your working directory in bash with the command pwd and view available files with the command dir or ls.
To work with a datset you must first import the pandas library. After opening python type:
    import
You can open files online or on your computer. Let's start with the csv file in your working directory.
    df = pd.read_csv('births-per.csv')
If we want to open the Stata data file (.dta), we use the pd.read_stata('births-per.csv') command and the filename.
    df = pd.read_stata('https://eddie-hearn.github.io/teaching/ZEM/data/births-per.dta')