Home > Net >  How to write sas datasets using libr package
How to write sas datasets using libr package

Time:11-05

I'm using the libr package to read and write SAS datasets. Reading datasets is fine. But writing them, the datasets usually don't open in SAS. What am I doing wrong?

Here is some sample code:

library(libr)

libname(test, "./data", "sas7bdat")

lib_add(test, mtcars)

lib_write(test) 

I don't get any errors. But then the mtcars.sas7bdat doesn't open in SAS. What is the problem?

CodePudding user response:

Under the hood, the libr package is using haven to read and write datasets. The problem is that haven is not very reliable at writing SAS datasets, and therefore, libr is not reliable at writing SAS datasets.

So you are doing nothing wrong. It just doesn't work very well. I suggest you output to XPT instead of sas7bdat. That is more reliable. Like this:

library(libr)

libname(test, "./data", "xpt")

lib_add(test, mtcars)

lib_write(test)

The haven XPT engine is much better in this situation, and is your best option. Then you can open the XPT file from SAS no problem.

  •  Tags:  
  • r sas
  • Related