SASweave: Literate programming using SAS
by Russell V. Lenth, University of Iowa, USA and
Søren Hjøsgaard, Aarhus University,
SASweave, patterned after Sweave
(Leisch, 2002), is a way to easily and reliably prepare documents
containing SAS code, output, and graphics. You first prepare
source file, which is a LaTeX file with additional environments
containing SAS code. SASweave reads that file, runs the SAS
and creates a .tex
that includes the SAS code and output in verbatim environments, as well
as any graphics that are created. SASweave can also pass the
results directly to pdflatex, creating the formatted document.
For illustration, here is a simple source
file and the resulting PDF
SASweave can also accommodate Sweave source files, which it passes to
function for processing, or source files containing both
SAS and R code. Exactly what to do and what order to do it in
determined by the extension in the name of the source file.
A related operation called tangling is provided by a sastangle utility.
This simply extracts the SAS code from the source file and
it in a .sas
Besides the convenience, another good reason to use SASweave, rather
than manually copying and pasting code and output, is that SASweave
guarantees that the output you see is the actual result of the code
displayed. Using such utilities to document your work is
known as literate
programming (a term coined by Donald Knuth, the original
author of TeX), whereby code, documentation, and output are all
1.2.10 (August 21, 2007)
Version 1.2.8 is also published in Journal of
Statistical Software, Volume 19, Issue 8.
- Linux/Unix systems -- .tgz file
Simply copy the scripts into appropriate directories; see
the README file.
- Windows -- installer
Simply run this installer and answer the questions.
You must also have gawk
installed on your system and in your PATH (see how to below) before installing
SASweave. Gawk is available from http://gnuwin32.sourceforge.net/packages/gawk.htm.
Make sure you either download the complete setup binary
installer, or both the Gawk binary and Gawk dependency installers.
- User Manual -- .pdf file
SAS 9.2 notes
Evidently, graphics layout in SAS 9.2 has changed enough that graphs might get clipped in SASweave. See this e-mail excerpt from Ken Kleinman at Harvard; it suggests a slight modofication of the script in saswsetup.awk that seems to do the trick.
- Ken has also provided an extension with a new 'sgfig' option that supports graphics produced by the new SAS 9.2 procedures sgplot, sgscatter, and sgpanel. To use these, you need to replace some of the scripts in the SASweave installation. They are provided in http://www.stat.uiowa.edu/~rlenth/SASweave/Kleinman-extension.
SASweave uses awk scripts (actually, gawk or nawk are required, because
we need the ability to load more than one awk script at a time).
You copy the scripts to a suitable location, the
shell scripts sasweave
to a location in the PATH, and the file SasWeave.sty to
a location in the TEXINPUTS path. You will need to edit sasweave and sastangle so
that they can find these scripts.
The installer gives you the option of associating SASweave with the
filename extensions .SAStex
and others. Associated files will be displayed in Windows
Explorer with the SASweave icon. Three operations are provided for
The file associations are made in the HKEY_CURRENT_USER secti-- user varaibles and system variables. on of the
registry; that is, the associations will only apply to the user who
installed SASweave. This allows users without administrative
rights to install SASweave.
- Make pdf -
runs SASweave and the pdflatex.
- runs SASweave to make the .tex file, does
not run pdflatex.
-- runs SAStangle to extract the code to a .sas file
How to add Gawk to your Windows PATH
The PATH is a list of directories that Windows uses to search for programs it wants to run. The location of Gawk must be in this PATH
before you can install SASweave. The instructions given here
assume that when you installed Gawk, you specified the installation
directory C:\Program Files\GnuWin32. You need to adapt the instructions if you installed it elsewhere.
- From the Start menu, right-click on "My Computer", and select "Properties" from the menu.
- Click on the "Advanced" tab in the resulting dialog window.
- Click on the button labeled "Environment Variables" near the bottom.
- In the User variables list, see if there is an entry for PATH.
Note that in either case, you add \bin to the installation directory for Gawk. Click "OK" twice more to exit the dialog windows.
- If there is, highlight it and click "Edit"; add ;C:\Program Files\GnuWin32\bin to the end of the list (note that you separate it from the previous entry using a semicolon). Then click "OK".
- If there isn't, click "New". Under "Variable name", enter PATH, and under "Variable value", enter C:\Program Files\GnuWin32\bin. Then click "OK".
The NEWS file summarizes the
change history of this software. Look here if you want an older
version of the software.
Gnu Public License. See the COPYING