|Choosing an Input Style|
The INPUT statement reads raw data from instream data lines or external files into a SAS data set. You can use the following different input styles, depending on the layout of data values in the records:
List input uses a scanning method for locating data values. Data values are not required to be aligned in columns but must be separated by at least one blank (or other defined delimiter). List input requires only that you specify the variable names and a dollar sign ($), if defining a character variable. You do not have to specify the location of the data fields.
An example of list input follows:
data scores; length name $ 12; input name $ score1 score2; datalines; Riley 1132 1187 Henderson 1015 1102 ;
List input has several restrictions on the type of data that it can read:
Note: Nonstandard numeric values, such as packed decimal
data, must use the formatted style of input. See Formatted Input for more information
|Modified List Input|
A more flexible version of list input, called modified list input, includes format modifiers. The following format modifiers enable you to use list input to read nonstandard data by using SAS informats:
The following is an example of the : and ~ format modifiers:
data scores; infile datalines dsd; input Name : $9. Score1-Score3 Team ~ $25. Div $; datalines; Smith,12,22,46,"Green Hornets, Atlanta",AAA Mitchel,23,19,25,"High Volts, Portland",AAA Jones,09,17,54,"Vulcans, Las Vegas",AA ; proc print data=scores noobs; run;
Output from Example with Format Modifiers
Name Score1 Score2 Score3 Team Div Smith 12 22 46 "Green Hornets, Atlanta" AAA Mitchel 23 19 25 "High Volts, Portland" AAA Jones 9 17 54 "Vulcans, Las Vegas" AA
Column input enables you to read standard data values that are aligned in columns in the data records. Specify the variable name, followed by a dollar sign ($) if it is a character variable, and specify the columns in which the data values are located in each record:
data scores; infile datalines truncover; input name $ 1-12 score2 17-20 score1 27-30; datalines; Riley 1132 987 Henderson 1015 1102 ;
Note: Use the TRUNCOVER option on the INFILE statement to ensure
that SAS handles data values of varying lengths appropriately.
To use column input, data values must be:
Note: You cannot use an informat
with column input.
Features of column input include the following:
Formatted input combines the flexibility of using informats with many of the features of column input. By using formatted input, you can read nonstandard data for which SAS requires additional instructions. Formatted input is typically used with pointer controls that enable you to control the position of the input pointer in the input buffer when you read data.
The INPUT statement in the following DATA step uses formatted input and pointer controls. Note that $12. and COMMA5. are informats and +4 and +6 are column pointer controls.
data scores; input name $12. +4 score1 comma5. +6 score2 comma5.; datalines; Riley 1,132 1,187 Henderson 1,015 1,102 ;
Note: You also can use informats to read data that is not aligned
in columns. See Modified List Input
for more information.
Important points about formatted input are:
You can use named input to read records in which data values are preceded by the name of the variable and an equal sign (=). The following INPUT statement reads the data lines containing equal signs.
data games; input name=$ score1= score2=; datalines; name=riley score1=1132 score2=1187 ; proc print data=games; run;
Note: When an equal sign follows a variable in an INPUT statement, SAS expects that data remaining on the input line contains only named input values. You cannot switch to another form of input in the same INPUT statement after using named input.
Also, note that any variable that exists in the input data but is not
defined in the INPUT statement generates a note in the SAS log indicating
a missing field.
|Additional Data-Reading Features|
In addition to different styles of input, there are many tools to meet the needs of different data-reading situations. You can use options in the INFILE statement in combination with the INPUT statement to give you additional control over the reading of data records. Additional Data-Reading Features lists common data-reading tasks and the appropriate features available in the INPUT and INFILE statements.
|If you want to read data that has ...||while ...||then use ...|
|multiple records||creating a single observation||#n or / line pointer control in the INPUT statement with a DO loop.|
|a single record||creating multiple observations||trailing @@ in the INPUT statement.|
|trailing @ with multiple INPUT and OUTPUT statements.|
|variable-length data fields and records||reading delimited data||list input with or without a format modifier in the INPUT statement and the TRUNCOVER, DELIMITER= and/or DSD options in the INFILE statement.|
|reading non-delimited data||$VARYINGw. informat in the INPUT statement and the LENGTH= and TRUNCOVER options in the INFILE statement.|
|a file with varying record layouts||IF-THEN statements with multiple INPUT statements, using trailing @ or @@ as necessary.|
|hierarchical files||IF-THEN statements with multiple INPUT statements, using trailing @ as necessary.|
|more than one input file or to control the program flow at EOF||EOF= or END= option in an INFILE statement.|
|multiple INFILE and INPUT statements.|
|FILEVAR=option in an INFILE statement.|
|FILENAME statement with concatentation, wildcard, or piping.|
|only part of each record||LINESIZE=option in an INFILE statement.|
|some but not all records in the file||FIRSTOBS=and OBS= options in an INFILE statement; FIRSTOBS= and OBS= system options; #n line pointer control.|
|instream datalines||controlling the reading with special options||INFILE statement with DATALINES and appropriate options.|
|starting at a particular column||@ column pointer controls.|
|leading blanks||maintaining them||$CHARw. informat in an INPUT statement.|
|a delimiter other than blanks (with list input or modified list input with the colon modifier)||DELIMITER= option and/or DSD option in an INFILE statement.|
|the standard tab character||DELIMITER= option in an INFILE statement; or the EXPANDTABS option in an INFILE statement.|
|missing values (with list input or modified list input with the colon modifier)||creating observations without compromising data integity; protecting data integrity by overriding the default behavior||TRUNCOVER option in an INFILE statement; DSD and/or DELIMITER= options might also be needed.|
Top of Page
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.