Home  /  Resources & Support  /  Introduction to Stata basics  /  Import and export data from SAS

Sometimes, we would like to work with data that are stored as in SAS format. It is easy to import data from SAS using Stata's import sas command.

Let's begin by getting a SAS datset from the Stata website. We can use Stata's copy command to copy the file auto.sas7bdat from the Stata website to our working directory.

. copy https://www.stata.com/sampledata/auto.sas7bdat myauto.sas7bdat

Next we can import the SAS dataset by typing import sas followed by the filename.

. import sas myauto.sas7bdat
(12 vars, 74 obs)

Let's type describe to verify that our data imported successfully.

. describe

Contains data
 Observations:            74
    Variables:            12
Variable Storage Display Value
name type format label Variable label
make str17 %17s
price int %10.0g
mpg byte %10.0g
rep78 byte %10.0g
headroom double %10.0g
trunk byte %10.0g
weight int %10.0g
length int %10.0g
turn byte %10.0g
displacement int %10.0g
gear_ratio double %10.0g
foreign byte %10.0g

Next lets use the list command to view the first five observations for the variables make and foreign.

. list make foreign in 1/5

make foreign
1. AMC Concord 0
2. AMC Pacer 0
3. AMC Spirit 0
4. Buick Century 0
5. Buick Electra 0

The variable make contains the manufacturer and model of the cars in the dataset. The variable foreign is an indicator variable where 0 indicates that the car was manufactured in the United States and 1 indicates that the car was manufactured elsewhere. SAS datasets often include a separate file that contains value labels for categorical variables such as foreign. We can get the label file for this dataset by copying it from the Stata website to our working directory.

. copy https://www.stata.com/sampledata/formats.sas7bcat formats.sas7bcat

Let's clear Stata's memory and import the SAS dataset again. This time, we will use the bcat() option to import the value labels for the dataset.

. clear
. import sas myauto, bcat(formats)
(12 vars, 74 obs)

Let's type label list to view a list of value labels that were contained in the formats file.

. label list
ORIGIN:
           0 Domestic
           1 Foreign

The label ORIGIN contains labels for the variable foreign. We can attach the label ORIGIN to the variable foreign using Stata's label values command.

. label values foreign ORIGIN

Let's relist the first five observations for the variable foreign to verify that the data are labeled.

. list foreign in 1/5

foreign
1. Domestic
2. Domestic
3. Domestic
4. Domestic
5. Domestic

Now we can save our data to a Stata dataset by typing save myauto.

. save myauto
file myauto.dta saved

You can watch a demonstration of these commands by clicking on the link to the YouTube video below. You can read more about these commands by clicking on the links to the Stata manual entries below.

See it in action

Watch Import data from SPSS and SAS.