24;77 195 177 163
24;31 220 213 198
24;56 173 166 155
24;12 135 125 116
;;;;
External Files
The following example shows how to read in raw data from an external file using the
INFILE and INPUT statements:
data weight;
infile file-specification or path-name;
input PatientID $ Week1 Week8 Week16;
loss=Week1-Week16;
run;
Note: See the SAS documentation for your operating environment for information about
how to specify a file with the INFILE statement.
Reading Raw Data with the INPUT Statement
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
column input
formatted input
named input
You can also combine styles of input in a single INPUT statement. For details about the
styles of input, see the INPUT statement in SAS DATA Step Statements: Reference.
List Input
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
Reading Raw Data with the INPUT Statement 445
Henderson 1015 1102
;
List input has several restrictions on the type of data that it can read:
Input values must be separated by at least one blank (the default delimiter) or by the
delimiter specified with the DLM= or DLMSTR= option in the INFILE statement. If
you want SAS to read consecutive delimiters as if there is a missing value between
them, specify the DSD option in the INFILE statement.
Blanks cannot represent missing values. A real value, such as a period, must be used
instead.
To read and store a character input value longer than 8 bytes, define a variable's
length by using a LENGTH, INFORMAT, or ATTRIB statement before the INPUT
statement, or by using modified list input, which consists of an informat and the
colon modifier in the INPUT statement. See “Modified List Input” on page 446 for
more information.
Character values cannot contain embedded blanks when the file is delimited by
blanks.
Fields must be read in order.
Data must be in standard numeric or character format.
Note: Nonstandard numeric values, such as packed decimal data, must use the formatted
style of input. See “Formatted Input” on page 448 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 & (ampersand) format modifier enables you to read character values that contain
one or more embedded blanks with list input and to specify a character informat.
SAS reads until it encounters two consecutive blanks, the defined length of the
variable, or the end of the input line, whichever comes first.
The : (colon) format modifier enables you to use list input but also to specify an
informat after a variable name, whether character or numeric. SAS reads until it
encounters a blank column, the defined length of the variable (character only), or the
end of the data line, whichever comes first.
The ~ (tilde) format modifier enables you to read and retain single quotation marks,
double quotation marks, and delimiters within character values.
The following is an example of the : and ~ format modifiers. You must use the DSD
option in the INFILE statement. Otherwise, the INPUT statement ignores the ~ format
modifier.
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
;
446 Chapter 21 Reading Raw Data

Get SAS 9.4 Language Reference, 6th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.