Chapter Contents

Previous

Next
INFILE

INFILE



Identifies an external file to read with an INPUT statement

Valid: in a DATA Step
Category: File-handling
Type: Executable


Syntax
Arguments
Options
Host Options
DBMS-Specifications
Details
How to Use the INFILE Statement
Reading Multiple Input Files
Updating External Files in Place
Accessing the Contents of the Input Buffer
Reading Delimited Data
Reading Past the End of a Line
Comparisons
Examples
Example 1: Changing How Delimiters are Treated
Example 2: Handling Missing Values and Short Records with List Input
Example 3: Reading Files That Contain Variable-Length Records
Example 4: Reading from Multiple Input Files
Example 5: Updating an External File
Example 6: Truncating Copied Records
Example 7: Listing the Pointer Location
See Also

Syntax

INFILE file-specification <options> <host-options>;
INFILE DBMS-specifications;


Arguments

file-specification
identifies the source of the input data records, which is an external file or in-stream data. File-specification can have these forms:

'external-file'
specifies the physical name of an external file. The physical name is the name that the operating environment uses to access the file.

fileref
specifies the fileref of an external file.
Requirement: You must have previously associated the fileref with an external file in a FILENAME statement, FILENAME function, or an appropriate operating environment command.
See: FILENAME

fileref(file)
specifies a fileref of an aggregate storage location and the name of a file or member, enclosed in parentheses, that resides in that location.

Operating Environment Information:   Different operating environments call an aggregate grouping of files by different names, such as a directory, a MACLIB, or a partitioned data set. For details on how to specify external files, see the SAS documentation for your operating environment.  [cautionend]
Requirement: You must have previously associated the fileref with an external file in a FILENAME statement, a FILENAME function, or an appropriate operating environment command.
See: FILENAME

CARDS | CARDS4
for a definition, see DATALINES .
Alias: DATALINES | DATALINES4

DATALINES | DATALINES4
specifies that the input data immediately follows the DATALINES or DATALINES4 statement in the DATA step. This allows you to use the INFILE statement options to control how the INPUT statement reads instream data lines.
Alias: CARDS | CARDS4
Featured in: Changing How Delimiters are Treated


Options

BLKSIZE=block-size
specifies the block size of the input file.
Default: dependent on the operating environment

Operating Environment Information:   For details, see the SAS documentation for your operating environment.  [cautionend]

COLUMN=variable
names a variable that SAS uses to assign the current column location of the input pointer. Like automatic variables, the COLUMN= variable is not written to the data set.
Alias: COL=
See Also: LINE=
Featured in: Listing the Pointer Location

DELIMITER=delimiter(s)
specifies a delimiter for list input, where delimiter is

'list-of-delimiting-characters'
specifies one or more characters to read as delimiters.
Requirement: Enclose the list of characters in quotation marks.
Featured in: Changing How Delimiters are Treated

character-variable
specifies a character variable whose value becomes the delimiter.

Alias: DLM=
Default: blank space
Tip: DELIMITER= allows you to use list input even when the data are separated by characters other than spaces.
See: Reading Delimited Data
See Also: DSD option
Featured in: Changing How Delimiters are Treated

DSD
changes how SAS treats delimiters when list input is used and sets the default delimiter to a comma. When you specify DSD, SAS treats two consecutive delimiters as a missing value and removes quotation marks from character values.
Interaction: Use the DELIMITER= option to change the delimiter.
Tip: Use the DSD option and list input to read a character value that contains a delimiter within a quoted string. The INPUT statement treats the delimiter as a valid character and removes the quotation marks from the character string before the value is stored. Use the tilde (~) format modifier to retain the quotation marks.
See: Reading Delimited Data
See Also: DELIMITER=
Featured in: Handling Missing Values and Short Records with List Input and Changing How Delimiters are Treated

END=variable
names a variable that SAS sets to 1 when the current input data record is the last in the input file. Until SAS processes the last data record, the END= variable is set to 0. Like automatic variables, this variable is not written to the data set.
Restriction: You cannot use the END= option with

  • the UNBUFFERED option

  • the DATALINES or DATALINES4 statement

  • an INPUT statement that reads multiple input data records.

Tip: Use the option EOF= when END= is invalid.
Featured in: Reading from Multiple Input Files

