.
The INPUT statement describes the arrangement of a target data to be read in a DATA step. You need to provide variable names followed by $ (indicating a character value), pointer control, column-specifications, informat, and/or line hold specifiers (i.e., @, and @@) in an INPUT statement.
The DATALINES statement (replacing the old CARDS statement) indicates that data lines follow in a DATA step. In order to read external data files, you have to use the INFILE statement.
There are six input styles used in the INPUT statement: list input, column input, formatted input, modified list input, named input, and mixed input. The following table summarizes features of four major styles.
| Variable | List (Free) | Modified | Column | Formatted |
| Length | Standard | Flexible | Nonstandard | Nonstandard |
| Delimiter | blank | Yes | n/a | n/a |
| Missing values | . | . or delimiter | blank | blank |
| Variable order, DSD | Yes | Yes | n/a | n/a |
| Re-reading | No | No | Yes | Yes |
| Format modifier | No | :, &, ~ | No | No |
| Pointer control | n/a | n/a | @n, +n, | @n, +n, #, / |
| Informat | No | No | No | $n., n.d, |
Which input style is the best? It depends on your skills and characteristics of data sets. If your data set has just a few observations with several variables, the list input or the named input will be better than the column input or the formatted input. When data elements are not separated with a blank or other delimiters, you cannot use the list input style. When data are well arranged, the column input or formatted input will be better than the list input. Therefore, you need to examine the data structure carefully when deciding the best input style. Of course, you must take this issue into account from the data coding stage.
The input style simply lists variables separated with a blank. This style is also called the free format.
A character variable should be followed by $. A missing value should be marked with a period (.); a blank does not mean a missing value in this input style. Do not use more than one "." for a missing value. The maximum length of a string variable is 8 characters (standard); that is, fixed 8bytes of memory are assigned to each variable. Therefore, a string longer than 8 characters will be trimmed. If you want to read a string longer than 8 characters, use LENGTH, INFORMAT, or ATTRIB statements. Or you may use different input styles such as column input or formatted input.
In the example above, you may use "INFORMAT analysis $15." instead of the LENGTH statement. INFORMAT tells how data are read, while FORMAT tells the format to be displayed. MMDDYY10. reads data in the MM/DD/YYYY format. DATE9. displays date in the DDMMMYYYY format. Without the FORMAT for year, SAS will return odd numbers such as 14884, which are internally used in SAS.
The following example reads an ASCII text file with a comma delimited. Remember the default delimiter is a blank. See the INFILE statement for the detail.
The modified list style is a mixture of the list input and the formatted input. This style can deal with ill-structured data. There are three format modifiers to be used especially when reading complex data.
The following example illustrates how : and & work in INPUT. The "Lindblom80" in the first row is trimed since it exceeds 8 characters; only first 8 characters, as specified in the INPUT statement, are read and the last two characters "08" are ignored. In the second row, SAS reads the first four characters "Park", which are shorter than 8 characters, and then encounters a comma (delimiter); SAS stops reading data for the variable "name" and moves on to next variable. The variable "title" is defined by & with a maximum 50 characters. The delimiter, a comma, in the first and third row is treated as a character value. Two consecutive double quotation marks are read as a double quotation marks. Therefore, the title of the second observation is Readig "Small Is Beautiful" as shown in the output. Characters exceeding the maximum, 50 characters in this case, will be ignored.
The INFILE statement above says that data are comma delimited and will be listed after DATALINES. DSD at the end of INFILE eliminates double quotation marks enclosing the character value when reading data. If you omit DSD, SAS will consider a comma in character values as a delimiter and read enclosing double quotation marks as character values. As a result, the output would look like,
The second example shows how ~ (tilde) and DSD work together to read a string with a delimiter. SAS reads a comma in the string as a character value but does not eliminate double quoatation marks enclosing the string. If you omit DSD, the title of the second row will be '"Still Muddling' because SAS treats a comma in the string as the delimiter and stops reading the character value for variable "title."
You may not ommit : after "year" in the INPUT statement above even when data are in the same fixed format. When the variable "year" is specified at the last of the list in the INPUT statement, : is not necessary.
The column input style reads the value of a variable from its specified column location. A variable name is followed by its starting and ending columns.
SAS reads a variable "name" from 1 through 5 columns, id from 6 through 12 columns, and so on. This input style works good for well structured data.
The formatted input style reads input values with specified inforamts after variable names. Informats provide the data type and the width of an input value. Numeric variables are expressed in the w.d format, where w represents the total length of a variable and d the number of digits below the decimal point. You cannot omit d even when d = 0. The use $CHARw. or $w. format is used for character variables, while the DATEw. or DDMMYYw. format is used for the date type.
You can use parentheses to simplify expressions.
The following example illustrates how effectively the formatted input uses column holders, informats (e.g., COMMAn., DOLLarn., PERCENTn., and MMDDYY10.), and parentheses. SAS reads a variable x1 as a string five characters long, a numeric variable x2 7 digits long without decimal point, three digit numeric variables x3 through x5, and then skip one column (+1) before reading a numeric variable income containing commas.
The formattted input can use both column and line pointer controls. These pointer controls are very useful when reading multiple observations from the same line or reading an observation from multiple lines.
The named input reads a data value that follows its variable name. A variable name and its data value are separated by an equal sign. String data are NOT enclosed by double quotation marks in this style. Like the list style, the named style supports standard length of variables only. The format provides some sorts of flexibility, but it will not be appropriate for a large data set.
The INPUT statement can contain list input, column input, formatted input, and/or named input.
Let us read multiple observations in a line using the formatted input style. The following script reads string variables "name" and "id" consecutively, and reads three digit numeric variables x1 through x3, and then keep reading next observations, if available, without moving to next line.
The following example reads data using a DO loop.
Suppose individual observations have different numbers of repeatition. Pay attention to IF and OUTPUT statements.
Now, let us read observations whose data are provided in multiple lines. The #n or / indicates a data line to be read for the variable.
The INPUT statement above says that read a 7 digit numeric variable "No" from the first line (#1), a 15 character string variable "name" from the second line (#2), a 50 character string variable "address" from the next line (/), and a 12 character string variable "phone" from the fourth line (#4). Alternatively, the INPUT may be replaced by "INPUT No 7.0 / Name $15 / Address $50 / Phone $12;."