INPUT Statement
Describes the arrangement of values in the input data record and assigns input values to the corresponding SAS variables.
| Valid in: | DATA step |
| Category: | File-Handling |
| Type: | Executable |
| Restriction: | This statement is not valid on the CAS server. |
Syntax
Without Arguments
-
brings an input data record into the input buffer without creating any SAS variables
-
releases an input data record that is held by a trailing @ or a double trailing @.
Arguments
specification(s)
can include
variable
names a variable that is assigned input values.
(variable-list)
specifies a list of variables that are assigned input values.
| Requirement | The (variable-list) is followed by an (informat-list). |
| See | How to Group Variables and Informats |
$
specifies to store the variable value as a character value rather than as a numeric value.
| Tip | If the variable is previously defined as character, $ is not required. |
| Example | Using Multiple Styles of Input in One INPUT Statement |
pointer-control
moves the input pointer to a specified line or column in the input buffer.
| See | Column Pointer Controls and Line Pointer Controls |
column-specifications
specifies the columns of the input record that contain the value to read.
| Tip | Informats are ignored. Only standard character and numeric data can be read correctly with this method. |
| See | Column Input |
| Example | Using Multiple Styles of Input in One INPUT Statement |
format-modifier
allows modified list input or controls the amount of information that is reported in the SAS log when an error in an input value occurs.
| Tip | Use modified list input to read data that cannot be read with simple list input. |
| See | When to Use List Input and Format Modifiers for Error Reporting |
| Example | Positioning the Pointer with a Character Variable |
informat.
specifies an informat to use to read the variable value.
| Tip | You can use modified list input to read data with informats. Modified list input is useful when the data requires informats but cannot be read with formatted input because the values are not aligned in columns. |
| See | Formatted Input and List Input |
| Example | Using Informat Lists |
(informat-list)
specifies a list of informats to use to read the values for the preceding list of variables.
| Restriction | The (informat-list) must follow the (variable-list). |
| See | How to Group Variables and Informats |
@
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. |
| See | Using Line-Hold Specifiers |
| Example | Holding a Record in the Input Buffer |
@@
holds the 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, or when a record needs to be reread on the next iteration of the DATA step. |
| See | Using Line-Hold Specifiers |
| Example | Holding a Record across Iterations of the DATA Step |
Column Pointer Controls
@n
moves the pointer to column n.
| Range | a positive integer |
| Tip | If n is not an integer, SAS truncates the decimal value and uses only the integer value. If n is zero or negative, the pointer moves to column 1. |
| Example | @15 moves the pointer to column 15:input @15 name $10.; |
| Example | Moving the Pointer Backward |
@numeric-variable
moves the pointer to the column given by the value of numeric-variable.
| Range | a positive integer |
| Tip | If numeric-variable is not an integer, SAS truncates the decimal value and uses only the integer value. If numeric-variable is zero or negative, the pointer moves to column 1. |
| Example | The value of the variable A moves the pointer to column
15:a=15; input @a name $10.; |
| Example | Positioning the Pointer with a Numeric Variable |
@(expression)
moves the pointer to the column that is given by the value of expression.
| Restriction | Expression must result in a positive integer. |
| Tip | If the value of expression is not an integer, SAS truncates the decimal value and uses only the integer value. If it is zero or negative, the pointer moves to column 1. |
| Example | The result of the expression moves the pointer to
column 15:b=5; input @(b*3) name $10.; |
@'character-string'
locates the specified series of characters in the input record and moves the pointer to the first column after character-string.
@character-variable
locates the series of characters in the input record that is given by the value of character-variable and moves the pointer to the first column after that series of characters.
| Example | The following statement reads in the WEEKDAY character
variable. The second @1 moves the pointer to the beginning of the
input line. The value for SALES is read from the next non-blank column
after the value of WEEKDAY: input @1 day 1. @5 weekday $10. @1 @weekday sales 8.2; |
| Example | Positioning the Pointer with a Character Variable |
@(character-expression)
locates the series of characters in the input record that is given by the value of character-expression and moves the pointer to the first column after the series.
| Example | Positioning the Pointer with a Character Variable |
+n
moves the pointer n columns.
| Range | a positive integer or zero |
| Tip | If n is not an integer, SAS truncates the decimal value and uses only the integer value. If the value is greater than the length of the input buffer, the pointer moves to column 1 of the next record. |
| Example | This statement moves the pointer to column 23, reads
a value for LENGTH from columns 23 through 26, advances the pointer
five columns, and reads a value for WIDTH from columns 32 through
35:input @23 length 4. +5 width 4.; |
| Example | Moving the Pointer Backward |
+numeric-variable
moves the pointer the number of columns that is given by the value of numeric-variable.
| Range | a positive or negative integer or zero |
| Tip | If numeric-variable is not an integer, SAS truncates the decimal value and uses only the integer value. If numeric-variable is negative, the pointer moves backward. If the current column position becomes less than 1, the pointer moves to column 1. If the value is zero, the pointer does not move. If the value is greater than the length of the input buffer, the pointer moves to column 1 of the next record. |
| Example | Moving the Pointer Backward |
+(expression)
moves the pointer the number of columns given by expression.
| Range | expression must result in a positive or negative integer or zero. |
| Tip | If expression is not an integer, SAS truncates the decimal value and uses only the integer value. If expression is negative, the pointer moves backward. If the current column position becomes less than 1, the pointer moves to column 1. If the value is zero, the pointer does not move. If the value is greater than the length of the input buffer, the pointer moves to column 1 of the next record. |
Line Pointer Controls
#n
moves the pointer to record n.
| Range | a positive integer |
| Interaction | The N= option in the INFILE statement can affect the number of records the INPUT statement reads and the placement of the input pointer after each iteration of the DATA step. See the option N=. |
| Example | The #2 moves the pointer to the second record to read
the value for ID from columns 3 and 4:input name $10. #2 id 3-4; |
#numeric-variable
moves the pointer to the record that is given by the value of numeric-variable.
| Range | a positive integer |
| Tip | If the value of numeric-variable is not an integer, SAS truncates the decimal value and uses only the integer value. |
#(expression)
moves the pointer to the record that is given by the value of expression.
| Range | expression must result in a positive integer. |
| Tip | If the value of expression is not an integer, SAS truncates the decimal value and uses only the integer value. |
/
advances the pointer to column 1 of the next input record.
| Example | The values for NAME and AGE are read from the first
input record before the pointer moves to the second record to read
the value of ID from columns 3 and 4:input name age / id 3-4; |
Format Modifiers for Error Reporting
?
suppresses printing the invalid data note when SAS encounters invalid data values.
| See | How Invalid Data Is Handled |
??
suppresses printing the messages and the input lines when SAS encounters invalid data values. The automatic variable _ERROR_ is not set to 1 for the invalid observation.
| See | How Invalid Data Is Handled |
Details
When to Use INPUT
Input Styles
Overview of Input Styles
-
column
-
list (simple and modified)
-
formatted
-
named.
Column Input
input name $ 1-8 age 11-12;
----+----1----+----2----+ Peterson 21 Morgan 17
List Input
input name $ age;
----+----1----+----2----+ Peterson 21 Morgan 17
Formatted Input
input name $char8. +2 income comma6.;
----+----1----+----2----+ Peterson 21,000 Morgan 17,132
Named Input
input name= $ age=;
----+----1----+----2----+ name=Peterson age=21 name=Morgan age=17
Multiple Styles in a Single INPUT Statement
input idno name $18. team $ 25-30 startwght endwght;
----+----1----+----2----+----3----+---- 023 David Shaw red 189 165 049 Amelia Serrano yellow 189 165
Pointer Controls
Overview of Pointers
|
Pointer Controls
|
Relative
|
Absolute
|
|---|---|---|
|
column pointer controls
|
+n
|
@n
|
|
+numeric-variable
|
@numeric-variable
| |
|
+(expression)
|
@(expression)
| |
|
@'character-string'
| ||
|
@character-variable
| ||
|
@(character-expression)
| ||
|
line pointer controls
|
/
|
#n
|
|
#numeric-variable
| ||
|
#(expression)
| ||
|
line-hold specifiers
|
@
|
(not applicable)
|
|
@@
|
(not applicable)
|
Using Column and Line Pointer Controls
Using Line-Hold Specifiers
-
a data record is read by more than one INPUT statement (trailing @)
-
one input line has values for more than one observation (double trailing @)
-
a record needs to be reread on the next iteration of the DATA step (double trailing @).
-
The pointer position does not change.
-
No new record is read into the input buffer.
-
The next INPUT statement for the same iteration of the DATA step continues to read the same record rather than a new one.
-
a null INPUT statement executes:
input;
-
an INPUT statement without a trailing @ executes
-
the next iteration of the DATA step begins.
-
immediately if the pointer moves past the end of the input record
-
immediately if a null INPUT statement executes:
input;
-
when the next iteration of the DATA step begins if an INPUT statement with a single trailing @ executes later in the DATA step:
input @;
Pointer Location After Reading
----+----1----+----2----+----3 REGION1 49670 REGION2 97540 REGION3 86342
input region $ jansales;
----+----1----+----2----+----3
REGION1 49670
↑-
column input
input region $ 1-7 jansales 12-16;
-
formatted input
input region $7. +4 jansales 5.; input region $7. @12 jansales 5.;
----+----1----+----2----+----3
REGION1 49670
↑Reading More Than One Record per Observation
Using the # Pointer Control
input @31 age 3. #3 id 3-4 #2 @6 name $20.;
input name $ 1-10 #2 age 13-14 #4;
input a / b #1 @52 c #2;
Reading Past the End of a Line
NOTE: SAS went to a new line when INPUT statement
reached past the end of a line.Positioning the Pointer before the Record
data test; input a @(a-3) b; datalines; 2 ;
How Invalid Data Is Handled
-
sets the value of the variable that is being read to missing or the value that is specified with the INVALIDDATA= system option. For more information see the INVALIDDATA= System Option in SAS Viya System Options: Reference.
-
prints an invalid data note in the SAS log.
-
prints the input line and column number that contains the invalid value in the SAS log. Unprintable characters appear in hexadecimal. To help determine column numbers, SAS prints a rule line above the input line.
-
sets the automatic variable _ERROR_ to 1 for the current observation.
-
input x ?? 10-12;
-
input x ? 10-12; _error_=0;
End-of-File
Arrays
INPUT array-name{*};array x{100};
input x{*} 2.;Comparisons
-
The INPUT statement reads raw data in external files or data lines that are entered in-stream (following the DATALINES statement) that need to be described to SAS. The SET statement reads a SAS data set, which already contains descriptive information about the data values.
-
The INPUT statement reads data while the PUT statement writes data values, text strings, or both to the SAS log or to an external file.
-
The INPUT statement can read data from external files; the INFILE statement points to that file and has options that control how that file is read.
Examples
Example 1: Using Multiple Styles of Input in One INPUT Statement
data club1;
input Idno Name $18.
Team $ 25-30 Startwght Endwght;
datalines;
023 David Shaw red 189 165
049 Amelia Serrano yellow 189 165
... more data lines ...
; |
Variable
|
Type of Input
| |
|---|---|---|
|
Idno, Startwght, Endwght
|
list input
| |
|
Name
|
formatted input
| |
|
Team
|
column input
| |
Example 2: Using a Null INPUT Statement
data _null_; infile file-specification-1; file file-specification-2; input; put _infile_; run;
Example 3: Holding a Record in the Input Buffer
----+----1----+----2----+ C HIST101 Watson S Williams 0459 S Flores 5423 C MATH202 Sen S Lee 7085
data schedule(drop=type);
retain Course Professor;
input type $1. @;
if type='C' then
input course $ professor $;
else if type='S' then
do;
input Name $10. Id;
output schedule;
end;
datalines;
C HIST101 Watson
S Williams 0459
S Flores 5423
C MATH202 Sen
S Lee 7085
;
run;
proc print;
run;
Example 4: Holding a Record across Iterations of the DATA Step
data test; input name $ age @@; datalines; John 13 Monica 12 Sue 15 Stephen 10 Marc 22 Lily 17 ;
Example 5: Positioning the Pointer with a Numeric Variable
----+----1----+----2----+----3----+ 8 New York 1 USA 14 5 Cary 1 USA 2274 3 Chicago 1 USA 37 22 Tokyo 5 ASIA 80 5 Vancouver 2 CANADA 6 9 Milano 4 EUROPE 123
data office (drop=x);
infile file-specification;
input x @;
if 1<=x<=10 then
input @x City $9.;
else do;
put 'Invalid input at line ' _n_;
delete;
end;
run;Example 6: Positioning the Pointer with a Character Variable
data office2;
set office;
infile file-specification;
array region {5} $ _temporary_
('USA' 'CANADA' 'SA' 'EUROPE' 'ASIA');
input @city Location : 2. @;
input @(trim(region{location})) Population : 4.;
run;
Example 7: Moving the Pointer Backward
-
This INPUT statement uses the @ pointer control to read a value for BOOK starting at column 26. Then the pointer moves back to column 1 on the same line to read a value for COMPANY:
input @26 book $ @1 company;
-
These INPUT statements use +numeric-variable or +(expression) to move the pointer backward one column. These two sets of statements are equivalent.
-
m=-1; input x 1-10 +m y 2.;
-
input x 1-10 +(-1) y 2.;
-