EOF=label
specifies a statement label that is the object of an implicit GO TO when the INFILE statement reaches end-of-file. When an INPUT statement attempts to read from a file that has no more records, SAS moves execution to the statement label indicated.
Interaction: Use EOF= instead of the END= option with

  • the UNBUFFERED option

  • the DATALINES or DATALINES4 statement

  • an INPUT statement that reads multiple input data records.

Tip: The EOF= option is useful when you read from multiple input files sequentially.
See Also: END=, EOV=, and UNBUFFERED

EOV=variable
names a variable that SAS sets to 1 when the first record in a file in a series of concatenated files is read. The variable is set only after SAS encounters the next file. Like automatic variables, the EOV= variable is not written to the data set.
Tip: Reset the EOV= variable back to 0 after SAS encounters each boundary.
See Also: END= and EOF=

EXPANDTABS | NOEXPANDTABS
specifies whether to expand tab characters to the standard tab setting, which is set at 8-column intervals that start at column 9.
Default: NOEXPANDTABS
Tip: EXPANDTABS is useful when you read data that contain the tab character that is native to your operating environment.

FILENAME=variable
names a variable that SAS sets to the physical name of the currently opened input file. Like automatic variables, the FILENAME= variable is not written to the data set.
Tip: Use a LENGTH statement to make the variable length long enough to contain the value of the filename.
See Also: FILEVAR=
Featured in: Reading from Multiple Input Files

FILEVAR=variable
names a variable whose change in value causes the INFILE statement to close the current input file and open a new one. When the next INPUT statement executes, it reads from the new file that the FILEVAR= variable specifies. Like automatic variables, this variable is not written to the data set.
Restriction: The FILEVAR= variable must contain a character string that is a physical filename.
Interaction: When you use the FILEVAR= option, the file-specification is just a placeholder, not an actual filename or a fileref that has been previously-assigned to a file. SAS uses this placeholder for reporting processing information to the SAS log. It must conform to the same rules as a fileref.
Tip: Use FILEVAR= to dynamically change the currently opened input file to a new physical file.
See Also: Updating External Files in Place
Featured in: Reading from Multiple Input Files

FIRSTOBS=record-number
specifies a record number that SAS uses to begin reading input data records in the input file.
Default: 1
Tip: Use FIRSTOBS= with OBS= to read a range of records from the middle of a file.
Example: This statement processes record 50 through record 100:
infile file-specification firstobs=50 obs=100;

FLOWOVER
causes an INPUT statement to continue to read the next input data record if it does not find values in the current input line for all the variables in the statement. This is the default behavior of the INPUT statement.
See: Reading Past the End of a Line
See Also: MISSOVER, STOPOVER, and TRUNCOVER

LENGTH=variable
names a variable that SAS sets to the length of the current input line. SAS does not assign the variable a value until an INPUT statement executes. Like automatic variables, the LENGTH= variable is not written to the data set.
Tip: This option in conjunction with the $VARYING informat is useful when the field width varies.
Featured in: Reading Files That Contain Variable-Length Records and Truncating Copied Records

LINE=variable
names a variable that SAS sets to the line location of the input pointer in the input buffer. Like automatic variables, the LINE= variable is not written to the data set.
Range: 1 to the value of the N= option
Interaction: The value of the LINE= variable is the current relative line number within the group of lines that is specified by the N= option or by the #n line pointer control in the INPUT statement.
See Also: COLUMN= and N=
Featured in: Listing the Pointer Location

LINESIZE=line-size
specifies the record length that is available to the INPUT statement.

Operating Environment Information:   Values for line-size are dependent on the operating environment record size. For details, see the SAS documentation for your operating environment.  [cautionend]
Alias: LS=
Range: up to 32767
Interaction: If an INPUT statement attempts to read past the column that is specified by the LINESIZE= option, the action that is taken depends on whether the FLOWOVER, MISSOVER, SCANOVER, STOPOVER, or TRUNCOVER option is in effect. FLOWOVER is the default.
Tip: Use LINESIZE= to limit the record length when you do not want to read the entire record.
Example: If your data lines contain a sequence number in columns 73 through 80, use this INFILE statement to restrict the INPUT statement to the first 72 columns:
infile file-specification linesize=72;

LRECL=logical-record-length
specifies the logical record length.

