Basic Use of SASmarkdown

Doug Hemken

Jul 2017

This discussion assumes you already have a basic understanding of Markdown for document formatting, Rmarkdown to include executable code in a document, and SAS to write the code.

In a first code chunk, set up your SAS engine configuration.

This depends on your operating system, the version of SAS, and whether or not you have SAS installed in the default location. This example catches both Windows and linux SAS for me.

require(SASmarkdown)

if (file.exists("C:/Program Files/SASHome/SASFoundation/9.4/sas.exe")) {
  saspath <- "C:/Program Files/SASHome/SASFoundation/9.4/sas.exe"
} else {
  saspath <- "sas"
}

sasopts <- "-nosplash -ls 75"

Then set up SAS code chunks.

A simple code chunk in Rmarkdown might look like:

```{r example1, engine="sas", engine.path=saspath, engine.opts=sasopts, comment=""}
proc means data=sashelp.class (keep = age);
run;
```

And in your document this would produce:

proc means data=sashelp.class (keep = age);
run;
                            The MEANS Procedure

                         Analysis Variable : Age 
 
     N            Mean         Std Dev         Minimum         Maximum
    ------------------------------------------------------------------
    19      13.3157895       1.4926722      11.0000000      16.0000000
    ------------------------------------------------------------------

To Rerun with HTML output.

Switch the "engine" to sashtml.

This example also reuses the previous code chunk by using the same chunk label, "example1", and suppresses the code echo with the "echo=FALSE" chunk option.

```{r example1, engine="sashtml", engine.path=saspath, engine.opts=sasopts, comment="", echo=FALSE}
```
Which produces:
Analysis Variable : Age
N Mean Std Dev Minimum Maximum
19 13.3157895 1.4926722 11.0000000 16.0000000

Run an HTML example with graphics.

If you use the "sashtml" engine, nothing special is required to include SAS ODS graphics.

```{r example2, engine="sashtml", engine.path=saspath, engine.opts=sasopts, comment=""}
proc corr data=sashelp.class nosimple plots=matrix;
run;
```

Producing:

proc corr data=sashelp.class nosimple plots=matrix;
run;
3 Variables: Age Height Weight

Pearson Correlation Coefficients, N = 19
Prob > |r| under H0: Rho=0
Age Height Weight
Age
1.00000
0.81143
<.0001
0.74089
0.0003
Height
0.81143
<.0001
1.00000
0.87779
<.0001
Weight
0.74089
0.0003
0.87779
<.0001
1.00000

Scatter Plot Matrix


Show SAS log files.

We can repeat the first example, showing the SAS log instead of the SAS code by using the saslog engine.

```{r example1, engine="saslog", engine.path=saspath, engine.opts=sasopts, comment=""}
```

Producing:

2          proc means data=sashelp.class (keep = age);
3          run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The PROCEDURE MEANS printed page 1.
NOTE: PROCEDURE MEANS used (Total process time):
      real time           0.14 seconds
      cpu time            0.10 seconds
      
                            The MEANS Procedure

                         Analysis Variable : Age 
 
     N            Mean         Std Dev         Minimum         Maximum
    ------------------------------------------------------------------
    19      13.3157895       1.4926722      11.0000000      16.0000000
    ------------------------------------------------------------------

Repeat with both log and html output.

Finally, you can have both the SAS log and the HTML output with the sashtmllog engine.

```{r example1, engine="sashtmllog", engine.path=saspath, engine.opts=sasopts, comment=""}
```

Producing:

6          proc means data=sashelp.class (keep = age);
7          run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: PROCEDURE MEANS used (Total process time):
      real time           0.04 seconds
      cpu time            0.04 seconds
      
Analysis Variable : Age
N Mean Std Dev Minimum Maximum
19 13.3157895 1.4926722 11.0000000 16.0000000