Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 37 ยท Procedures: PROC

PROC FORMAT and Custom Formats

Create custom SAS formats and informats with PROC FORMAT: VALUE ranges, character formats, OTHER, multi-label and CNTLIN tables, LIBRARY= and FMTSEARCH, with a runnable Python stand-in.

A SAS format changes how a value is displayed without changing the stored value. The number 21915 can appear as 01JAN2020, 0.153 as 15.3%, and the code 1 as Male. SAS ships with hundreds of formats (date9., dollar10.2, percent8.1), but PROC FORMAT lets you define your own, which is how you turn codes into words, group ages into bands, and keep one lookup table for a whole project.

SAS is not available on this site. sas blocks are read-only and I do not print SAS output. The TryIts are Python stand-ins that apply the same range logic to small lists.

Defining a value format

SAS
proc format;
  value sexfmt
    1 = "Male"
    2 = "Female"
    other = "Unknown";

  value agegrp
    low -< 18  = "Under 18"
    18  -< 65  = "18 to 64"
    65  - high = "65 and over"
    . = "Missing";

  value $country
    "DE" = "Germany"
    "FR" = "France"
    "US" = "United States"
    other = "Other";
run;

Points to notice:

  • A numeric format name is a plain name; a character format name starts with $.
  • Format names cannot end in a digit and, in a value statement, do not have a trailing period (the period appears only when you use the format).
  • Ranges use - for inclusive, and < next to a boundary to exclude it: 18 -< 65 means 18 up to but not including 65. low and high stand for the extremes (low does not include missing).
  • other catches everything that no range matched, and a lone . handles missing numbers.
  • Overlapping ranges cause an error, so make them tile cleanly.

Applying a format

You attach a format with a format statement or the put() function:

SAS
proc freq data=work.patients;
  tables age sex;
  format age agegrp. sex sexfmt.;
run;

data work.patients2;
  set work.patients;
  age_band = put(age, agegrp.);     /* creates a real character variable */
run;

The format statement in a PROC step is temporary: it affects the report only. In a DATA step it is stored in the data set's header and travels with it. The data itself never changes: age is still a number, but PROC FREQ now groups it into the three bands. That makes formats a quick, powerful way to recode on the fly.

put(value, fmt.) returns the formatted text, so you can store it in a new variable when you do need the band as data.

Stand-in: a format as a range table

Python Stand-in: VALUE agegrp

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The boundary values 18 and 65 land in the upper band, which is exactly what -< produces in SAS. The second print mimics what PROC FREQ does when you apply the format: it counts the labels.

Character formats and lookups

Character formats are lookup tables. They are a clean alternative to long if/else chains:

SAS
data work.orders2;
  set work.orders;
  country_name = put(country_code, $country.);
run;

Unmatched codes fall to other. Without an other entry, SAS prints the raw value, which is useful for spotting codes you forgot.

Informats

An informat works in the opposite direction: it controls how raw text is read into a value. Use invalue to define one:

SAS
proc format;
  invalue gradein
    "A" = 4
    "B" = 3
    "C" = 2
    "D" = 1
    "F" = 0
    other = .;
run;

data work.grades;
  input name $ letter :gradein.;
  datalines;
Ana A
Bo C
Cy F
;
run;

Here the text C is read as the number 2. Built-in informats such as mmddyy10. and comma12. work the same way; the rule is the same: informats go in on input, formats come out on put and format.

Where formats are stored

By default a format lives in work.formats and disappears when the session ends. To keep it, write it to a permanent library:

SAS
libname fmtlib "C:\project\formats";

proc format library=fmtlib;
  value sexfmt 1="Male" 2="Female";
run;

options fmtsearch=(fmtlib work);

fmtsearch tells SAS where to look, in order. If a data set uses a format that SAS cannot find, you get the error Format SEXFMT was not found or could not be loaded. The fixes are to run the proc format step again, add the library to fmtsearch, or set options nofmterr; to let SAS continue with a note (use that only deliberately).

Building formats from a table: CNTLIN

If the lookup is long, or lives in a data set, build the format from data. The input data set must have FMTNAME, START, and LABEL variables (and TYPE: N numeric, C character, I numeric informat):

SAS
data work.cntl;
  retain fmtname "$country" type "C";
  input start $ label $20.;
  datalines;
DE Germany
FR France
US United States
;
run;

proc format cntlin=work.cntl;
run;

The opposite direction, proc format library=work cntlout=work.fmtdump;, writes the contents of existing formats into a data set so you can inspect them. Keep the lookup in one data set, and the same list can drive both your formats and your documentation.

Stand-in: lookup from a table

Python Stand-in: build a lookup, apply with a default

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Multi-label and picture formats

Two advanced kinds exist. A multilabel format (value x (multilabel)) lets one value fall into several ranges, which PROC MEANS and PROC TABULATE can report with the mlf option. A picture format (picture phone low-high = "999-9999") builds text from digits using a template, for instance to show 5551234 as 555-1234. Start with plain value formats; they cover most needs.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does applying a format to a variable change?

    A format changes the display; the underlying stored value is unchanged.
  2. 2. What does the range 18 -< 65 mean?

    The < next to the boundary excludes it, so 65 belongs to the next range.
  3. 3. How must a character format name start?

    Character format names begin with a dollar sign, for example $country.
  4. 4. Which option tells SAS where to search for permanent formats?

    FMTSEARCH lists the libraries, in order, that SAS searches for formats.
  5. 5. What is an informat used for?

    Informats are applied while reading input; formats are applied while writing or displaying.

Summary

  • PROC FORMAT with value creates display formats; character formats begin with $.
  • Ranges use -, -<, low, high, other and .; they must not overlap.
  • Use format for display, put() to create stored text, and invalue for informats.
  • Keep formats in a permanent library and list it in fmtsearch.
  • cntlin= builds formats from a data set; cntlout= exports them.

Next: PROC TRANSPOSE and Reshaping Data