SAS FAQ: How do I read SPSS or Stata data files into SAS using Proc Import?

Note: This page was done using SAS version 9.1.3. We assume that SAS/ACCESS Interface to PC File Formats has been installed and licensed, which should be the case with  most SAS users on UCLA campus.


Stata files

Note: SAS supports Stata up to version 9. If you have a Stata version 10 file you must save it as a version 9 file before you can import it using SAS. Use the following Stata command to save hsb.dta as hsb_old.dta, a version 9 file.

	saveold hsb_old, replace

Reading a Stata file into SAS using proc import is quite easy and works much like reading in an Excel file. SAS recognizes the file extension for Stata (*.dta) and automatically knows how to read it. Let's say that we have the following data stored in a Stata file hsb.dta.

     +-----------------------------------+
     | id   female   read   write   math |
     |-----------------------------------|
  1. |  1   female     34      44     40 |
  2. |  2   female     39      41     33 |
  3. |  3     male     63      65     48 |
  4. |  4   female     44      50     41 |
  5. |  5     male     47      40     43 |
     |-----------------------------------|
  6. |  6   female     47      41     46 |
  7. |  7     male     57      54     59 |
  8. |  8   female     39      44     52 |
  9. |  9     male     48      49     52 |
 10. | 10   female     47      54     49 |
     +-----------------------------------+

Then the following proc import statement will read the hsb.dta data file and create a temporary data set called mydata. The proc print statement lets us see that we have imported the data correctly. From the proc contents output below we can see that SAS takes both variable labels and value labels from the Stata file.

proc import datafile="d:\hsb.dta" out=mydata dbms = dta replace;
run;
proc print data=mydata;
run;

Obs    ID    FEMALE    READ    WRITE    MATH

  1     1    female     34       44      40
  2     2    female     39       41      33
  3     3    male       63       65      48
  4     4    female     44       50      41
  5     5    male       47       40      43
  6     6    female     47       41      46
  7     7    male       57       54      59
  8     8    female     39       44      52
  9     9    male       48       49      52
 10    10    female     47       54      49
		
proc contents data= mydata2;
run;
	
#    Variable    Type    Len    Format     Label
2    FEMALE      Num       8    FEMALE.    female
1    ID          Num       8               id
5    MATH        Num       8               math score
3    READ        Num       8               reading score
4    WRITE       Num       8               writing score

SPSS files

Reading a SPSS file into SAS using proc import is quite easy and works much like reading an Excel file. SAS recognizes the file extension for SPSS (*.sav) and automatically knows how to read it. Let's say that we have the following data stored in a SPSS file hsb.sav.

   id   Female   Read   Write    Math
  1.00	1.00	34.00	44.00	40.00
  2.00	1.00	39.00	41.00	33.00
  3.00	.00	63.00	65.00	48.00
  4.00	1.00	44.00	50.00	41.00
  5.00	.00	47.00	40.00	43.00
  6.00	1.00	47.00	41.00	46.00
  7.00	.00	57.00	54.00	59.00
  8.00	1.00	39.00	44.00	52.00
  9.00	.00	48.00	49.00	52.00
  10.00	1.00	47.00	54.00	49.00

Then the following proc import statement will read it in and create a temporary data set called mydata. The proc print command lets us see that we have imported the data correctly. From the proc contents output below we can see that SAS takes both variable labels and value labels from the SPSS file.

proc import datafile="d:\hsb.sav" out=mydata dbms = sav replace;
run;
proc print data=mydata;
run;

Obs           ID    FEMALE         READ        WRITE         MATH

  1         1.00    female        34.00        44.00        40.00
  2         2.00    female        39.00        41.00        33.00
  3         3.00    male          63.00        65.00        48.00
  4         4.00    female        44.00        50.00        41.00
  5         5.00    male          47.00        40.00        43.00
  6         6.00    female        47.00        41.00        46.00
  7         7.00    male          57.00        54.00        59.00
  8         8.00    female        39.00        44.00        52.00
  9         9.00    male          48.00        49.00        52.00
 10        10.00    female        47.00        54.00        49.00


proc contents data=mydata;
run;

  #    Variable    Type    Len    Format     Label

  2    FEMALE      Num       8    FEMALE.    FEMALE
  1    ID          Num       8    F9.2       ID
  5    MATH        Num       8    F9.2       math score
  3    READ        Num       8    F9.2       reading score
  4    WRITE       Num       8    F9.2       writing score

How to cite this page

Report an error on this page or leave a comment

The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.