Skip to Main Content

How do I read a data file that has commas or tabs as delimeters? | Stata FAQ

Consider the file below that contains four variables,
make, mpg, weight, and price.
The file looks like what is shown below (the variable names are indeed the first line of
data.)

make,mpg,weight,price
AMC Concord,22,2930,4099
AMC Pacer,17,3350,4749
AMC Spirit,22,2640,3799
Buick Century,20,3250,4816
Buick Electra,15,4080,7827

You can read this kind of file using the insheet command
as shown below.

insheet using comma.txt

We can issue the list command to see if the data was
read properly.

list

              make       mpg    weight     price 
  1.   AMC Concord        22      2930      4099 
  2.     AMC Pacer        17      3350      4749 
  3.    AMC Spirit        22      2640      3799 
  4. Buick Century        20      3250      4816 
  5. Buick Electra        15      4080      7827 

As you can see, the insheet command was pretty smart. It got the variable names
from the first row of the data.  It looks at the first row and can get the
variable names from the first row.  It also examines the file and
determines for itself whether the data are separated by commas or by
tabs.  The exact same command could read the same file but delimited with tabs (you
can try reading https://stats.idre.ucla.edu/wp-content/uploads/2016/02/tab.txt for
yourself).  If you would like more information about reading data in Stata, see the
Stata
Learning Module
on inputting raw data into Stata.

As an aside, you can actually read the raw data file comma.txt directly
over the internet into Stata, using the following command:

insheet using https://stats.idre.ucla.edu/stat/stata/faq/comma.txt