SAS macro to save a SAS dataset as a Stata dataset

If you want to use SAVASTATA, then feel free to download load it here:


If you are not prompted to "Save To Disk", then right-click the link and choose "Save Link Target As..."  Otherwise, you will need to save the web page to your computer.  Make sure you save the file as a plain text file not an htm/html file.

The best way to download SAVASTATA is to use Stata's command ssc install to install usesas because usesas uses SAVASTATA to load a SAS dataset into Stata.  The file will be saved in whatever directory Stata saves downloaded files that begin with the letter "s".  The Stata command findfile will help you find the file




Disclaimer:  There is no warranty on this software either expressed or implied.  This program is released under the terms and conditions of GNU General Public License.



Programmer:  Dan Blanchette
The Carolina Population Center
The University of North Carolina
Chapel Hill, NC  USA

Date:  20Oct2003
Last updated:  28Mar2011

Save the most recently created SAS dataset in the WORK library to a Stata dataset


%savastata( "directory to save Stata dataset", options );



DependencySAVASTATA requires that you have a working copy of SAS and a working copy of Stata Intercooled, SE or MP on your computer to run successfully.  If your SAS dataset is small enough SAVASTATA may work on Stata Small (Student Version).



SAVASTATA uses the most recently created dataset in the WORK directory, figures out how best to store the data as a Stata dataset, keeps most date formats and other basic formats for numeric variables, checks for invalid Stata variable names, if using or outputting to Stata 6:  checks to see if long variable names need to be shortened, prints them to the SAS log and shortens them, writes out the SAS dataset to an ASCII datafile, attempts to preserve all user-defined formats for numeric variables, writes out Stata do-files, and submits them to Stata in batch mode in order to have Stata read the data in and saves the data as a Stata dataset.

If your SAS dataset is using formats that are in a formats catalog, SAVASTATA will make an attempt to preserve them as value labels in Stata.  Stata does not allow all the variations of user-defined formats that SAS offers.


Restrictions of each version of Stata:

  1. Variable names can be no more than 8 characters long:  Stata 6
  2. Variable names can be up to 32 characters long:  Starting with Stata 7
  3. String variables can contain a maximum of 80 characters:  Stata 6, 7 and 8 Intercooled.
  4. String variables can contain a maximum of 244 characters:  Stata 7 and 8 SE and Intercooled starting with verison 9.
  5. The maximum number of variables is 2,047:  Stata Intercooled
  6. The maximum number of variables is 32,767:  Stata SE and MP starting with version 7
  7. Special missings (.a through .z) for numeric data are allowed:  Starting with Stata 8



NOTE:  Options can be used in any order.  You can specify as many as you want or none at all.
The following three options work as they do in Stata:
If the Stata dataset you want to output already exists, then overwrite it with the dataset generated by SAVASTATA.


Outputs a dataset to the previous version of Stata.  SAVASTATA uses the Stata command saveold when this option is specified.  NOTE:  Stata 9 SE or MP can only save to Stata 7 Intercooled and not to Stata 7 SE.


Outputs a Stata Intercooled dataset if using Stata SE or MP.  Starting with Stata 10 it only checks that your dataset has no more than 2,047 variables.  It does not save the dataset as an intercooled dataset since starting with Stata 10 that is no longer an option.

NOTE:  If you do not specify what version of Stata to save in, Stata will save the dataset in the current version.

Creates two check files for the user to compare the SAS input dataset with the Stata output dataset to make sure SAVASTATA created the files correctly.  This is a comparison that should be done after any datafile is converted to any other type of datafile by any software.  The files are created in the same directory as the output Stata datafile and are named starting with the name of the datafile followed by either "_SAScheck.lst" or "_STATAcheck.log".  The SAS check file contains output from PROC MEANS, PROC CONTENTS (by position), and a PROC PRINT of the first 5 observations.  The Stata check file contains the equivalent output by the Stata commands summarize, describe, and a list of the first 5 observations.

Numeric variables that contain decimals will be stored as float instead of the default of double.  This may result in a loss of precision, but float is the default storage type that Stata uses.  This will help decrease your file size.

