Save dataset as a SAS dataset
savasas [varlist] [using filename] [if] [in] [, type(version_of_sas_data_file) replace formats rename messy check sascode compress ]
NOTE: Before the first use of savasas the sasexe.ado file may need to be edited to set the location of your SAS executable file. Instructions on how to do this is at the bottom of this help file.
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 ".sas7bdat" file name extension and contain all observations and every variable in the Stata datafile. savasas can save to other versions of SAS: Version 8 or 9 (*.sas7bdat), Version 6 (*.sd2/*.ssd01/*.ssd02), SAS 6 Transport/xport (*.xpt).
The procedure is as follows:
(1) savasas uses fdasave to save the dataset in memory temporarily to a SAS xport datafile.
(2) savasas writes a SAS input program to load the data into SAS and to assign variable names, labels (and formats).
(3) savasas runs the program in SAS in batch mode to load the xport datafile created by fdasave.
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.
(4) SAS saves the data as whatever version SAS file type was specified.
For Stata SE users: 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 (myData_infile.sas) and the SAS xport datafile (myData.xpt) to a machine that has a working copy of SAS. The SAS program will need to be modified to point to the location of the SAS xport datafile.
When savasas has successfully saved your data as a SAS dataset it will display a message similar to the following:
savasas successfully saved the SAS file: *
SAS reports that the dataset has 1000 observations and 20 variables. *
type(version_of_sas_data_file) specifies the SAS datafile version desired, from the following list:
one of SAS version datafile
sas current version of SAS * 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 current version SAS (.sas7bdat) file will be created if no file extension or type specification is made.
SAS version 6 and Transport/xport file restrictions: 1) 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.
2) Variable labels can be a maximum of 40 characters. savasas saves the first 40 characters. replace works like Stata's replace in that it allows you to overwrite the SAS data file if it already exists.
formats specifies that value labels are to be saved as 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";
NOTE: SAS user-defined formats for versions prior to SAS version 9 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, 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. Formats will be renamed if needed.
rename specifies that any required renaming of 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 the 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.
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.
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 data file followed by "_SAScheck.lst" (SAS), for example: "mydata_SAScheck.lst"
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. For example: savasas writes a SAS program myData_infile.sas to read in the xport datafile myData.xpt.
compress specifies that the SAS dataset is to be created with minimum data storage settings that still accurately store the data. This used to be the default for savasas, but the compress command can slow down savasas quite a bit when the dataset has many thousands of variables.
To save the dataset in memory to the same directory with the same name as the dataset in memory as a sas7bdat file, type
To save the dataset in memory to the current directory with the name mySASdata as a sas7bdat file, type
. savasas using mySASdata
To 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.sas7bdat, check
To 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, formats replace
. savasas using mySASdata.sas7bdat, replace
. savasas in 1/30 using mySASdata, type(sas6) check
. savasas using mySASdata, sascode type(sas6)
. savasas using mySASdata, messy type(sasx) rename
Setting up savasas
If you are setting up this program on your computer for the first time, please edit sasexe.ado to set the location of your SAS executable file (sas.exe). If you do not, savasas will look in the "usual" locations for it. 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 trick.
edit sasexe.ado (click, to edit the sasexe.ado file, remember to save when done.)
Dan Blanchette The Carolina Population Center University of North Carolina - Chapel Hill, USA firstname.lastname@example.org
The user-written program outdat inspired this program.
On-line: fdasave, save, outdat (if installed), usesas (if installed)