9 Saving Intermediate SAS files

You may want to save the intermediate SAS files for a variety of reasons. Perhaps you want to use your examples in a live demonstration. Perhaps you want to make your examples available for download from a website. Or perhaps you are trying to debug some issue with the code in your document, and you are not entirely sure where the problem has entered the document rendering process.

Knitr produces a number of intermediate files on the way to your final document. By default, these are all temporary files. But with the appropriate options, it is possible to save any of them. This is true with all of the SAS engines.

  • Each SAS code chunk is saved as a SAS command file, with the file extension “.sas”.
  • As each SAS code chunk is evaluated, a SAS log file and a SAS output file are produced. The log file has a “.log” file extension. The output file can have a “.lst” file extension (SAS “listing” output), an “.html” file extension (an HTML fragment), or a “.tex” file extension (a LaTeX fragment).
  • When all of the code chunks have been evaluated and the output has been knit into your text, a Markdown document is saved, with file extension “.md”.
  • The Markdown document is then processed by Pandoc, rendering your final document.

9.1 Saving SAS files

Use the saveSAS=TRUE chunk option to save each chunk. You can use this option per chunk, or set it as a global option.

The SAS file names are then taken from each code chunk label.

A typical chunk specification might look like this:

```{sas procmeans, saveSAS=TRUE} 
proc means data=sashelp.class;
run;
```

This will save three intermediate files: procmeans.sas, procmeans.log, and procmeans.lst.

proc means data=sashelp.class;
run;

If we stopped here you could see these in a file explorer. Here we'll show them with some R code.

list.files(pattern="proc")
[1] "procmeans.log" "procmeans.lst" "procmeans.sas"

The raw contents of the SAS files are:

SAS commands:

OPTIONS NONUMBER NODATE PAGESIZE = MAX FORMCHAR = '|----|+|---+=|-/<>*' FORMDLIM=' ';title; 
 proc means data=sashelp.class; 
 run; 

SAS log:

1                             The SAS System 
                                          10:54 Wednesday, November 30, 2022 
  
 NOTE: Copyright (c) 2016 by SAS Institute Inc., Cary, NC, USA.  
 NOTE: SAS (r) Proprietary Software 9.4 (TS1M6)  
       Licensed to UNIVERSITY OF WISCONSIN MADISON -SFA- T&R, Site 70090197. 
 NOTE: This session is executing on the X64_10PRO  platform. 
  
  
  
 NOTE: Analytical products: 
        
       SAS/STAT 15.1 
       SAS/ETS 15.1 
       SAS/OR 15.1 
       SAS/IML 15.1 
       SAS/QC 15.1 
  
 NOTE: Additional host information: 
  
  X64_10PRO WIN 10.0.19041  Workstation 
  
 NOTE: SAS initialization used: 
       real time           1.13 seconds 
       cpu time            0.15 seconds 
        
  
 NOTE: AUTOEXEC processing beginning; file is  
       Z:\R\SASmarkdown_docs\autoexec.sas. 
  
  
 NOTE: There were 19 observations read from the data set SASHELP.CLASS. 
 NOTE: The data set WORK.CLASS has 19 observations and 6 variables. 
 NOTE: DATA statement used (Total process time): 
       real time           0.00 seconds 
       cpu time            0.00 seconds 
        
  
  
 NOTE: AUTOEXEC processing completed. 
  
 1          OPTIONS NONUMBER NODATE PAGESIZE = MAX FORMCHAR = 
 1        ! '|----|+|---+=|-/<>*' FORMDLIM=' ';title; 
 2          proc means data=sashelp.class; 
 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.56 seconds 
       cpu time            0.03 seconds 
        
  
 NOTE: SAS Institute Inc., SAS Campus Drive, Cary, NC USA 27513-2414 
 NOTE: The SAS System used: 
       real time           2.83 seconds 
       cpu time            0.23 seconds 
        

SAS listing:

  
                                                                             
   
                             The MEANS Procedure 
  
  Variable    N           Mean        Std Dev        Minimum        Maximum 
  ------------------------------------------------------------------------- 
  Age        19     13.3157895      1.4926722     11.0000000     16.0000000 
  Height     19     62.3368421      5.1270752     51.3000000     72.0000000 
  Weight     19    100.0263158     22.7739335     50.5000000    150.0000000 
  ------------------------------------------------------------------------- 

9.2 Saving Markdown and LaTeX files

You can also save your intermediate Markdown document (and an intermediate LaTeX document if you are making a PDF) with the appropriate option in your document YAML.

---
output:
  html_document:
    keep_md: yes
  pdf_document:
    keep_md: yes
    keep_tex: true
---