Replace double quotes ( " ) occurring in character variables with single quotes ( ' ) and replace compound quotes ( `" or "' ) occurring in variable labels or formats with single quotes ( ' ).  SAVASTATA cannot always process character variables with double quotes or variable labels or formats with compound quotes.

Puts the intermediary files generated by SAVASTATA used to create the Stata dataset in the directory named in the pathname provided in the call to the SAVASTATA macro.  This is a debugging option.

Outputs only the ASCII datafile and does not save your dataset in Stata format.  It also turns on -messy switch so that the Stata input files are not deleted after your program has run.  Use this switch to make your own edits to the input of these data into Stata.  Use this option if you do not have Stata on the same computer that you have SAS.  The files generated by SAVASTATA can be moved to another computer that does have Stata and run there to create the Stata dataset.

The default version of Stata that your ascii file will be set to be used for will be Stata 6 (only allow up to 2,047 variables).

If you want to specify what version of Stata will be used to read the ascii data you can do so like so:
  %savastata("C:\my_data\", -ascii9); 
Here are more examples of the -ascii option:
  %savastata("C:\my_data\", -ascii9SE);     **  (for Stata 9 SE/MP) **;
  %savastata("C:\my_data\", -ascii9);       **  (for Stata 9 Intercooled) **;
  %savastata("C:\my_data\", -ascii10MP);    **  (for Stata 10 SE/MP) **;
  %savastata("C:\my_data\", -ascii8);       **  (for Stata 8 Intercooled) **;

  %savastata("C:\my_data\", -ascii9.2SE);   **  IS AN INVALID VERSION, 
                                             *     just specify: -ascii9SE **;
  %savastata("C:\my_data\", -ascii 9.2 SE); **  IT IS INVALID TO USE SPACES, 
                                             *     just specify: -ascii9SE **;


How to use the SAVASTATA macro:

Using the SAVASTATA macro requires that you understand how to use the "%include" SAS statement and that you know how to call a SAS macro.
For example, if you have copied this file to "C:\SASmacros\", then you tell SAS about this macro by adding the following line to your SAS program:
  %include "C:\SASmacros\";

The include statement makes SAS aware of the SAVASTATA macro which is in the file  To use the macro you have to make a call to it.  To do that you add a line like the following to your SAS program:
  %savastata(C:\mySASdir\, -old); 
The information inside the parentheses is passed on to the SAVASTATA macro.  The first string of information is the location you want to save your SAS dataset as a Stata dataset.  This is somewhat like a libname statement.  The second string of information is the options you wish to pass on to the SAVASTATA macro.  You can use as many options as you like or none at all.



  %include "C:\SASmacros\"; ** Include macro once in a SAS session and call it 
                                          *  as many times as you like in that session. **;

  data work.ToBeStata; ** This makes a copy of the SAS dataset in the in the WORK library. **;
   set in.mySASfile;
  options xmin; ** If you use this SAS option in Windows, you will not see all the pop-up windows when SAS invokes Stata. **;
  %savastata(C:\mydata\,);   ** Saves the dataset in the c:\mydata\ directory if it does not 
                              *  already exist in that directory. **;

  %savastata(C:\mydata\, -replace);   ** Saves the dataset c:\mydata\, overwriting it if 
                                      *  it already exists. **; 
  %savastata(C:\data\, -old);      ** Saves the dataset as the previous version of Stata 
                                    *  in c:\data\  directory **;
  %savastata("C:\data\", -old -replace);  ** Saves the dataset as the previous version of Stata 
                                           * in c:\data\  directory, overwriting it if it 
                                           * already exists. **;
  %savastata(C:\data\, -intercooled);  ** Saves the dataset as the Intercooled version of Stata 
                                        *  in C:\data\  directory.  This is only possible if
                                        *  your version of Stata is an SE edition.  Starting with
                                        *  Stata 10 it only checks that your dataset has no more
                                        *  than 2,047 variables. **;

  ** This is a UNIX/Linux example: **; 
  %savastata(/project/data/, -old -intercooled); ** Saves the dataset as previous version of 
                                                  *   Stata Intercooled in the /project/data/ 
                                                  *   directory **;


Setting up SAVASTATA

These are instructions to edit the file.

NOTE:  If you are setting up this macro on your computer for the first time, you can choose which version of Stata you are going to have SAVASTATA use.  If you do not choose to set the following switches, SAVASTATA will figure out what version of Stata you are running for you.  This may add a noticeable amount of time to processing so you may want to set these switches to the correct version of Stata.  You can easily figure out what version of Stata you are using by looking at the top of your results window in Stata or by typing in the Stata command about at the Stata command line.  One advantage of leaving SAVASTATA to figure out what version of Stata is being used is that when you upgrade your version of Stata you will not have to update SAVASTATA.

  %**  Need to set all if you set any. **;
  %let u_ver=0.0; %** version of Stata that is being used. decimal values are okay. *; 
                   %* set this if you do not want Stata to be run just figure out what *;
                   %*  version of Stata you are using. *;
  %let s_ver=0.0; %** version of Stata that data will be saved as. *; 
                   %* set this only if you set the u_ver variable. *;
                   %* old option will change this if used **;
  %let u_SEver=0; %** 1 if SE/MP version of Stata that is being used. *; 
  %let s_SEver=0; %** 1 if SE/MP version of Stata that data will be saved as. *; 
                  %* intercooled option will change this if used **;

NOTE:  If you are running SAVASTATA on the UNIX/Linux platforms then you need to be able to start a Stata batch job by:
$  stata -b do "" 
If not then change the setting of the ustata macro variable.
  %let ustata=stata; 
  /* This may work:
  %let ustata=/usr/local/stata/stata;  */

If you are running SAVASTATA on the Windows platform, you may need set the location of the Stata executable file:  stata.exe in the SAVASTATA SAS macro.  If you do not know where your stata.exe is located, find your Stata short-cut icon, right click on it, choose "properties", and look in the "target" field.  This will show you where the stata.exe is located on your hard drive.

Change what is inside the parentheses to the location of your stata.exe file.
   %let wstata=%nrstr(C:\Stata\stata.exe);  ** This may work. **;
In earlier versions of Stata, the Windows Stata executable file is named wstata.exe.


Wink Plone Theme by Quintagroup © 2013.

Personal tools
This is themeComment for Wink theme