INPUT Statement, Formatted

Reads input values with specified informats and assigns them to the corresponding SAS variables.
Valid in: DATA step
Category: File-handling
Type: Executable

Syntax

Arguments

pointer-control
moves the input pointer to a specified line or column in the input buffer.
variable
specifies a variable that is assigned input values.
Requirement:The (variable-list) is followed by an (informat-list).
(variable-list)
specifies a list of variables that are assigned input values.
informat.
specifies a SAS informat to use to read the variable values.
Tip:Decimal points in the actual input values override decimal specifications in a numeric informat.
(informat-list)
specifies a list of informats to use to read the values for the preceding list of variables
In the INPUT statement, (informat-list) can include
informat.
specifies an informat to use to read the variable values.
pointer-control
specifies one of these pointer controls to use to position a value: @, #, /, or +.
n*
specifies to repeat n times the next informat in an informat list.
Example:This statement uses the 7.2 informat to read GRADES1, GRADES2, and GRADES3 and the 5.2 informat to read GRADES4 and GRADES5:
input (grades1-grades5)(3*7.2, 2*5.2);
Restriction:The (informat-list) must follow the (variable-list).
@
holds an input record for the execution of the next INPUT statement within the same iteration of the DATA step. This line-hold specifier is called trailing @.
Restriction:The trailing @ must be the last item in the INPUT statement.
Tip:The trailing @ prevents the next INPUT statement from automatically releasing the current input record and reading the next record into the input buffer. It is useful when you need to read from a record multiple times.
@@
holds an input record for the execution of the next INPUT statement across iterations of the DATA step. This line-hold specifier is called double trailing @.
Restriction:The double trailing @ must be the last item in the INPUT statement.
Tip:The double trailing @ is useful when each input line contains values for several observations.

Details

When to Use Formatted Input

With formatted input, an informat follows a variable name and defines how SAS reads the values of this variable. An informat gives the data type and the field width of an input value. Informats also read data that are stored in nonstandard form, such as packed decimal, or numbers that contain special characters such as commas.(footnote1) See Definition of Informats in SAS Formats and Informats: Reference for descriptions of SAS informats.
Simple formatted input requires that the variables be in the same order as their corresponding values in the input data. You can use pointer controls to read variables in any order. For more information, see the INPUT Statement.

Missing Values

Generally, SAS represents missing values in formatted input with a single period for a numeric value and with blanks for a character value. The informat that you use with formatted input determines how SAS interprets a blank. For example, $CHAR.w reads the blanks as part of the value, whereas BZ.w converts a blank to zero.

Reading Variable-Length Records

By default, SAS uses the FLOWOVER option to read varying-length data records. If the record contains fewer values than expected, the INPUT statement reads the values from the next data record. To read varying-length data. you might need to use the TRUNCOVER option in the INFILE statement. For more information, see Reading Past the End of a Line.

How to Group Variables and Informats

When the input values are arranged in a pattern, you can group the informat list. A grouped informat list consists of two lists:
  • the names of the variables to read enclosed in parentheses
  • the corresponding informats separated by either blanks or commas and enclosed in parentheses.
Informat lists can make an INPUT statement shorter because the informat list is recycled until all variables are read and the numbered variable names can be used in abbreviated form. Using informat lists avoids listing the individual variables.
For example, if the values for the five variables SCORE1 through SCORE5 are stored as four columns per value without intervening blanks, this INPUT statement reads the values:
input (score1-score5) (4. 4. 4. 4. 4.);
However, if you specify more variables than informats, the INPUT statement reuses the informat list to read the remaining variables. A shorter version of the previous statement is
input (score1-score5) (4.);
You can use as many informat lists as necessary in an INPUT statement, but do not nest the informat lists. After all the values in the variable list are read, the INPUT statement ignores any directions that remain in the informat list. For an example, see Including More Informat Specifications than Necessary.
The n* modifier in an informat list specifies to repeat the next informat n times. Here is an example.
input (name score1-score5) ($10. 5*4.);

How to Store Informats

The informats that you specify in the INPUT statement are not stored with the SAS data set. Informats that you specify with the INFORMAT or ATTRIB statement are permanently stored. Therefore, you can read a data value with a permanently stored informat in a later DATA step without having to specify the informat or use PROC FSEDIT to enter data in the correct format.

Comparisons

When a variable is read with formatted input, the pointer movement is similar to the pointer movement of column input. The pointer moves the length that the informat specifies and stops at the next column. To read data with informats that are not aligned in columns, use modified list input. Using modified list input enables you to take advantage of the scanning feature in list input. See When to Use List Input.

Examples

Example 1: Formatted Input with Pointer Controls

This INPUT statement uses informats and pointer controls:
data sales;
   infile file-specification;
   input item $10. +5 jan comma5. +5 feb comma5. 
      +5 mar comma5.;
run;
It can read these input data records:
----+----1----+----2----+----3----+----4
trucks         1,382     2,789     3,556
vans           1,265     2,543     3,987
sedans         2,391     3,011     3,658
The value for ITEM is read from the first 10 columns in a record. The pointer stops in column 11. The trailing blanks are discarded and the value of ITEM is written to the program data vector. Next, the pointer moves five columns to the right before the INPUT statement uses the COMMA5. informat to read the value of JAN. This informat uses five as the field width to read numeric values that contain a comma. Once again, the pointer moves five columns to the right before the INPUT statement uses the COMMA5. informat to read the values of FEB and MAR.

Example 2: Using Informat Lists

This INPUT statement uses the character informat $10. to read the values of the variable NAME and uses the numeric informat 4. to read the values of the five variables SCORE1 through SCORE5:
data scores;
   input (name score1-score5) ($10. 5*4.);
   datalines;
Whittaker 121 114 137 156 142
Smythe    111 97  122 143 127
;

Example 3: Including More Informat Specifications than Necessary

This informat list includes more specifications than are necessary when the INPUT statement executes:
data test;
   input (x y z) (2.,+1);
   datalines;
2 24 36
0 20 30
   ;
The INPUT statement reads the value of X with the 2. informat. Then, the +1 column pointer control moves the pointer forward one column. Next, the value of Y is read with the 2. informat. Again, the +1 column pointer moves the pointer forward one column. Then, the value of Z is read with the 2. informat. For the third iteration, the INPUT statement ignores the +1 pointer control.

See Also

FOOTNOTE 1:See SAS Language Reference: Concepts for information about standard and nonstandard data values.[return]