SASweave: Literate programming using SAS
by Russell V. Lenth, University of Iowa, USA and
Søren Hjøsgaard, Aarhus University,
Denmark
Overview
SASweave, patterned after Sweave
(Leisch, 2002), is a way to easily and reliably prepare documents
containing SAS code, output, and graphics. You first prepare
a .SAStex
source file, which is a LaTeX file with additional environments
containing SAS code. SASweave reads that file, runs the SAS
code,
and creates a .tex
file
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
file.
SASweave can also accommodate Sweave source files, which it passes to
R's Sweave
function for processing, or source files containing both
SAS and R code. Exactly what to do and what order to do it in
is
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
saves
it in a .sas
file.
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
packaged together.
Downloading SASweave
Current version:
1.2.10 (August 21, 2007)
- Linux/Unix systems -- .tgz file
Simply copy the scripts into appropriate directories; see
the README file.
- Windows -- installer
file
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
Version 1.2.8 is also published in Journal of
Statistical Software, Volume 19, Issue 8.
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.
Linux/Unix notes
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
associated
shell scripts sasweave
and sastangle
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.
Windows notes
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
associated files:
- Make pdf -
runs SASweave and the pdflatex.
- Weave
- runs SASweave to make the .tex file, does
not run pdflatex.
- Tangle
-- runs SAStangle to extract the code to a .sas file
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.
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.
- 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".
Note that in either case, you add \bin to the installation directory for Gawk. Click "OK" twice more to exit the dialog windows.
Update information
The NEWS file summarizes the
change history of this software. Look here if you want an older
version of the software.
License
Gnu Public License. See the COPYING
for details.