Author: Doug Hemken

# Basic Use of SASmarkdown

#### Doug Hemken

#### Aug 2022

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

SASmarkdown defines six knitr engines, to return different combinations of SAS code, SAS listing output, SAS HTML output, and SAS log output for use in your document.

SASmarkdown also defines several knitr chunk (code block) options for working with SAS output.

## Set up your SAS engine configuration.

The details depend on your operating system, the version of SAS, and whether or not you have SAS installed in a default location.

    ```{r setup, message=FALSE}
    library(SASmarkdown)
    ```

This defines six knitr engines

- sas - SAS code and listing output
- saslog - SAS log and listing output
- sashtml - code and HTML output
- sashtmllog - log and HTML output
- sashtml5 - code and HTML5 output
- sashtml5log - log and HTML5 output

These are ordinarily used in pairs, to give you access to the code, log, and output format you prefer.

If SAS is not found, you will have to define the engine path and any engine options in your document. For example

    ```{r enginepath}
    saspath <- "C:/Program Files/SASHome/SASFoundation/9.4/sas.exe"
    sasopts <- "-nosplash -ls 75" # '-nosplash' fails in some unix terminals
    knitr::opts_chunk$set(engine.path=list(sas=saspath, saslog=saspath),
                      engine.opts=list(sas=sasopts, saslog=sasopts),
                      comment=NA)
    ```

## Set up SAS code chunks.

A simple code chunk in your document might look like:

    ```{sas example1}
    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
        ------------------------------------------------------------------

## HTML output.

HTML gives you a method of embedding SAS tables and graphs directly in your document.

Switch the engine specification to sashtml. This example suppresses the code echo with the `echo=FALSE` chunk option.

    ```{sashtml example2, echo=FALSE}
    proc means data=sashelp.class (keep = age);
    run;
    ```

Which produces:

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

## HTML with graphics.

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

    ```{sashtml example3}
    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

## Show SAS log files.

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

    ```{saslog example4}
    ```

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.08 seconds
          cpu time            0.07 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.

    ```{sashtmllog example1}
    ```

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.05 seconds
          cpu time            0.03 seconds

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