-------------------------------------------------------------------------------
help for saswrapper                                           manual:  [R] none
                                                             dialog:   none    
-------------------------------------------------------------------------------

Run a SAS program from within Stata

saswrapper [varlist] [if] [in] [using sas program filename] [, pre_sas_prog(`"sas code"') post_sas_prog(`"sas code"') check messy usesas nodata savasas(sas dataset name) noformats rename clear char2lab float quotes]

Description

saswrapper runs a SAS program in batch and prints the output in the Stata results window/log file. This usually occurs by specifying a SAS program file after using, but saswrapper can also run SAS code specified by the pre_sas_prog() and/or post_sas_prog() options. By default, saswrapper will save the current data in memory using savasas and make it available in SAS's WORK library. If that is not desired, use the nodata option. The usesas option tells saswrapper to load the last SAS dataset created in the WORK library by the submitted SAS program into Stata using the SAVASTATA SAS macro.

NOTE: saswrapper calls SAS to run the SAS program. This requires the ability to run SAS on your computer. If saswrapper does not run SAS for you, your sasexe.ado file may need to be edited to set the location of your SAS executable file (sas.exe) and your SAVASTATA SAS macro file (savastata.sas). It may be that saswrapper will be able to run with the default settings in sasexe.ado. See the setup instructions below.

saswrapper uses the SAVASTATA SAS macro to create the Stata dataset from the SAS dataset when the usesas option is specified. saswrapper downloads the SAVASTATA SAS macro and stores it where user-written Stata ado-files are stored that begin with the letter "s". This macro can be used in SAS. Learn about SAVASTATA here: http://www.cpc.unc.edu/research/tools/data_analysis/sas_to_stata/savastat > a.html

Options

using sas program filename specifies saswrapper to run this SAS program. saswrapper assumes the SAS program file extension .sas if no file extension/suffix is specified.

pre_sas_prog() and/or post_sas_prog() contain SAS code to be run by saswrapper. If SAS programming code is supplied by using, pre_sas_prog(), and post_sas_prog() then the order the code will be run is:

pre_sas_prog()

using

post_sas_prog()

It is best to enclose the SAS code within compound double quotes:

. saswrapper, pre_sas_prog(`"proc means;"')

in case the SAS code contains any double quotes. An interesting way to submit SAS code is to first put it in a local macro and use three forward slashes to continue the line:

. local pre_sas_prog data new; /// set work.stata_data; /// gender = "female"; /// run;

. saswrapper , pre_sas_prog(`" `pre_sas_prog' "')

You can put a lot of SAS code in a local macro if you don't use an equal sign (=) after then local macro name. For the above example to work, Stata has to be using the carriage return as an end-of-line delimiter and not semi-colons which is changed by the #delimit command. Stata will see Stata-style comments in the SAS code as Stata comments so avoid starting a line with a star/asterisk "*":

* some comment ; ///

because Stata will comment out the three forward slashes at the end and thus end the input to the local macro. Since the contents of the local macro will be one long line, do not attempt to do anything in your SAS code that expects carriage returns like the SAS datalines statement that reads in raw data.

check specifies to generate basic stats for both the SAS and Stata datasets for the user to make sure saswrapper 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 SAS program specified in using or if no program was specified in using then it will be created in the current directory. The file is named starting with the name of the datafile followed by "_SAScheck.lst" (SAS). e.g. "mySASdata_SAScheck.lst"

messy specifies that all the intermediary files created by saswrapper during its operation are not to be deleted. The messy option prevents saswrapper 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.

usesas specifies to load the SAS dataset into memory that was most recently created in the SAS WORK library in the SAS program submitted to saswrapper. saswrapper figures out how much memory the SAS dataset will require to be loaded into Stata and sets Stata's memory for you if your memory setting is less than is required.

nodata specifies to override the default behavior of saswrapper which is to save the current dataset in memory to the SAS WORK library. Use this option when your SAS program is not going to use the dataset in memory.

savasas specifies a different dataset name than the name of the dataset in memory. If the dataset in memory does not have a name and the option savasas is not used, then the dataset in memory will be available in SAS's WORK library as "stata_data" (a.k.a. "WORK.STATA_DATA").

Options when saving data to SAS

varlist specifies what selection of variables in the dataset in memory are to be saved to the SAS dataset in the WORK library in the SAS program submitted to saswrapper. If no variables are specified then all variables will be saved the SAS dataset.

if exp subsets the dataset in memory before saving the dataset to the SAS dataset in the WORK library in the SAS program submitted to saswrapper.

in range subsets the dataset in memory before saving the dataset to the SAS dataset in the WORK library in the SAS program submitted to saswrapper.

noformats specifies that no value labels be saved as SAS formats.

rename specifies that any required renaming of file name and/or variable names is to be done when saving the dataset in memory to SAS using savasas. The rename option is only necessary when variable names are not unique in SAS. 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.

Options when using the usesas option

clear specifies to clear the data currently in memory before running saswrapper.

char2lab specifies to encode long SAS character variables like the Stata command encode. Character variables that are too long for a Stata string variable are maintained in value labels.

float specifies that numeric variables that would otherwise be stored as numeric type double be stored with numeric type float. This option should only be used if you are certain you have no integer variables that have more than 7 digits (like an id variable).

quotes specifies that double quotes that exist in string variables are to be replaced with single quotes. Since the data are written out to an ASCII file and then read into Stata, there are rare instances when double quotes are not allowed inside string variables.

Examples

. saswrapper using "mySASprog.sas"

. saswrapper using "mySASprog.sas", nodata usesas clear

. saswrapper using "c:\data\mySASprog.sas", savasas(tester) check

. saswrapper , pre_sas_prog(`" proc print; "') . saswrapper , pre(`" data new; set work.stata_data; if income > 0 then do; employed = 1; output; end; run; "') usesas clear

. saswrapper , post(`" proc univariate; "')

NOTE: 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, saswrapper will try to set it for you. 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.

Setting up saswrapper

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, saswrapper will look in the "usual" locations for it. saswrapper 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. edit sasexe.ado (click, to edit the sasexe.ado file, remember to save when done.)

Author

Dan Blanchette The Carolina Population Center University of North Carolina - Chapel Hill, USA dan_blanchette@unc.edu

Also see

On-line: savasas, usesas