Operating Environment Information:   Values for logical-record-length are dependent on the operating environment. For details, see the SAS documentation for your operating environment.  [cautionend]
Default: dependent on the operating environment's file characteristics.
Tip: LRECL= specifies the physical line length of the file. LINESIZE= tells the INPUT statement how much of the line to read.

MISSOVER
prevents an INPUT statement from reading a new input data record if it does not find values in the current input line for all the variables in the statement. When an INPUT statement reaches the end of the current input data record, variables without any values assigned are set to missing.
Tip: Use MISSOVER if the last field(s) may be missing and you want SAS to assign missing values to the corresponding variable.
See: Reading Past the End of a Line
See Also: FLOWOVER, SCANOVER, STOPOVER, and TRUNCOVER
Featured in: Handling Missing Values and Short Records with List Input

N=available-lines
specifies the number of lines that are available to the input pointer at one time.
Default: the highest value following a # pointer control in any INPUT statement in the DATA step. If you omit a # pointer control, the default value is 1.
Interaction: This option affects only the number of lines that the pointer can access at a time; it has no effect on the number of lines an INPUT statement reads.
Tip: When you use # pointer controls in an INPUT statement that are less than the value of N=, you might get unexpected results. To prevent this, include a # pointer control that equals the value of the N= option. For example,
infile 'external
file' n=5;
input #2 name : $25. #3 job : $25. #5;
The INPUT statement includes a #5 pointer control, even though no data are read from that record.
Featured in: Listing the Pointer Location

NBYTE=variable
specifies the name of a variable that contains the number of bytes to read from a file when you are reading data in stream record format (RECFM=S in the FILENAME statement).
Default: the LRECL value of the file
Interaction: If the number of bytes to read is set to -1, the FTP and SOCKET access methods return the number of bytes that are currently available in the input buffer.
See: the FILENAME, SOCKET RECFM= option and the FILENAME, FTP RECFM= option

OBS=record-number
specifies the record number of the last record to read in an input file that is read sequentially.
Tip: Use OBS= with FIRSTOBS= to read a range of records from the middle of a file.
Example: This statement processes only the first 100 records in the file:
infile file-specification obs=100;

PAD | NOPAD
controls whether SAS pads the records that are read from an external file with blanks to the length that is specified in the LRECL= option.
Default: NOPAD
See Also: LRECL=

PRINT | NOPRINT
specifies whether the input file contains carriage control characters.
Tip: To read a print file in a DATA step without having to remove the carriage control characters, specify PRINT. To read the carriage control characters as data values, specify NOPRINT.

RECFM=record-format
specifies the record format of the input file.

Operating Environment Information:    Values for record-format are dependent on the operating environment. For details, see the SAS documentation for your operating environment.  [cautionend]

SCANOVER
causes the INPUT statement to scan the input data records until the character string that is specified in the @'character-string' expression is found.
Interaction: The MISSOVER, TRUNCOVER, and STOPOVER options change how the INPUT statement behaves when it scans for the @'character-string' expression and reaches the end of record. By default (FLOWOVER option), the INPUT statement scans the next record while these other options cause scanning to stop.
Tip: It is redundant to specify both SCANOVER and FLOWOVER.
See: Reading Past the End of a Line
See Also: FLOWOVER, MISSOVER, STOPOVER, and TRUNCOVER

SHAREBUFFERS
specifies that the FILE statement and the INFILE statement share the same buffer.
Alias: SHAREBUFS
Tip: Use SHAREBUFFERS with the INFILE, FILE, and PUT statements to update an external file in place. This saves CPU time because the PUT statement output is written straight from the input buffer instead of the output buffer.
Tip: Use SHAREBUFFERS to update specific fields in an external file instead of an entire record.
Featured in: Updating an External File

START=variable
names a variable whose value SAS uses as the first column number of the record that the PUT _INFILE_ statement writes. Like automatic variables, the START variable is not written to the data set.
See Also: _INFILE_ option in the PUT statement

STOPOVER
causes the DATA step to stop processing if an INPUT statement reaches the end of the current record without finding values for all variables in the statement. When an input line does not contain the expected number of values, SAS sets _ERROR_ to 1, stops building the data set as though a STOP statement has executed, and prints the incomplete data line.
Tip: Use FLOWOVER to reset the default behavior.
See: Reading Past the End of a Line
See Also: FLOWOVER, MISSOVER, SCANOVER, and TRUNCOVER
Featured in: Handling Missing Values and Short Records with List Input

