Author: Doug Hemken

# Linking SAS Code Chunks

#### Doug Hemken

#### Aug 2022

Linking SAS code chunks makes use of SAS's autoexec.sas file. When you attach the SASmarkdown library, a chunk option is set up to place chunks in an autoexc file.

## Configure the SAS engine

    library(SASmarkdown)

## Set up to carry over

To have the result of one code chunk available to use in a later code chunk, set the chunk option `collectcode=TRUE`.

In this example, a data set is copied to the WORK library and only one variable is kept.

In Rmarkdown this would look like:

    ```{sas datastep, collectcode=TRUE}
    data class;
        set sashelp.class;
        keep age;
        run;
    ```

And in your final document it would appear as:

    data class;
        set sashelp.class;
        keep age;
        run;

## Using the previous code chunk

Without collectcode to link the code chunks, a later chunk that referenced the data in the WORK library would produce an error, but this now works. (No special option is needed for this later step.)

    proc means data=class;
    run;

                                The MEANS Procedure

                             Analysis Variable : Age

         N            Mean         Std Dev         Minimum         Maximum
        ------------------------------------------------------------------
        19      13.3157895       1.4926722      11.0000000      16.0000000
        ------------------------------------------------------------------