开发者

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

In R, I have used the write.foreign() function from the foreign library in order to write a data frame as a SAS data se开发者_JS百科t.

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.


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.


I'm not much of a SAS user either, but I've used write.xport() before and it's worked fine. My crude understanding is that there are two types of SAS files, internal ones and XPORT files. The XPORT ones are the ones that are more compatible across different versions, architectures, etc.


This is an edit to Hong Ooi's answer.

In R:

library(foreign)

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

In SAS:

Upload both test.csv and test.sas files. Open test.sas. You may have to edit the test.sas code that is output from the write.foreign function. What worked for me is updating the INFILE line to include the library / location: "/home/kristenmae0/test.csv"


You can do it easily with SAS : just have a test with SAS/IML (proc iml) or IMLPlus (object oriented version) with SAS/IML Studio.

See this : http://support.sas.com/documentation/cdl/en/imlsstat/63827/HTML/default/viewer.htm#imlsstat_statr_sect004.htm

or download SAS/IML Studio for free : http://www.sas.com/apps/demosdownloads/92_SDL_sysdep.jsp?packageID=000721 This release of SAS/IML Studio provides the capability to interface with the R language.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