Below is a comparison of the commands used to write data into various file formats using R, SAS, SPSS and Stata. The variables gender and workshop are categorical factors and q1 to q4, pretest and posttest are considered continuous and normally distributed.

The practice data set is shown here. The programs and the data they use are also available for download here. Examples that are missing for Stata reflect differences between the two books. We will get around to those when we write the second edition of *R for Stata Users*.

**Writing Comma-Delimited Text Files**

**R**

write.csv(mydata, file="mydataFromR.csv";)

**SAS**

PROC EXPORT DATA=MYLIB.MYDATA OUTFILE="mydataFromSAS.csv" DBMS=CSV REPLACE PUTNAMES=YES;

**SPSS**

SAVE TRANSLATE OUTFILE='mydataFromSPSS.csv' /TYPE=CSV /MAP /REPLACE /FIELDNAMES /CELLS=VALUES.

**Stata**

use mydata,clear outfile using mydata.csv, comma type mydata.csv

**Writing Tab Delimited Files**

**R**

write.table(mydata, file = "mydataFromR.tab", quote = FALSE, sep = "t";, na = "", row.names = TRUE, col.names = TRUE) # Space delimited: write.table(mydata, file = "mydataFromR.txt")

**SAS**

PROC EXPORT DATA=MYLIB.MYDATA OUTFILE="mydataFromSAS.txt" DBMS=TAB REPLACE; PUTNAMES=YES;

**SPSS**

SAVE TRANSLATE OUTFILE='mydataFromSPSS.dat' /TYPE=TAB /MAP /REPLACE /FIELDNAMES /CELLS=VALUES.

**Stata**

use mydata,clear outfile using mydata.tab, tab type mydata.tab

**Writing Excel Files**

**R**

# Do this once: install.packages("xlsReadWrite") library("xlsReadWrite") xls.getshlib() # Write the file: library("xlsReadWrite") write.xls(mydata, file = "mydataFromR.xls")

**SAS**

PROC EXPORT DATA=MYDATA OUTFILE="mydata.xls"; DBMS=EXCELCS LABEL REPLACE; SHEET="mydata"

**SPSS**

SAVE TRANSLATE OUTFILE='mydataFromSPSS.xls' /TYPE=XLS /VERSION=2 /MAP /REPLACE /FIELDNAMES.

**Writing SAS Files**

**R**

library("foreign") write.foreign(mydata, datafile = "mydataFromR.txt", codefile = "mydataFromR.sas", package = "SAS")

**Writing SPSS Files**

**R**

library("foreign") write.foreign(mydata, datafile = "mydataFromR.txt", codefile = "mydataFromR.sps", package = "SPSS")

**Writing Stata Files**

**R**

library("foreign") write.foreign(mydata, datafile="mydata2.csv", codefile="mydata2.do", package="Stata")

i want to convert R file into PDF or XLS.

Please help me out

Hi Jay,

That depends on what type of file you’re converting. The best place to ask questions like this is:

http://stackoverflow.com/questions/tagged/r

Cheers,

Bob

Thanks a lot for this nice post. I have juste found out that the package ‘xlsReadWrite’ is not available (for R version 3.2.4). Do you have other alternative to export excel file from R.

Hi Vincent,

Here’s the latest on reading & writing Excel files.

http://www.mango-solutions.com/wp/2015/05/r-the-excel-connection/

http://www.milanor.net/blog/steps-connect-r-excel-xlconnect/

Cheers,

Bob