SAS/IML Software and Matrix Computations

Statistical programming, matrix languages, and more
BookmarkSubscribeRSS Feed
Visiting
Obsidian | Level 7

Recently working on a project requiring use RData dataset, which created by R, searched online, seems there is no good method to import RData into SAS, any suggestion? Thank you.

 

here is a piece of code to output a RData format table using R:

setwd("C:\path...")

library(car)

test<- mtcars

write.RData(test, "mydata.RData)

 

I have tried to output sas7bdat format table from R, but it can't be opened by SAS.

 

 

21 REPLIES 21
LinusH
Tourmaline | Level 20
Either if you can output to a csv-file instead.
Or if you have an IML license, call R from there and output that to a SAS dataset:
https://stackoverflow.com/questions/29376903/is-there-a-way-to-load-rda-rdata-data-from-r-into-sas
Data never sleeps
quickbluefish
Barite | Level 11

I second the PROC IML solution if you have it available.  Everything between the SUBMIT / R; and the ENDSUBMIT; statements is R code.  You can then use this call routine to import the R dataframe to a SAS dataset:

https://documentation.sas.com/doc/en/pgmsascdc/v_059/casimllang/casimllang_proc_sect041.htm

The only thing you'll need to figure out is what R statement you need to read the .RDat file into a dataframe (after SUBMIT / R;)

Visiting
Obsidian | Level 7

The RData file is already saved by others, don't need to generate dataset in SAS, could you please paste a sample code for importing data only. Thank you.

Tom
Super User Tom
Super User

@Visiting wrote:

The RData file is already saved by others, don't need to generate dataset in SAS, could you please paste a sample code for importing data only. Thank you.


You have lost me here.  You started by saying you wanted to create SAS datasets, now you saying you don't?  What is it that you need to do?

Visiting
Obsidian | Level 7

I want to import RData format data into SAS, which is generated by R.

Patrick
Opal | Level 21

@Visiting wrote:

I want to import RData format data into SAS, which is generated by R.


To make the data available to SAS you need to convert your R data frame to a format SAS can read. The most obvious format is a SAS data set.

@quickbluefish already shared this link with a code template how to achieve this.
As documented under the link you need SAS/IML as well as:

Patrick_0-1738712359251.png

 

If above requirements are not met then convert your R data frame to a .csv on any environment where R is installed and then load this csv to a location that SAS can access. The read the csv into a SAS data set. 

I'm not sure what you mean by "CSV has limitation, worrying about losing records if the dataset is too big". A csv is just a text file that can become very very big. Just don't open it with Excel as there you've got of course limitations - but that has nothing to do with the csv. 

And just thinking: If you're on SAS Viya 3.5 or newer then consider writing the R data frame to a parquet file and then use the SAS parquet libname engine to read it. Parquet has the advantage over csv that it maintains the variable names and types and it's also compressed meaning less data to push from your R environment to your SAS environment.

Visiting
Obsidian | Level 7

How to check if IML license available? CSV has limitation, worrying about losing records if the dataset is too big.

SASKiwi
PROC Star

Run PROC PRODUCT_STATUS to confirm IML is installed and PROC SETINIT to check if it is licensed:

proc product_status;
run;

proc setinit;
run;

 

Visiting
Obsidian | Level 7

The license is available until Dec2025.

Ksharp
Super User

As quickbluefish said , you get job done with SAS/IML .

Firstly read in RData by R statement,

Once read this Rdata as a DataFrame with R and   using function  call ImportDataSetFromR() to bring it in SAS.

 

 

 




proc iml;
submit / R;
setwd("C:\path...")
library(car)
read.RData(test, "mydata.RData)   /*<---Read in RData with R statement*/
endsubmit;

call ImportDataSetFromR("Work.MyData", "test");   /*<--- write DataFrame "test" into a sas dataset "MyData"*/
quit;

 

Visiting
Obsidian | Level 7

It is close to get it work, thank you!

But I got an error: "The RLANG system option must be specified in the SAS configuration file or on the SAS invocation command line to enable the submission of R language statements".

How can I solve this issue?

 

Tom
Super User Tom
Super User

That option needs to be set when you launch SAS.  How are you accessing SAS?

Do you run SAS directly? On a personal computer? Or on a shared multi-user server?  

Or are you using some front end to run SAS.  Like SAS/Studio or Enterprise Guide.

 

Visiting
Obsidian | Level 7

I use SAS EG at a cloud server.

Tom
Super User Tom
Super User

@Visiting wrote:

I use SAS EG at a cloud server.


No idea what that means. 

 

Are you running a virtual PC in the cloud so you can run the Windows application that is Enterprise Guide?   

Or do you mean that the SAS server your Enterprise Guide sessions are using to actually run SAS code is in the cloud?

Perhaps both machines are in the cloud.

 

Whose cloud.  Your companies cloud?  or are you paying SAS to host the your EG and SAS servers?

 

To be able to execute R code from within PROC IML it has nothing to do with Enterprise Guide.  Only with the SAS sessions you are using Enterprise Guide to access.  

 

So to use Enterprise guide to run this code you need>

 

1) R is installed on the machine where SAS (not Enterprise Guide) is running.

2) PROC IML is licensed (and installed).

3) The SAS session was started with the -RLANG option.  And the RHOME environment variable is set properly so that R runs.

   - For Enterprise Guide that means that the application server that you connect to from EG to be able to run SAS code has to have the first 3 things set.

 

If you cannot do that then you will probably need to just run an independent R session to read in the Rdata file and convert it to something that SAS can read directly. 

 

1) You could write a CSV file. Make sure you do not have any character variables with imbedded end-of-line characters since they will cause the CSV file to be hard or impossible to read for SAS.  Since you know what you are writing it should be easy to write the data step to read it into a dataset. (Don't use PROC IMPORT for anything important as it has to GUESS what you intended.)

 

2) There are available R packages that can create SAS transport files.  You can then use the SAS autocall macro %XPT2LOC to read the file.   If you have trouble with the file you might try seeing if the %XPORT2SAS macro can read it instead as it has some enhancement to handle some of the mistakes that non-SAS programs for making transport files have made in the past.

 

3) You could use some other format.  Perhaps an XLSX file.  That has fewer issues with PROC IMPORT since there is less guessing involved on the SAS side than for a CSV file. Especially if the XLSX file is made from a dataset (or what R likes to call dataframes) where the cells in a column are of a single type.

 

 

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 21 replies
  • 2337 views
  • 11 likes
  • 8 in conversation