Using Formatted Input

INPUT Statement Syntax

Formatted input is a powerful method for reading both standard and nonstandard data in fixed fields.
Syntax, INPUT statement using formatted input:
INPUT <pointer-control> variable informat.;
  • pointer-control positions the input pointer on a specified column.
  • variable is the name of the variable.
  • informat. is the special instruction that specifies how SAS reads the raw data.

Using the @n Column Pointer Control

The @n is a pointer control that moves the input pointer to a specific column number. The @ moves the pointer to column n, which is the first column of the field that is being read.
Syntax, INPUT statement using formatted input and the @n pointer control:
INPUT @n variable informat.;
  • variable is the name of the variable.
  • informat. is a special instruction that specifies how SAS reads the raw data.
In this example, you use the @n pointer control to locate variable values in the external file Empdata. As you can see, the values for LastName begin in column 1. The example could start with the @1 pointer control.
input @1 LastName $7.
However, the default column pointer location is column 1, so no column pointer control is necessary to read the first field.
input LastName $7.
LastName Beginning in Column 1
The values for FirstName begin in column 9. To point to column 9, use an @ sign and the column number in the INPUT statement:
input LastName $7. @9 FirstName $5.
FirstName Beginning in Column 9

Examples: Reading Columns in Any Order

Column pointer controls can also be useful. For example, you can use the @n to move a pointer forward or backward when reading a record.
In this INPUT statement, the value for FirstName is read first, starting in column 9.
input @9 FirstName $5.
INPUT Statement: Reading FirstName
Read the values for LastName, which begin in the first column. Here you must use the @n pointer control to move the pointer back to column 1.
input @9 FirstName $5. @1 LastName $7.
INPUT Statement: Reading LastName
The rest of the INPUT statement specifies the column locations of the raw data value for JobTitle and Salary.
input @9 FirstName $5. @1 LastName $7. @15 JobTitle 3.
      @19 Salary comma9.;
INPUT Statement: Specifying Column Locations

The +n Pointer Control

The +n pointer control moves the input pointer forward to a column number that is relative to the current position. The + moves the pointer forward n columns.
Syntax, INPUT statement using formatted input and the +n pointer control:
INPUT +n variable informat.;
  • variable is the name of the variable.
  • informat. is a special instruction that specifies how SAS reads the raw data.
In order to count correctly, it is important to understand where the column pointer is located after each data value is read.

Example: How the +n Pointer Control Works

Suppose you want to read the data from Empdata in the following order: LastName, FirstName, Salary, JobTitle. Because the values for LastName begin in column 1, a column pointer control is not needed.
input LastName $7.
Column Pointer Location
With formatted input, the column pointer moves to the first column following the field that was just read. In this example, after LastName is read, the pointer moves to column 8.
Column Pointer Location
To start reading FirstName, which begins in column 9, the column pointer moves ahead one column with +1.
input LastName $7. +1 FirstName $5.
Column Pointer Location
After reading FirstName, the column pointer moves to column 14. To skip over the values for JobTitle and read the values for Salary, which begin in column 19, move the column pointer ahead five columns from column 14.
input LastName $7. +1 FirstName $5. +5 Salary comma9.
Column Pointer Location
The last field to be read contains the values for JobTitle. You can use the @n column pointer control to return to column 15.
input LastName $7. +1 FirstName $5. +5 Salary comma9. 
      @15 JobTitle 3.;
Column Pointer Location
Note: You can use the notation +(-n) to move the +n pointer backward.
Last updated: January 10, 2018
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.7.131