![]() Chapter Contents |
![]() Previous |
![]() Next |
| INPUT |
| Valid: | in a DATA step |
| Category: | File-handling |
| Type: | Executable |
Syntax |
| INPUT <specification(s)><@|@@>; |
| Syntax Description |
The INPUT statement with no arguments is called a null INPUT statement.
| Featured in: | Using a Null INPUT Statement |
| Requirement: | The (variable-list) is followed by an (informat-list). |
| See Also: | How to Group Variables and Informats |
| Tip: | If the variable is previously defined as character, $ is not required. |
| Featured in: | Using Multiple Styles of Input in One INPUT Statement |
| See: | Column Pointer Controls and Line Pointer Controls |
| See: | Column Input |
| Featured in: | Using Multiple Styles of Input in One INPUT Statement |
| Restriction: | The (informat-list) must follow the (variable-list). |
| See: | How to Group Variables and Informats |
| Featured in: | Holding a Record Across Iterations of the DATA Step |
| Featured in: | Positioning the Pointer with a Character 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. |
| 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. |
| 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; |
| See Also: | How Invalid Data are Handled |
| See Also: | How Invalid Data are Handled |
| Details |
| Input Styles |
There are four ways to describe a record's values in the INPUT statement:
Each variable value is read by using one of these input styles. An INPUT statement may contain any or all of the available input styles, depending on the arrangement of data values in the input records. However, once named input is used in an INPUT statement, you cannot use another input style.input name $ 1-8 age 11-12;This INPUT statement can read the following data records:
----+----1----+----2----+ Peterson 21 Morgan 17Because NAME is a character variable, a $ appears between the variable name and column numbers. For more information, see INPUT, Column.
input name $ age;This INPUT statement can read data values that are separated by blanks or aligned in columns (with at least one blank between):
----+----1----+----2----+ Peterson 21 Morgan 17For more information, see INPUT, List.
input name $char8. +2 age 2.;This INPUT statement reads these data records correctly:
----+----1----+----2----+ Peterson 21 Morgan 17The pointer control of +2 moves the input pointer to the field that contains the value for the variable AGE. For more information, see INPUT, Formatted.
input name= $ age=;This INPUT statement reads the following data records correctly:
----+----1----+----2----+ name=Peterson age=21 name=Morgan age=17For more information, see INPUT, Named.
| Multiple Styles in a Single INPUT Statement |
An INPUT statement can contain any or all of the different input styles:
input idno name $18. team $ 25-30 startwght endwght;This INPUT statement reads the following data records correctly:
----+----1----+----2----+----3----+---- 023 David Shaw red 189 165 049 Amelia Serrano yellow 189 165The value of IDNO, STARTWGHT, and ENDWGHT are read with list input, the value of NAME with formatted input, and the value of TEAM with column input.
Note:
Once named input is used
in an INPUT statement, you cannot change input styles.
| Pointer Controls |
With column and line pointer controls, you can specify an absolute line number or column number to move the pointer or you can specify a column or line location relative to the current pointer position. Pointer Controls Available in the INPUT Statement lists the pointer controls that are available with the INPUT statement.
Note:
Always specify pointer controls before
the variable to which they apply.
Column pointer controls indicate the column in which an input value starts.
Use line pointer controls at the end of the INPUT statement
to move to the next input record or to define the number of input records
per observation. Line pointer controls specify which input record to read.
To read multiple data records into the input buffer, use the N= option in
the INFILE statement to specify the number of records. If you omit N=, you
need to take special precautions. For more information, see Reading More Than One Record per Observation.
Line-hold specifiers keep the pointer on the current input record when
SAS releases a record held by a trailing @ when
input;
input;
input @;
For example, you can read these data records with list, column, and formatted input:
----+----1----+----2----+----3 REGION1 49670 REGION2 97540 REGION3 86342
This INPUT statement uses list input to read the data records:
input region $ jansales;After reading a value for REGION, the pointer stops in column 9.
----+----1----+----2----+----3
REGION1 49670
↑
These INPUT statements use column and formatted input to read the data records:
To read a value for the variable REGION, both INPUT statements instruct the pointer to read 7 columns and stop in column 8.
----+----1----+----2----+----3
REGION1 49670
↑
| Reading More Than One Record per Observation |
input @31 age 3. #3 id 3-4 #2 @6 name $20.;Unless you use N= in the associated INFILE statement, the INPUT statement reads three input records each time the DATA step executes.
input name $ 1-10 #2 age 13-14 #4;
input a / b #1 @52 c #2;The INPUT statement assigns A a value from the first record. The pointer advances to the next input record to assign B a value. Then the pointer returns from the second record to column 1 of the first record and moves to column 52 to assign C a value. The #2 pointer control identifies two input records for each observation so that the pointer can return to the first record for the value of C.
If the number of input records per observation varies,
use the N= option in the INFILE statement to give the maximum number of records
per observation. For more information, see the N= option.
NOTE: SAS went to a new line when INPUT statement
reached past the end of a line.
You can alter the default behavior
(the FLOWOVER option) in the INFILE statement.
Use the STOPOVER option in the INFILE statement to treat this condition as an error and to stop building the data set.
Use the MISSOVER option in the INFILE statement to set the remaining INPUT statement variables to missing values if the pointer reaches the end of a record.
Use the TRUNCOVER option in the INFILE statement to read column input or formatted
input when the last variable that is read by the INPUT statement contains
varying-length data.
data test; input a @(a-3) b; datalines; 2 ;Therefore, SAS moves the pointer to column 1 after the value of A is read. Both variables A and B contain the same value.
| How Invalid Data are Handled |
When SAS encounters an invalid character in an input value for the variable indicated, it
input x ? 10-12; _error_=0;
input x ?? 10-12;In either case, SAS sets invalid values of X to missing values. For information on the causes of invalid data, see SAS Language Reference: Concepts.
| End-of-File |
End-of-file occurs when an INPUT statement reaches the end of the data. If a DATA step tries to read another record after it reaches an end-of-file then execution stops. If you want the DATA step to continue to execute, use the END= or EOF= option in the INFILE statement. Then you can write SAS program statements to detect the end-of-file, and to stop the execution of the INPUT statement but continue with the DATA step. For more information, see INFILE.
| Arrays |
The INPUT statement can use array references to read input data values. You can use an array reference in a pointer control if it is enclosed in parentheses. See Positioning the Pointer with a Character Variable.
INPUT array-name{*};
array x{100};
input x{*} 2.;
| Comparisons |
| Examples |
This example uses several input styles in a single 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 ...
;
| The values for . . . | Are read with . . . | |
|---|---|---|
| Idno, Startwght, Endwght | list input | |
| Name | formatted input | |
| Team | column input | |
data _null_; infile file-specification-1; file file-specification-2; input; put _infile_; run;
----+----1----+----2----+ C HIST101 Watson S Williams 0459 S Flores 5423 C MATH202 Sen S Lee 7085
To know which INPUT statement to use, check each record as it is read. Use an INPUT statement that reads only the variable that tells whether the record contains class or student.
data schedule(drop=type);
infile file-specification;
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;
run;
proc print; run;The first INPUT statement reads the TYPE value from column 1 of every line. Because this INPUT statement ends with a trailing @, the next INPUT statement in the DATA step reads the same line. The IF-THEN statements that follow check whether the record is a class or student line before another INPUT statement reads the rest of the line. The INPUT statements without a trailing @ release the held line. The RETAIN statement saves the values about the particular college course. The DATA step writes an observation to the SCHEDULE data set after a student record is read.
The following output that PROC PRINT generates shows the resulting data set SCHEDULE.
The SAS System 1
OBS Course Professor Name Id
1 HIST101 Watson Williams 459
2 HIST101 Watson Flores 5423
3 MATH202 Sen Lee 7085 |
This example shows how to create multiple observations for each input data record. Each record contains several NAME and AGE values. The DATA step reads a NAME value and an AGE value, outputs an observation, then reads another set of NAME and AGE values to output, and so on until all the input values in the record are processed.
data test; input name $ age @@; datalines; John 13 Monica 12 Sue 15 Stephen 10 Marc 22 Lily 17 ;The INPUT statement uses the double trailing @ to control the input pointer across iterations of the DATA step. The SAS data set contains six observations.
----+----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 123The first column has the column position for the office location. The next numeric column is the region category. The geographic region occurs before the number of employees in that office.
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;
This example uses character variables to position the pointer. The OFFICE data set, created in Example 5 , contains a character variable CITY whose values are the office locations. Suppose you discover that you need to read additional values from the raw data file. By using another DATA step, you can combine the @character-variable pointer control with a trailing @ and the @character-expression pointer control to locate the values.
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;The ARRAY statement assigns initial values to the temporary array
elements. These elements correspond to the geographic regions of the office
locations. The first INPUT statement uses an @character-variable
pointer control. Each record is scanned for the series of characters in the
value of CITY for that observation. Then the value of LOCATION is read from
the next nonblank column. LOCATION is a numeric category for the geographic
region of an office. The second INPUT statement uses an array reference in
the @character-expression pointer control to determine the location
POPULATION in the input records. The expression also uses the TRIM function
to trim trailing blanks from the character value. This way an exact match
is found between the character string in the input data and the value of the
array element.
The following output that PROC PRINT generates shows the resulting data set OFFICE2.
The SAS System 1
OBS City Location Population
1 New York 1 14
2 Cary 1 2274
3 Chicago 1 37
4 Vancouver 2 6
5 Milano 4 123 |
This example shows several ways to move the pointer backward.
input @26 book $ @1 company;
| See Also |
Statements:
|
![]() Chapter Contents |
![]() Previous |
![]() Next |
![]() Top of Page |
Copyright © 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.