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.4.
Stata files
Note: SAS 9.4 can import Stata .dta up to version 12. If you have a Stata version 13 or later file,
you must save it as a version 12 file before you can import it using SAS. Use the
saveold
Stata command to save hsbdemo.dta as hsb_old.dta, , and the version
option to specify version 12. From within Stata 14 we issue the commands:
use https://stats.idre.ucla.edu/stat/data/hsbdemo, clear saveold hsb_old, version(12) 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 hsbdemo.dta (some of the data has been omitted).
+-----------------------------------+ | 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.
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.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
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