TRUNCOVER
overrides the default behavior of the INPUT statement when an input data record is shorter than the INPUT statement expects. By default, the INPUT statement automatically reads the next input data record. TRUNCOVER enables you to read variable-length records when some records are shorter than the INPUT statement expects. Variables without any values assigned are set to missing.
Tip: Use TRUNCOVER to assign the contents of the input buffer to a variable when the field is shorter than expected.
See: Reading Past the End of a Line
See Also: FLOWOVER, MISSOVER, SCANOVER, and STOPOVER

UNBUFFERED
tells SAS not to perform a buffered ("look ahead") read.
Alias: UNBUF
Interaction: When you use UNBUFFERED, SAS never sets the END= variable to 1.
Tip: When you read in-stream data with a DATALINES statement, UNBUFFERED is in effect.

_INFILE_=variable
names a character variable that references the contents of the current input buffer for this INFILE statement. You can use the variable in the same way as any other variable, even as the target of an assignment. The variable is automatically retained and initialized to blanks. Like automatic variables, the _INFILE_= variable is not written to the data set.
Restriction: variable cannot be a previously defined variable. Make sure that the _INFILE_= specification is the first occurence of this variable in the DATA step. Do not set or change the length of _INFILE_= variable with the LENGTH or ATTRIB statements. However, you can attach a format to this variable with the ATTRIB or FORMAT statement.
Interaction: The maximum length of this character variable is the logical record length (LRECL=) for the specified INFILE statement. However, SAS does not open the file to know the LRECL= until prior to the execution phase. Therefore, the designated size for this variable during the compilation phase is 32,767.
Tip: Modification of this variable directly modifies the INFILE statement's current input buffer. Any PUT _INFILE_ (when this INFILE is current) that follows the buffer modification reflects the modified buffer contents. The _INFILE_= variable accesses only the current input buffer of the specified INFILE statement even if you use the N= option to specify multiple buffers.
Tip: To access the contents of the input buffer in another statement without using the _INFILE_= option, use the automatic variable _INFILE_.
Main Discussion: Accessing the Contents of the Input Buffer


Host Options

Operating Environment Information:   For descriptions of operating environment-specific options in the INFILE statement, see the SAS documentation for your operating environment.  [cautionend]

DBMS-Specifications

DBMS-Specifications
enable you to read records from some DBMS files. You must license SAS/ACCESS software to be able to read from DBMS files. See the SAS/ACCESS documentation for the DBMS that you use.


Details

Operating Environment Information:   The INFILE statement contains operating environment-specific material. See the SAS documentation for your operating environment before using this statement.  [cautionend]

How to Use the INFILE Statement

Because the INFILE statement identifies the file to read, it must execute before the INPUT statement that reads the input data records. You can use the INFILE statement in conditional processing, such as an IF-THEN statement, because it is executable. This allows you to control the source of the input data records.

Usually, you use an INFILE statement to read data from an external file. When data are read from the job stream, you must use a DATALINES statement. However, to take advantage of certain data-reading options that are available only in the INFILE statement, you can use an INFILE statement with the file-specification DATALINES and a DATALINES statement in the same DATA step.

When you use more than one INFILE statement for the same file-specification and you use options in each INFILE statement, the effect is additive. To avoid confusion, use all the options in the first INFILE statement for a given external file.

Reading Multiple Input Files

You can read from multiple input files in a single iteration of the DATA step in one of two ways:

.

Updating External Files in Place

You can use the INFILE statement in combination with the FILE statement to update records in an external file. Follow these steps:

  1. Specify the INFILE statement before the FILE statement.

  2. Specify the same fileref or physical filename in each statement.

  3. Use options that are common to both the INFILE and FILE statements in the INFILE statement instead of the FILE statement. (Any such options that are used in the FILE statement are ignored.)

See Updating an External File.

To update individual fields within a record instead of the entire record, see SHAREBUFFERS.

Accessing the Contents of the Input Buffer

In addition to the _INFILE_= variable, you can use the automatic _INFILE_ variable to reference the contents of the current input buffer for the most recent execution of the INFILE statement. This character variable is automatically retained and initialized to blanks. Like other automatic variable, _INFILE_ is not written to the data set.

