Using Informats

Ways to Specify Informats

Overview of Specifying Informats

You can specify informats in the following ways:
  • in an INPUT statement
  • with the INPUT, INPUTC, and INPUTN functions
  • in an INFORMAT statement in a DATA step or a PROC step
  • in an ATTRIB statement in a DATA step or a PROC step

INPUT Statement

The INPUT statement with an informat after a variable name is the simplest way to read values into a variable. For example, the following INPUT statement uses two informats:
input @15 style $3. @21 price 5.2;
The $w. character informat reads values into the variable STYLE. The w.d numeric informat reads values into the variable PRICE.
For a complete discussion of the INPUT statement, see INPUT Statement in SAS Statements: Reference.

INPUT Function

The INPUT function converts a SAS character expression using a specified informat. The informat determines whether the resulting value is numeric or character. Thus, the INPUT function is useful for converting data. Here is an example:
TempCharacter='98.6';
TemperatureNumber=input(TempCharacter,4.);
The INPUT function in combination with the w.d informat converts the character value of TempCharacter to a numeric value and assigns the numeric value 98.6 to TemperatureNumber.
Tip
If the first argument of the INPUT function is a character expression and not a variable, the character expression must include quotation marks.
Use the PUT function with a SAS format to convert numeric values to character values. For an example of a numeric-to-character conversion, see PUT Function in SAS Functions and CALL Routines: Reference. For a complete discussion of the INPUT function, see INPUT Function in SAS Functions and CALL Routines: Reference.

INFORMAT Statement

The INFORMAT statement associates an informat with a variable. SAS uses the informat in any subsequent INPUT statement to read values into the variable. For example, in the following statements the INFORMAT statement associates the DATEw. informat with the variables Birthdate and Interview:
informat Birthdate Interview date9.;
input @63 Birthdate Interview;
An informat that is associated with an INFORMAT statement behaves like an informat that you specify with a colon (:) format modifier in an INPUT statement. For details about using the colon (:) modifier, see INPUT Statement, List in SAS Statements: Reference. Therefore, SAS uses a modified list input to read the variable so that
  • the w value in an informat does not determine column positions or input field widths in an external file
  • the blanks that are embedded in input data are treated as delimiters unless you change the DLM= or DLMSTR= option in an INFILE statement
  • for character informats, the w value in an informat specifies the length of character variables
  • for numeric informats, the w value is ignored
  • for numeric informats, the d value in an informat behaves in the usual way for numeric informats.
If you have coded the INPUT statement to use another style of input, such as formatted input or column input, that style of input is not used when you use the INFORMAT statement.
See INPUT Statement, List in SAS Statements: Reference for more information about how to use modified list input to read data.
Note: Anytime a text file originates from anywhere other than the local encoding environment, it might be necessary to specify the ENCODING= option in either ASCII or EBCDIC environments. For example, when you read an EBCDIC text file on an ASCII platform, it is recommended that you specify the ENCODING= option in the FILENAME or INFILE statement. However, if you use the DSD and the DLM= or DLMSTR= options in the FILENAME or INFILE statement, the ENCODING= option is a requirement because these options require certain characters in the session encoding (such as quotation marks, commas, and blanks).The use of encoding-specific informats should be reserved for use with true binary files. That is, they contain both character and non-character fields.

ATTRIB Statement

The ATTRIB statement can also associate an informat, as well as other attributes, with one or more variables. For example, in the following statements, the ATTRIB statement associates the DATEw. informat with the variables Birthdate and Interview:
attrib Birthdate Interview informat=date9.;
input @63 Birthdate Interview;
An informat that is associated by using the INFORMAT= option in the ATTRIB statement behaves like an informat that you specify with a colon (:) format modifier in an INPUT statement. For details about using the colon (:) modifier, see INPUT Statement, List in SAS Statements: Reference.. Therefore, SAS uses a modified list input to read the variable in the same way as it does for the INFORMAT statement.
For more information, see ATTRIB Statement in SAS Statements: Reference.

Permanent versus Temporary Association

When you specify an informat in an INPUT statement, SAS uses the informat to read input data values during that DATA step. SAS, however, does not permanently associate the informat with the variable. To permanently associate an informat with a variable, use an INFORMAT statement or an ATTRIB statement. SAS permanently associates an informat with the variable by modifying the descriptor information in the SAS data set.

User-Defined Informats

In addition to the informats that are supplied with Base SAS software, you can create your own informats. In Base SAS software, PROC FORMAT enables you to create your own informats and formats for both character and numeric variables. For more information about user-defined informats, see FORMAT Procedure in Base SAS Procedures Guide.
When you execute a SAS program that uses user-defined informats, these informats should be available. The two ways to make these informats available are
  • to create permanent, not temporary, informats with PROC FORMAT
  • to store the source code that creates the informats (the PROC FORMAT step) with the SAS program that uses them.
If you execute a program that cannot locate a user-defined informat, the result depends on the setting of the FMTERR= system option. If the user-defined informat is not found, then these system options produce these results:
System Option
Result
FMTERR
SAS produces an error that causes the current DATA or PROC step to stop.
NOFMTERR
SAS continues processing by substituting a default informat.
Although using NOFMTERR enables SAS to process a variable, you lose the information that the user-defined informat supplies. This option can cause a DATA step to misread data, and it can produce incorrect results. For more information, see FMTERR System Option in SAS System Options: Reference.
To avoid problems, make sure that users of your program have access to all the user-defined informats that are used.