Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can anyone help me write a R data frame as a SAS data set?

Tags:

r

dataset

sas

In R, I have used the write.foreign() function from the foreign library in order to write a data frame as a SAS data set.

write.foreign(df = test.df, datafile = 'test.sas7bdat', codefile = 'test.txt', package = "SAS")

The SAS data file is written, but when I try to open it in SAS Viewer 9.1 (Windows XP), I receive the following message - "SAS Data set file format is not supported".

Note: I am generally unfamiliar with SAS, so if an answer exists that would have been known by a regular SAS user, please excuse my ignorance.

like image 786
Jubbles Avatar asked Mar 29 '11 17:03

Jubbles


People also ask

Can R use SAS files?

The easiest way to import SAS files into R is to use the read_sas() function from the haven library. The following step-by-step example shows how to import a SAS file into R in practice.

Can R read SAS data sets?

In R, there are a couple of that can read SAS files into dataframes. In this post, we are going to use the r-packages haven, sas7bdat, and the GUI of RStudio to load a SAS file as well.


1 Answers

write.foreign with option package="SAS" actually writes out a comma-delimited text file and then creates a script file with SAS statements to read it in. You have to run SAS and submit the script to turn the text file into a SAS dataset. Your call should look more like

write.foreign(df=test.df, datafile="test.csv", codefile="test.sas", package="SAS")

Note the different extension. Also, write.foreign writes factor variables as numeric variables with a format controlling their appearance -- ie, the R definition of a factor. If you just want the character representation, you'll have to convert the factors via as.character before exporting.

like image 67
Hong Ooi Avatar answered Oct 14 '22 11:10

Hong Ooi