When you specify the _INFILE_= option in a INFILE statement then this variable is also indirectly referenced by the automatic _INFILE_ variable. If the automatic _INFILE_ variable is present and you omit _INFILE_= in a particular INFILE statement, then SAS creates an internal _INFILE_= variable for that INFILE statement. Otherwise, SAS does not create the _INFILE_= variable for a particular FILE.

During execution and at the point of reference, the maximum length of this character variable is the maximum length of the current _INFILE_= variable. However, because _INFILE_ merely references other variables whose lengths are not known until prior to the execution phase, the designated length is 32,767 during the compilation phase. For example, if you assign _INFILE_ to a new variable whose length is undefined, the default length of the new variable is 32,767. You can not use the LENGTH statement and the ATTRIB statement to set or override the length of _INFILE_. You can use the FORMAT statement and the ATTRIB statement to assign a format to _INFILE_.

Like other SAS variables, you can update the _INFILE_ variable in an assignment statement. You can also use a format with _INFILE_ in a PUT statement. For example

put _infile_ $hex100.;
outputs the contents of the input buffer using a hexadecimal format.

Any modification of the _INFILE_ directly modifies the current input buffer for the current INFILE statement. The execution of any PUT _INFILE_ statement that follows this buffer modification will reflect the contents of the modified buffer.

_INFILE_ only accesses the contents of the current input buffer for a INFILE statement, even when you use the N= option to specify multiple buffers. You can access all the N= buffers, but you must use a INPUT statement with the # line pointer control to make the desired buffer the current input buffer.

Reading Delimited Data

By default, the delimiter to read input data records with list input is a blank space. Both the DSD option and the DELIMITER= option affect how list input handles delimiters. The DELIMITER= option specifies that the INPUT statement use a character other than a blank as a delimiter for data values that are read with list input. When the DSD option is in effect, the INPUT statement uses a comma as the default delimiter.

To read a value as missing between two consecutive delimiters, use the DSD option. By default, the INPUT statement treats consecutive delimiters as a unit. When you use DSD, the INPUT statement treats consecutive delimiters separately. Therefore, a value that is missing between consecutive delimiters is read as a missing value. To change the delimiter from a comma to another value, use the DELIMITER= option.

For example, this DATA step program uses list input to read data that are separated with commas. The second data line contains a missing value. Because SAS allows consecutive delimiters with list input, the INPUT statement cannot detect the missing value.

data scores;
   infile datalines delimiter=',';
   input test1 test2 test3;
   datalines;
91,87,95
97,,92
,1,1
;
With the FLOWOVER option in effect, the data set SCORES contains two, not three, observations. The second observation is built incorrectly:

OBS TEST1 TEST2 TEST3
1 91 87 95
2 97 92 1

To correct the problem, use the DSD option in the INFILE statement.

   infile datalines dsd;
Now the INPUT statement detects the two consecutive delimiters and therefore assigns a missing value to variable TEST 2 in the second observation.

OBS TEST1 TEST2 TEST3
1 91 87 95
2 97 . 92
3 1 1 1

The DSD option also enables list input to read a character value that contains a delimiter within a quoted string. For example, if data are separated with commas, DSD enables you to place the character string in quotation marks and read a comma as a valid character. SAS does not store the quotation marks as part of the character value. To retain the quotation marks as part of the value, use the tilde (~) format modifier in an INPUT statement. See Changing How Delimiters are Treated.

Reading Past the End of a Line

By default, if the INPUT statement tries to read past the end of the current input data record, it moves the input pointer to column 1 of the next record to read the remaining values. This default behavior is specified by the FLOWOVER option. A message is written to the SAS log:

NOTE: SAS went to a new line when INPUT
@'CHARACTER_STRING' scanned past the end of a line.
The STOPOVER option treats this condition as an error and stops building the data set. The MISSOVER option sets the remaining INPUT statement variables to missing values. The SCANOVER option scans the input record until it finds the specified character-string. The FLOWOVER option restores the default behavior.

The TRUNCOVER option, like the MISSOVER option, overrides the default behavior of the INPUT statement. The MISSOVER option, however, causes the INPUT statement to set a value to missing if the statement is unable to read an entire field because the field length specified in the INPUT statement is too short. The TRUNCOVER option writes whatever characters are read to the appropriate variable so that you know what the input data record contained.

For example, an external file with variable-length records contains these records:

----+----1----+----2
1
22
333
4444
55555
The following DATA step reads these data to create a SAS data set. Only one of the input records is as long as the informatted length of the variable TESTNUM.
data numbers;
   infile 'external-file';
   input testnum 5.;
run;
This DATA step creates the three observations from the five input records because by default the FLOWOVER option is used to read the input records.

If you use the MISSOVER option in the INFILE statement, the DATA step creates five observations. However, all the values that were read from records that were too short are set to missing. Use the TRUNCOVER option in the INFILE statement to correct this problem:

infile 'external-file' truncover;
The DATA step now reads the same input records and creates five observations. See The Value of TESTNUM Using Different INFILE Statement Options to compare the SAS data sets.

The Value of TESTNUM Using Different INFILE Statement Options
OBS FLOWOVER MISSOVER TRUNCOVER
1 22 . 1
2 4444 . 22
3 55555 . 333
4
. 4444
5
55555 55555


Comparisons


Examples

Example 1: Changing How Delimiters are Treated

By default, the INPUT statement uses a blank as the delimiter. This DATA step uses a comma as the delimiter:

data num;
   infile datalines dsd;
   input x y z;
   datalines;
,2,3
4,5,6
7,8,9
;
The argument DATALINES in the INFILE statement allows you to use an INFILE statement option to read in-stream data lines. The DSD option sets the comma as the default delimiter. Because a comma precedes the first value in the first dataline, a missing value is assigned to variable X in the first observation, and the value 2 is assigned to variable Y.

If the data uses multiple delimiters or a single delimiter other than a comma, simply specify the delimiter values with the DELIMITER= option. In this example, the characters a and b function as delimiters:

 data nums;
    infile datalines dsd delimiter='ab';
    input X Y Z;
    datalines;
   1aa2ab3
   4b5bab6
   7a8b9
   ;
The output that PROC PRINT generates shows the resulting NUMS data set. Values are missing for variables in the first and second observation because DSD causes list input to detect two consecutive delimiters. If you omit DSD, the characters a, b, aa, ab, ba, or bb function as the delimiter and no variables are assigned missing values.

The NUMS Data Set
                         The SAS System                        1

                       OBS    X    Y    Z

                        1     1    .    2
                        2     4    5    .
                        3     7    8    9

This DATA step uses modified list input and the DSD option to read data that are separated by commas and that may contain commas as part of a character value:

data scores;
   infile datalines dsd;
   input Name : $9. Score
         Team  : $25. Div $;
   datalines;
Joseph,76,"Red Racers, Washington",AAA
Mitchel,82,"Blue Bunnies, Richmond",AAA
Sue Ellen,74,"Green Gazelles, Atlanta",AA
;
The output that PROC PRINT generates shows the resulting SCORES data set. The delimiter (comma) is stored as part of the value of TEAM while the quotation marks are not. The folowing output shows how to use the tilde (~) format modifier in an INPUT statement to retain the quotation marks in character data.

Data Set SCORES
                         The SAS System                        1

  OBS    NAME         SCORE             TEAM              DIV

   1     Joseph         76     Red Racers, Washington     AAA
   2     Mitchel        82     Blue Bunnies, Richmond     AAA
   3     Sue Ellen      74     Green Gazelles, Atlanta    AA 


Example 2: Handling Missing Values and Short Records with List Input

This example demonstrates how to prevent missing values from causing problems when you read the data with list input. Some data lines in this example contain fewer than 5 temperature values. Use the MISSOVER option so that these values are set to missing.

data weather;
   infile datalines missover;
   input temp1-temp5;
   datalines;
97.9 98.1 98.3
98.6 99.2 99.1 98.5 97.5
96.2 97.3 98.3 97.6 96.5
;
SAS reads the three values on the first data line as the values of TEMP1, TEMP2, and TEMP3. The MISSOVER option causes SAS to set the values of TEMP4 and TEMP5 to missing for the first observation because no values for those variables are in the current input data record.

When you omit MISSOVER option or use FLOWOVER, SAS moves the input pointer to line 2 and reads values for TEMP4 and TEMP5. The next time the DATA step executes, SAS reads a new line which, in this case, is line 3. This message appears in the SAS log:

NOTE: SAS went to a new line when INPUT statement
      reached past the end of a line.

You can also use the STOPOVER option in the INFILE statement. This causes the DATA step to halt execution when an INPUT statement does not find enough values in a record of raw data:

 infile datalines stopover;
