Hemken / SASmarkdown

A collection of R functions that extends knitr's capability for using SAS as a language engine
Other
21 stars 5 forks source link

Get and send data to R environment? #20

Closed SchmidtPaul closed 2 years ago

SchmidtPaul commented 2 years ago

Hi, fantastic package! My goal is often to compare SAS and R results, e.g. when fitting linear mixed models via PROC MIXED and glmmTMB(). Thus, I would like to

  1. Send data from R to SAS
  2. Send data from SAS to R

I know about the call ExportDataSetToR() and call ImportDataSetFromR() functions as e.g. described here, but I don't think they will work in this scenario.

So is my only option to write chunks that actually export and import files in the background or do you have a better solution?

Hemken commented 2 years ago

Paul, that page was written with SASmarkdown. But note that the latest update to SAS (M7) does not work with the latest version of R (4.2.1) – SAS says this will be fixed in their August update, M8.

Using hidden code blocks to export and import should also work just fine.

From: Paul Schmidt @.> Sent: Wednesday, July 27, 2022 2:34 AM To: Hemken/SASmarkdown @.> Cc: Subscribed @.***> Subject: [Hemken/SASmarkdown] Get and send data to R environment? (Issue #20)

Hi, fantastic package! My goal is often to compare SAS and R results, e.g. when fitting linear mixed models via PROC MIXED and glmmTMB(). Thus, I would like to

  1. Send data from R to SAS
  2. Send data from SAS to R

I know about the call ExportDataSetToR() and call ImportDataSetFromR() functions as e.g. described herehttps://www.ssc.wisc.edu/~hemken/SASworkshops/SASWindows/RfromSAS.html#sending-sas-data-to-r, but I don't think they will work in this scenario.

So is my only option to write chunks that actually export and import files in the background or do you have a better solution?

— Reply to this email directly, view it on GitHubhttps://github.com/Hemken/SASmarkdown/issues/20, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACYBMEYVOJCXUAWLES6PXYLVWDX6XANCNFSM54YWQF3A. You are receiving this because you are subscribed to this thread.Message ID: @.***>

SchmidtPaul commented 2 years ago

Thanks for the quick reply, but I think (?) you misunderstood because I was not clear enough. What I want is to have a Rmarkdown document looking something like this:

---
title: "SAS and R"
output: word_document
---

```{r, include = FALSE}
library(SASmarkdown)
knitr::opts_chunk$set(
  engine = "sas",
  engine.path = "C:/Program Files/SASHome/SASFoundation/9.4/sas.exe",
  engine.opts = "-nosplash -ls 75",
  comment = NA
)

From R to SAS and back

Assuming that the PlantGrowth dataset is the one I want to use:

lm(weight ~ group, data = PlantGrowth)
ImportDataSetFromR("PlantGrowth_SAS", "PlantGrowth"); /* <- something like this? */

proc mixed data = PlantGrowth_SAS;
class group;
model weight = group;
lsmeans group;
ods output lsmeans = lsmeans_SAS;
run;

ExportDataSetToR("lsmeans_SAS", "lsmeans" ); /* <- something like this? */
lsmeans


So as you can see, I am really trying to move data objects between R and SAS. And I would like to run my R-Code in R chunks and my SAS-code in SAS chunks. The `lsmeans` object should really contain the information of the ods output table `lsmeans_SAS` that was created in the SAS chunk above it.

Then again, you did confirm that "Using hidden code blocks to export and import should also work just fine." so I am assuming there is no more elegant way/function for this. If that is the case, feel free to close this issue!
Hemken commented 2 years ago

The import and export functions only work within the PROC IML context. If you are working from a SAS session, you can have a persistent connection to an R session, and pass commands and data back and forth.

However, SASmarkdown does not set you up with a single SAS session – each code block runs as a separate session. (“collectcode” makes it a little less awkward to bridge the gap between SAS sessions/codeblocks, by re-running selected code placed in an autoexec.sas file.)

What you are after should be possible using a Jupyter interface. You’ll need to configure both the R and the SAS kernels.

I think your original solution, exporting the data as CSV and importing it into the other software, is probably the easiest to implemtent.

From: Paul Schmidt @.> Sent: Wednesday, July 27, 2022 7:37 AM To: Hemken/SASmarkdown @.> Cc: Doug Hemken @.>; Comment @.> Subject: Re: [Hemken/SASmarkdown] Get and send data to R environment? (Issue #20)

Thanks for the quick reply, but I think (?) you misunderstood because I was not clear enough. What I want is to have a Rmarkdown document looking something like this:


title: "SAS and R"

output: word_document



library(SASmarkdown)

knitr::opts_chunk$set(

  engine = "sas",

  engine.path = "C:/Program Files/SASHome/SASFoundation/9.4/sas.exe",

  engine.opts = "-nosplash -ls 75",

  comment = NA

)

From R to SAS and back

Assuming that the PlantGrowth dataset is the one I want to use:


lm(weight ~ group, data = PlantGrowth)

ImportDataSetFromR("PlantGrowth_SAS", "PlantGrowth"); # <- something like this?

proc mixed data = PlantGrowth_SAS;

class group;

model weight = group;

lsmeans group;

ods output lsmeans = lsmeans_SAS;

run;

ExportDataSetToR("lsmeans_SAS", "lsmeans" ); # <- something like this?

lsmeans

So as you can see, I am really trying to move data objects between R and SAS. And I would like to run my R-Code in R chunks and my SAS-code in SAS chunks.

— Reply to this email directly, view it on GitHubhttps://github.com/Hemken/SASmarkdown/issues/20#issuecomment-1196672983, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACYBME4DBD6W2A2US52AYYTVWEUQRANCNFSM54YWQF3A. You are receiving this because you commented.Message ID: @.***>

SchmidtPaul commented 2 years ago

Alright, thanks for the insight. It is what I was expecting. Please feel free to close this issue now.

I do have a question that results from this discussion, however, so I will just leave it here in case you know a quick answer: Since I am going to be importing and exporting multiple csv files, I am wondering what the (relative) path is that is easiest to implement for both R and SAS. In R I can simply use here::here() and create a subfolder in the R-project where the .Rmd file is in. But how would I get this path *into" my SAS-code for the PROC IMPORT and PROC EXPORT then? Or should I take a completely different appraoch and e.g. choose an absolute path? After all, these csv files can be deleted afterwards.

Hemken commented 2 years ago

SAS will look for them in the root of the project, so I think relative paths can be based there.

If you include an R code block at the end of your document that unlink()s all the files created, then I’d be lazy and just leave everything in the project root.

From: Paul Schmidt @.> Sent: Thursday, July 28, 2022 7:17 AM To: Hemken/SASmarkdown @.> Cc: Doug Hemken @.>; Comment @.> Subject: Re: [Hemken/SASmarkdown] Get and send data to R environment? (Issue #20)

Alright, thanks for the insight. It is what I was expecting. Please feel free to close this issue now.

I do have a question that results from this discussion, however, so I will just leave it here in case you know a quick answer: Since I am going to be importing and exporting multiple csv files, I am wondering what the (relative) path is that is easiest to implement for both R and SAS. In R I can simply use here::here()https://here.r-lib.org/ and create a subfolder in the R-project where the .Rmd file is in. But how would I get this path *into" my SAS-code for the PROC IMPORT and PROC EXPORT then? Or should I take a completely different appraoch and e.g. choose an absolute path? After all, these csv files can be deleted afterwards.

— Reply to this email directly, view it on GitHubhttps://github.com/Hemken/SASmarkdown/issues/20#issuecomment-1198063512, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACYBME7DI3R3TALTFP27LLTVWJ22FANCNFSM54YWQF3A. You are receiving this because you commented.Message ID: @.***>