SAS Codebook


Codebooks can be generated using a simple call to a SAS macro.

%*--- example codebook macro call ---;

Use on Rho Network

Because R and MikTeX programs and packages have already been installed to the Rho network, creating a codebook at Rho is straightforward. The SAS macro lives at


Simply %include the macro from this location and you're off to the races. See the Sample Code section below for two typical uses of the codebook macro.

Using the codebook macro outside of the Rho network requires a little more work. Instructions for use of the macro outside of the Rho network are provided below.

Software Requirements

The SAS macro utilizes R and MikTeX (a relation of LaTeX) to produce the output in PDF format. In order to use the codebook macro you must have all 3 pieces of software installed on your computer. We assume you already have SAS installed on your machine. To get R and MikTeX on your machine:

  1. Download the SAS codebook macro - Note the location in which you save the SAS file.
  2. Download and install R from - The R install is relatively straightforward.
  3. Download the zip file R functions - Unzip this file and note the location in which you save the folder of functions.
  4. Install the following R packages: Formula, Hmisc, ggplot2 - Package installation is straightforward. Launch the R application. To install the Formula package type > install.packages("Formula"). And similarly for the other packages.
  5. Download and install MikTeX from - There are several potential gotchas with the MikTeX install.
    1. Unlike most software, MikTeX does not automatically install in your Program Files directory. You will need to explicitely tell the installer where to install the program. Try to avoid locations that include spaces in the file path.
    2. Change the default paper size from A4 to Letter (yes, that was a slight US bias that you detected there).
    3. Change the default update mode from "Ask me first" to "on the fly".

Additional Setup Requirements

In the macro, modify the default values for the parameters PDFLATEX, REXE, RSOURCE, SWEAVE, and RLIBRARY to fit your local setup. Your paths will vary depending on exactly where you decide to save/install the programs and folders downloaded in the previous steps. Example file paths from a recent local install

  1. pdflatex=%str(C:\Users\Public\MikTeX\miktex\bin\pdflatex)
  2. rexe=%str(C:\Users\Public\R\R-3.2.1\bin\r.exe)
  3. rlibrary=%str(C:\Users\Public\R\R-3.2.1\library)
  4. rsource=%str(C:\Users\Public\R\R_functions)
  5. sweave=%str(C:\Users\Public\R\R-3.2.1\share\texmf\tex\latex)

Sample Code

Having performed the above setup steps, you are now ready to use the SAS macro. The simplest call to the macro requires only the name of a dataset (e.g., FILE=derive.adsl). It is also helpful, though not required, to specify the libname associated with the format catalog for the study (e.g., FORMATS=library).

It is also possible to generate multiple codebooks at once. This can be done in one of two ways.

  1. Specify multiple datasets in a space-separated list (e.g., FILE=derive.adsl source.prohib).
  2. Instead of specifying a list of datasets, specify a libref (e.g., LIBREF=derive).

Various other optional parameters exist. The interested reader can find additional information in the macro header block.

*---------- define dataset and format libnames ----------;

libname derive  "_some_path_" access=readonly;
libname library "_some_path_" access=readonly;

*---------- macro invocation ----------;

%include "_some_path_\";1

*---------- generate a code book for one dataset ----------;

*---------- generate code books for an entire library ----------;

1 See above for macro download links.

2 The macro creates pdf file ADSL.pdf. By default the pdf is saved in the same directory as the calling program. Use optional parameter PDFLOC to specify an alternate saving location.

3 The macro creates one pdf file corresponding to every dataset in the DERIVE library. The pdf files are named after the datasets (e.g., dataset ADCM will generate ADCM.pdf).