Wednesday, December 14, 2011

Running Stata in batch from SAS

Admit it - some statistical software packages have an easier way of doing something than others. That's why one may want to use two or more packages on the same data.  Here is an easy way to run Stata code from SAS.

1.  Output the data set from SAS using PROC EXPORT.
proc export data=out
outfile='C:\Clients\StataFromSAS\data1.csv'
dbms=csv
replace;
run;

2.  Make a Stata .do file that would read in data1.csv and perform the desired analysis.  Save it as 'C:\Clients\StataFromSAS\analysisStata.do".  For example:
insheet using 'C:\Clients\StataFromSas\data1.csv', clear
set linesize 100
summarize var1
tab var1


3.  In SAS, the X statement issues an operating-environment command from within a SAS session (e.g. performs a DOS command under Windows).  Since Stata can be run from a DOS prompt in batch mode, we can call its .do file from SAS using X statement.  First, change DOS directory to where the Stata log file should go, and then execute analysisStata.do in batch mode (for more on Stata in batch mode, see appendix C.5 of Stata [GS] manual).  The following SAS statements accomplish this:
x "cd C:\Clients\StataFromSAS\";
x '"C:\Program Files (x86)\Stata12\StataSE-64.exe" /e do "C:\Clients\StataFromSAS\analysisStata.do"';

4. Enjoy :)

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner

Followers

google analytics