SAVASAS
Stata command to save Stata datasets as SAS datatsets.
If you want to use savasas, then feel free to use Stata's command ssc install to download and install saswrapper:
ssc install savasas , replaceDisclaimer: 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.
About savasas
Programmer: Dan Blanchette (dan_blanchette@unc.edu)
The Carolina Population Center
The University of North Carolina
Chapel Hill, NC USA
Save a Stata dataset as a SAS dataset
The Carolina Population Center
The University of North Carolina
Chapel Hill, NC USA
Date: 27Oct2003
Last updated: 16Aug2010
Save a Stata dataset as a SAS dataset
savasas [varlist] [using filename ] [if exp]
[in range] [ , replace formats rename check
type( version_of_sas_data_file ) messy sascode ]
Description
savasas saves the Stata dataset in memory as a SAS file. By default the output dataset will have
the same name and be in the same directory, but with the "
The procedure is as follows:
The maximum length for a string variable is 244 characters. In such cases, the first 200 characters will be taken and passed on to SAS (this is a limitation of the SAS xport dataset used to transfer data from Stata to SAS).
NOTE: savasas calls SAS to run the SAS program. This requires the ability to run SAS on your computer. If you do not have a working copy of SAS, use the sascode option and copy the SAS program (
.sas7bdat" file name
extension and contain all observations and every variable in the Stata datafile. savasas can save
to other versions of SAS. Version 7/8/9: "*.sas7bdat",
Version 6: "*.ssd01" / "*.sd2"
, SAS 6 Transport/xport: .xpt.
The procedure is as follows:
- savasas uses Stata's command fdasave to save the dataset in memory temporarily as a SAS xport datafile.
- savasas writes a SAS input program to load the dataset into SAS and to assign variable names, labels (and formats).
-
savasas runs the program in SAS in batch mode to load the xport data
Note to Windows users: This is where the Windows operating system will pop-up windows that indicate that SAS is running in batch. Do not try to close these windows as that will potentially halt savasas. - SAS saves the data as the version of SAS being run on by savasas unless a other version SAS file type is specified.
The maximum length for a string variable is 244 characters. In such cases, the first 200 characters will be taken and passed on to SAS (this is a limitation of the SAS xport dataset used to transfer data from Stata to SAS).
NOTE: savasas calls SAS to run the SAS program. This requires the ability to run SAS on your computer. If you do not have a working copy of SAS, use the sascode option and copy the SAS program (
mySASfile_infile.sas) and the xport datafile
(mySASfile_infile.raw) to a computer that has a working copy of SAS. The SAS
program will need to be modified to point to the location of the xport datafile.
Options
replace works like Stata's replace in that it allows you to overwrite the SAS
datafile if it already exists.
formats specifies to save value labels that exist in the Stata dataset as SAS formats in a SAS
formats in a file that will have the same name as the datafile but with the
"
.sas7bcat" file extension. This formats catalog file will be created in
the same directory as the SAS datafile. To use this SAS formats catalog file you will need to add code to your
program to make SAS aware that this file contains format definitions used by variables in the dataset. Your code
should look something like this:
libname in "c:\myData";
options fmtsearch=(in.mySASdataset);
NOTE: SAS user-defined formats have to be 8 characters or less and cannot end in a number.
This is a list of format names that SAS reserves and thus cannot be user-defined format names:
best, binary, comma, commax, d, date, datetime, dateampm, day, ddmmyy, dollar,
dollarx, downame, e, eurdfdd, eurdfde, strong eurdfdn, eurdfdt, eurdfdwn, eurdfmn,
eurdfmy, eurdfwdx, eurdfwkx, f, float, fract, hex, hhmm, hour, ib, ibr, ieee, julday,
julian, percent, minguo, mmddyy, mmss, mmyy, monname, month, monyy, negparen, nengo,
numx, octal, pd, pdjulg, pdjuli, pib, pibr, pk, pvalue, qtr, qtrr, rb, roman, s370ff,
s370fib, s370fibu, s370fpd, s370fpdu, s370fpib, s370frb, s370fzd, s370fzdl, s370fzds,
s370fzdt, s370fzdu, ssn, time, timeampm, tod, weekdate, weekdatx, weekday, worddate,
worddatx, wordf, words, year, yen, yymm, yymmdd, yymon, yyq, yyqr, z, zd
savasas will make some attempt to rename invalid SAS formats but it would be best for you to rename or
drop them yourself before saving to SAS.
rename specifies that any required renaming of the file name and/or variable names is to be
done. The rename option is only necessary when saving to SAS 6/transport or when variable
names are not unique in SAS. When saving to SAS 6/transport rename attempts to rename long
variable names (more than 8 characters) to be unique by shortening all long variable names to the first 8 characters
or up to 7 plus a number. savasas displays the list of renamed variables.
rename also renames the SAS file name when the name provided is not a valid SAS file name.
check specifies that basic stats for both datasets are to be generated to compare the Stata input
dataset with the SAS output dataset in order to make sure savasas 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 SAS file is created in the same directory as the output SAS datafile and is named starting with
the name of the datafile followed by "
_SAScheck.lst" (SAS), for example:
"mydata_SAScheck.lst" .
type(version_of_sas_data_file) specifies the SAS datafile version desired,
from the following list:
|
one of
|
|
SAS version datafile |
| ------- |
|
------------------------------------------------------ |
| sas |
|
SAS version 9 * the default if no type specified * |
| sas6 |
|
SAS version 6 |
| sasx |
|
SAS version 6 transport/xport |
Hint: If you specify the correct file name extension, you do not have to specify type. Remember
that a SAS Version 9 (
SAS version 6 and Transport/xport file restrictions:
.sas7bdat) file
will be created if no file extension or type specification is made.
SAS version 6 and Transport/xport file restrictions:
-
File names or variable names cannot be longer than 8 characters. savasas checks for names that are
longer than 8 characters; and, if the rename option is issued, renames them to the first 8 characters or up to 7 plus a
number. In addition, it displays the list of renamed variables.
- Variable labels can be a maximum of 40 characters. savasas saves the first 40 characters.
messy specifies that all the intermediary files created by savasas during its
operation are not to be deleted. The messy option prevents savasas from
cleaning up after it has finished. This option is mostly useful for debugging purposes in order to find out
where something went wrong. All intermediary files have a name starting with an underscore ( _ ) followed by
the process ID and are located in Stata's temp directory.
sascode specifies that only a SAS xport datafile and an input SAS program are to be created.
By default, savasas executes all four steps outlined above. The sascode
option aborts this process after step 2. The user then needs to read in the data manually using SAS.
savasas writes a SAS program
(
mydata_infile.sas) to read
in the xport datafile (mydata.xpt).
Examples
To save the dataset in memory to the same directory with the same name as the dataset in memory
as a "
.sas7bdat" file, type:
. savasasTo save the dataset in memory to the current directory with the name mySASdata as a "
.sas7bdat" file, type:
. savasas using mySASdataTo save the dataset in memory to c:\data\ directory with the name mySASdata as a "
.sas7bdat" file and create
2 check files, type:
. savasas using "c:\data\mySASdata", checkTo save only variables between var1 and var20 from the dataset in memory to the current directory with the name mySASdata as a "
.sas7bdat" file and create a
"formats.sas7bcat" file
containing SAS formats of any Stata value labels, replacing an existing file if necessary, type:
. savasas gender-q32_b using mySASdata, replace formatsMore examples:
. savasas using mySASdata.sas7bdat, replace . savasas in 1/30 using mySASdata, type(sas6) check . savasas using mySASdata, messy type(sasx) rename
Setting up savasas
If you are setting up this program on your computer for the first time, you may need to edit the
sasexe.ado file to set the location of
your SAS executable file
(sas.exe) . If
you do not, savasas will look in the "usual" locations for it. savasas
also may need to have the location of the SAS macro savastata.sas set. The
sasexe.ado file is an ASCII text file
and should be saved as such after editing. Stata's do-file editor will do the job.
Acknowledgements
The user-written program outdat inspired this program.


