PaulDickman.com

SAS permanent format catalogues

Temporary vs permanent SAS data sets

Permanent SAS data sets can be created by specifying a two-level dataset name.

The following code creates the temporary SAS data set called PAUL (or WORK.PAUL):
data paul
...
run;

The following code creates the permanent SAS data set called PROJECT.PAUL:
data project.paul
...
run;

Permanent format catalogues

Formats created using PROC FORMAT are stored in format catalogues. By default, the format catalogue is stored in the work directory and deleted at the end of the SAS session (along with temporary data sets).

Permanent format catalogues are created using the library= option in PROC FORMAT.

The following code creates the a permanent format catalogue in the library PROJECT:
proc format library=project;
value smoke
.='missing'
1='current smoker'
2='former smoker'
3='never smoked'
;
run;

But how do we tell SAS where to look for the format?

The FMTSEARCH= system option

This option was introduced in version 6.08?, so is not documented in the SAS Language manual.

The online help provides the following information:

FMTSEARCH = (libref-1 libref-2... libref-n)

The FMTSEARCH= system option controls the order in which format catalogs are searched. If the FMTSEARCH= option is specified, format catalogs are searched in the order listed, until the desired member is found.

The WORK.FORMATS catalog is always searched first, unless it appears in the FMTSEARCH list. The LIBRARY.FORMATS catalog is searched after WORK.FORMATS and before anything else in the FMTSEARCH list, unless it appears in the FMTSEARCH list.

example usage: options fmtsearch=(book finsurv cancer);

SAS will search for formats in the following catalogues (in order): WORK, LIBRARY, BOOK, FINSURV, CANCER.

Suggested usage

  1. Define libnames and the FMTSEARCH= option in autoexec.sas.
  2. Create a SAS file called FORMATS.SAS which contains the PROC FORMAT code.


Sample code to be included in AUTOEXEC.SAS:
libname project 'c:\project';
options fmtsearch=(project);

Sample code to be included in FORMATS.SAS:
/* delete the existing catalogue */
proc datasets library=project memtype=(catalog);
delete formats / memtype=catalog;
run;

proc format library=project;
value smoke
.='missing'
1='current smoker'
2='former smoker'
3='never smoked'
;
run;

The code to delete the existing catalogue is optional.

The guidelines being developed by the analysis group for documenting and storing data files on the server will require that formats be specified in this manner in a file called FORMATS.SAS.

Sample SAS code (one.sas)

libname project 'c:\temp';

options fmtsearch=(project);

proc format library=project;
value smoke
.='missing'
1='current smoker'
2='former smoker'
3='never smoked'
;
run;

data project.paul;
do i=1 to 300;
smoke=mod(floor(100*ranuni(-1)),3)+1;
expose=ranuni(-1)<0.5;
disease=ranuni(-1)<0.2;
age=ranbin(-5,150,0.3);
output;
end;
drop i;
label
smoke='Smoking status'
expose='Exposure status'
disease='Disease status'
age='Age in years'
;
run;

proc freq;
tables smoke;
format smoke smoke.;
run;