12

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jubbles
  • 4,450
  • 8
  • 35
  • 47

4 Answers4

14

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.

Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
  • +1 for the real best answer, but shouldn't that be `datafile = "test.sas7bdat"`? – Matt Parker Apr 13 '11 at 16:57
  • 1
    @Matt: no, `datafile` is the name of the text file that is written. If you want to specify the name of the SAS dataset that SAS will create from that text file, you have to use the `dataname` argument ("rdata" by default, no extension). – Hong Ooi Apr 13 '11 at 17:27
  • Huh - I thought it needed to have the .sas7bdat extension, and I did this just a few days ago. Upon closer inspection, all I had was a CSV file with a .sas7bdat extension, which SAS completely ignored. Thanks for the clarification. – Matt Parker Apr 13 '11 at 18:14
  • This is a nifty way of handling this. The only downside I've found personally is that the `INFILE` statement that R produces to read in the csv file does not work in SAS EG for SAS 9 if you are running SAS off of a server. So in my case, it's attempting to read the file I create from the server, rather than my local machine which is annoying. I haven't found a workaround yet, but if I do I will be sure to share here. – obewanjacobi Jun 07 '21 at 21:34
8

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.

Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235
  • 1
    Initially, when I tried write.xport(), I ran into the same error message. But when I changed the file extension from 'sas7bdat' to 'xpt', it worked like a charm. Thank you for your help. – Jubbles Mar 29 '11 at 18:36
  • @Jubbles: Great, glad it worked. Extensions (especially on Windows) can be a pain. They're necessary but not sufficient most of the time. – Ari B. Friedman Mar 29 '11 at 18:59
0

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"

-1

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.

RIE
  • 1
  • Unless there exists functionality within SAS/IML Studio to convert a R object (the first link you provided documents functionality from SAS to R), like a data frame, to a SAS data file object (or its functional equivalent), I feel that you missed the point of my question. I wanted to convert a R data frame into a format useable by SAS. To give you context, I am comfortable with and much prefer R to SAS, but many colleagues prefer SAS and need to use my data. – Jubbles Apr 13 '11 at 14:27
  • @Jubbles: IML/IML Studio can convert in both directions: SAS to R, and R to SAS. – Hong Ooi Apr 14 '11 at 03:10
  • @Hong Ooi: Thanks! I will keep that in mind when working with my SAS colleagues. – Jubbles Apr 14 '11 at 13:00