UCLA Academic Technology Services HomeServicesClassesContactJobs
Search

Stata FAQ
How do I convert a Stata file to SAS?

In this FAQ we will cover two situations for converting SAS to Stata:

  1. Stata to SAS without Stat/Transfer
  2. Stata to SAS with Stat/Transfer

1. Stata to SAS without Stat/Transfer

Let's say that we have a Stata file called gpa.dta with variables gpa, greq, grev, mat and ar that we wish to convert into a SAS file called gpa.sd2 (or gpa.ssd01 if you are on the Cluster system). Here are the steps you will need to follow in Stata:

 
. use gpa
. outfile using gpa.txt, wide 

In the first step we read in the gpa file. In the second step we write out an ASCII file. The wide option ensures that each observation will be written on a single line. Now we will need to run SAS, to read in the ASCII file and save it as a SAS system file.

 
LIBNAME out '.';

DATA out.gpa;
  INFILE 'gpa.txt';
  INPUT gpa greq grev mat ar;
RUN;  
 

The libname statement tells SAS the directory the output file will be saved in. Using the two-part name, out.gpa, in the data statement indicates that a permanent file will be created and saved. The infile statement indicates the ASCII text file to be used, while the input statement gives the names of the variables.

2. Stata to SAS with Stat/Transfer

If you have Stat/Transfer or have access to Stat/Transfer converting Stata to SAS is fast and easy:

Note that sometimes you may find that you may have trouble using SAS 8 files created this way with Statistical Procedures (e.g. PROC REG) and will get the message "ERROR: Integer divide by zero." in your log file.  If so, see Why do I get an "Integer Divide by Zero" error when using a SAS data file? which describes the problem and how you can solve it.

3. Verifying the transfer

Regardless of how you convert from SAS to Stata its a good idea to verify that the transfer worked properly. To this end we will run some procedures in both SAS and Stata. First, the Stata statements:

 . describe

Contains data
  obs:            30                          
 vars:             5                          
 size:           720 (99.9% of memory free)
-------------------------------------------------------------------------------
   1. gpa       float  %9.0g                  
   2. greq      float  %9.0g                  
   3. grev      float  %9.0g                  
   4. mat       float  %9.0g                  
   5. ar        float  %9.0g                  
-------------------------------------------------------------------------------
Sorted by: 
 
. summarize

Variable |     Obs        Mean   Std. Dev.       Min        Max
---------+-----------------------------------------------------
     gpa |      30    3.313333   .5998467        2.5        4.3  
    greq |      30    565.3333   48.61767        500        655  
    grev |      30    575.3333   83.03441        480        720  
     mat |      30          67   9.247553         55         85  
      ar |      30    3.566667   .8384441        2.5          5 

. list in 1/5

           gpa       greq       grev        mat         ar 
  1.       3.2        625        540         65        2.7  
  2.       4.1        575        680         75        4.5  
  3.         3        520        480         65        2.5  
  4.       2.6        545        520         55        3.1  
  5.       3.7        520        490         75        3.6  

Now let's generate the same information in SAS for comparison.

LIBNAME IN '.' ;

PROC CONTENTS DATA=in.gpa;
RUN;
PROC MEANS DATA=in.gpa;
RUN;
PROC PRINT DATA=in.gpa(obs=5);
RUN;
 

The results are shown below.  As you can see, the SAS file has the same variables as the original Stata file, the variables have the same mean, standard deviation etc., and the listing of the first five variables is the same, suggesting that the file was converted successfully and without error.

CONTENTS PROCEDURE

Data Set Name: IN.GPA                                  Observations:         30
Member Type:   DATA                                    Variables:            5
Engine:        V612                                    Indexes:              0
Created:       16:00 Wednesday, June 30, 1999          Observation Length:   40
Last Modified: 16:00 Wednesday, June 30, 1999          Deleted Observations: 0
Protection:                                            Compressed:           NO
Data Set Type:                                         Sorted:               NO
Label:

-----Alphabetic List of Variables and Attributes-----

#    Variable    Type    Len    Pos
-----------------------------------
5    AR          Num       8     32
1    GPA         Num       8      0
2    GREQ        Num       8      8
3    GREV        Num       8     16
4    MAT         Num       8     24

Variable   N          Mean       Std Dev       Minimum       Maximum
--------------------------------------------------------------------
GPA       30     3.3133333     0.5998467     2.5000000     4.3000000
GREQ      30   565.3333333    48.6176733   500.0000000   655.0000000
GREV      30   575.3333333    83.0344064   480.0000000   720.0000000
MAT       30    67.0000000     9.2475533    55.0000000    85.0000000
AR        30     3.5666667     0.8384441     2.5000000     5.0000000
--------------------------------------------------------------------

OBS    GPA    GREQ    GREV    MAT     AR
1    3.2     625     540     65    2.7
2    4.1     575     680     75    4.5
3    3.0     520     480     65    2.5
4    2.6     545     520     55    3.1
5    3.7     520     490     75    3.6

Web Notes


How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


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.