Because SAS does not find a TEMP4 value in the first data record, it sets _ERROR_ to 1, stops building the data set, and prints data line 1.

Example 3: Reading Files That Contain Variable-Length Records

This example shows how to use LENGTH=, in combination with the $VARYING. informat, to read a file that contains variable-length records:

data a;
   infile file-specification length=linelen; 
   input firstvar 1-10 @;  /* assign LINELEN   */
   varlen=linelen-10;      /* Calculate VARLEN */
   input @11 secondvar $varying500. varlen;
run;
The following occurs in this DATA step:

See the informat $VARYINGw. for more information.

Example 4: Reading from Multiple Input Files

The following DATA step reads from two input files during each iteration of the DATA step. As SAS switches from one file to the next, each file remains open. The input pointer remains in place to begin reading from that location the next time an INPUT statement reads from that file.

data qtrtot(drop=jansale febsale marsale
                 aprsale maysale junsale);
     /* identify location of 1st file */
   infile file-specification-1;
     /* read values from 1st file     */
   input name $ jansale febsale marsale; 
   qtr1tot=sum(jansale,febsale,marsale);

     /* identify location of 2nd file */
   infile file-specification-2;
     /* read values from 2nd file     */
   input @7 aprsale maysale junsale;
   qtr2tot=sum(aprsale,maysale,junsale);
run;
The DATA step terminates when SAS reaches an end-of-file on the shortest input file.

This DATA step uses FILEVAR= to read from a different file during each iteration of the DATA step:

data allsales;
   length fileloc myinfile $ 300;
   input fileloc $ ; /* read instream data       */

  /* The INFILE statement closes the current file 
     and opens a new one if FILELOC changes value 
     when INFILE executes                        */
   infile file-specification filevar=fileloc 
          filename=myinfile end=done; 

  /* DONE set to 1 when last input record read  */
   do while(not done);
  /* Read all input records from the currently  */
  /* opened input file, write to ALLSALES       */
     input name $ jansale febsale marsale;
     output;
   end;
   put 'Finished reading ' myinfile=; 
   datalines;
external-file-1
external-file-2
external-file-3
;
The FILENAME= option assigns the name of the current input file to the variable MYINFILE. The LENGTH statement ensures that the FILENAME= variable and FILEVAR= variable have a length long enough to contain the value of the filename. The PUT statement prints the physical name of the currently open input file to the SAS log.

Example 5: Updating an External File

This example shows how to use the INFILE statement with the SHAREBUFFERS option and the INPUT, FILE, and PUT statements to update an external file in place:

data _null_;
     /* The INFILE and FILE statements     */
     /* must specify the same file.       */
   infile file-specification-1 sharebuffers;
   file file-specification-1;
   input state $ 1-2 phone $ 5-16; 
     /* Replace area code for NC exchanges */
   if state= 'NC' and substr(phone,5,3)='333' then
     phone='910-'||substr(phone,5,8);
   put phone 5-16;
run;

Example 6: Truncating Copied Records

The LENGTH= option is useful when you copy the input file to another file with the PUT _INFILE_ statement. Use LENGTH= to truncate the copied records. For example, these statements truncate the last 20 columns from each input data record before the input data record is written to the output file:

data _null_;
   infile  file-specification-1 length=a;
   input;
   a=a-20;
   file file-specification-2;
   put _infile_;
run;

The START= option is also useful when you want to truncate what the PUT _INFILE_ statement copies. For example, if you do not want to copy the first 10 columns of each record, these statements copy from column 11 to the end of each record in the input buffer:

data _null_;
   infile file-specification start=s;
   input;
   s=11;
   file file-specification-2;
   put _infile_;
run;

Example 7: Listing the Pointer Location

This DATA step assigns the value of the current pointer location in the input buffer to the variables LINEPT and COLUMNPT:

data _null_;
   infile datalines n=2 line=Linept col=Columnpt;
   input name $ 1-15 #2 @3 id;
   put linept= columnpt=;
   datalines;
J. Brooks
  40974
T. R. Ansen
  4032
;
These statements produce the following line for each execution of the DATA step because the input pointer is on the second line in the input buffer when the PUT statement executes:
Linept=2 Columnpt=9
Linept=2 Columnpt=8

See Also

Statements:

FILENAME
INPUT
PUT


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.