Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 37 ยท Reading and Writing Data

SAS Informats and Formats: Reading and Displaying Values

Understand the difference between informats (reading text into values) and formats (displaying values), the common ones such as date9. and dollar10.2, and how to define your own with PROC FORMAT.

SAS separates what a value is from how it is shown. An informat tells SAS how to read raw text into a stored value, and a format tells SAS how to display a stored value. Mixing them up is a classic beginner stumble, so this lesson gives a simple rule and then covers the built-in and custom ones.

The one-sentence rule

Informats go in (when reading); formats come out (when displaying).

For example, the text 15MAR2024 is read with the informat date9. and becomes the number 23450 (days since 1960). To show it as a date again you apply the format date9.. The stored number never changes; only the appearance does.

Anatomy of a format name

A format or informat name has the form name + optional width + . + optional decimals:

TEXT
$char10.   character, width 10
8.2        numeric, width 8 with 2 decimals
comma12.2  commas, width 12, 2 decimals
date9.     date like 15MAR2024

The period is required. 8 is a number and 8. is a format. Character formats begin with $.

Common formats and informats

Name Used for Example text
w.d Plain numbers 1234.5
comma w.d Numbers with commas 1,234.50
dollar w.d Currency $1,234.50
percent w.d Percentages (0.25 shown as 25%) 25%
date9. Date, DDMMMYYYY 15MAR2024
mmddyy10. US date 03/15/2024
ddmmyy10. European date 15/03/2024
yymmdd10. ISO date 2024-03-15
worddate. Long text date March 15, 2024
time8. Clock time 13:45:00
$upcase. Convert to uppercase ANA
$w. Character of width w

Informats read with the same names (comma12., mmddyy10., yymmdd10.), and there are special informats that exist only for reading. For example anydtdte. guesses many date layouts, though a specific informat is safer because ambiguous strings such as 03/04/2024 could be March 4 or April 3.

Using informats in INPUT

SAS
data orders;
   input id order_date :mmddyy10. price :dollar8.2;
   format order_date date9. price dollar10.2;
   datalines;
1 03/15/2024 $1,250.00
2 04/01/2024 $980.50
;
run;

:mmddyy10. reads the text. format below changes only the display. If you forget the format statement, PROC PRINT would show 23450 for the first date.

You can also convert text that is already in a data set with the input function and an informat, as seen in Functions and Dates, and convert numbers to text with the put function and a format:

SAS
text_date = put(order_date, yymmdd10.);   /* "2024-03-15" */

Attaching formats in procedures

A format statement works inside DATA steps and also inside many PROC steps, where it applies only for that step:

SAS
proc print data=orders;
   format price dollar12.2 order_date worddate.;
run;

A format given in a DATA step is stored with the data set, so later procedures use it automatically.

Custom formats with PROC FORMAT

PROC FORMAT creates your own formats, ideal for coded values and for grouping numbers into ranges:

SAS
proc format;
   value $sexfmt  "F" = "Female"
                  "M" = "Male"
                  other = "Unknown";
   value agegrp   low - 11 = "Under 12"
                  12 - 14  = "12 to 14"
                  15 - high = "15 and over";
run;

data labelled;
   set sashelp.class;
   format sex $sexfmt. age agegrp.;
run;

A character format name starts with $. In the value statement, low and high stand for the extremes and other catches everything not listed. Ranges are inclusive at both ends by default; use < to exclude an endpoint (12 -< 15).

Because the stored age is untouched, you can still calculate with it, and procedures that group (for example proc freq or proc means with class) will group by the formatted value: you get a table with the three age bands without creating a new variable. That is a major reason SAS programmers love formats.

A format created by PROC FORMAT lives in the WORK library by default and disappears at the end of the session. To keep it, write it to a permanent library with proc format library=proj; and tell later sessions where to look with options fmtsearch=(proj work);.

Why a missing format shows up as an error

If a data set uses a custom format that SAS cannot find, you get ERROR: The format AGEGRP was not found or could not be loaded. Run the PROC FORMAT step first or fix fmtsearch. Adding options nofmterr; lets SAS fall back to plain values, but it is better to find the format.

A formatter model

This Python stand-in (not SAS) shows a numeric value with its stored form and several displays, plus a range-lookup table like PROC FORMAT.

Python Python stand-in: stored value versus display

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.

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which statement is true?

    Informats are for input, formats are for output. A format never changes the stored value.
  2. 2. Why do format names end with a period?

    The period is required; without it SAS reads the token as something else.
  3. 3. In PROC FORMAT, what does 'other' do?

    other is the catch-all for values not matched by the listed values or ranges.
  4. 4. How do you display a date value as 2024-03-15?

    A format statement controls how the stored number is displayed.

Summary

  • Informats read text into stored values; formats display stored values. The stored value is unchanged.
  • Names look like $char10., 8.2, comma12., date9., mmddyy10.; the period is required.
  • Use input() and put() for conversion inside the DATA step.
  • PROC FORMAT builds custom lookups and range groupings; keep them in a permanent library and point to it with fmtsearch.

Next: INFILE and Importing Files with PROC IMPORT