3 Linking Code Blocks
3.1 Introduction
By default, the SAS code blocks ("chunks") in an R Markdown document are each run as a separate batch file - they are not connected to each other. However, often we want to write about a sequence of connected steps, interspersing code, results, and explanatory text.
We could simply repeat the necessary code in each code chunk, perhaps
hiding part of the code with a selective echo
chunk option (a
useful technique to know about).
However, it is even simpler
to automate this code repetition by using the collectcode
chunk
option. We simply need to mark those code
chunks we need re-executed in subsequent chunks.
3.3 Example: Linking Code Blocks
Suppose your first code block is a DATA step, explaining how to
calculate a new variable. Specify the collectcode
chunk option
to have this DATA step "carry over" in (all of) the following
code blocks.
This is written as:
```{sas, collectcode=TRUE}
data class;
set sashelp.class;
bmi = 703*weight/height**2;
run;
```
In your document, the result is:
data class;
set sashelp.class;
bmi = 703*weight/height**2;
run;
Next, we have a separate code block that uses the data. This is just a plain SAS code block.
```{sas}
proc means data=class;
var bmi;
run;
```
In your document this is:
proc means data=class;
var bmi;
run;
The MEANS Procedure
Analysis Variable : bmi
N Mean Std Dev Minimum Maximum
------------------------------------------------------------------
19 17.8632519 2.0926193 13.4900007 21.4296601
------------------------------------------------------------------
Written using
- SASmarkdown version 0.8.0.
- knitr version 1.40.
- R version 4.2.2 (2022-10-31 ucrt).