Reading data from external files
For example, assume you have the data from example 1 in a file.
File contents (4 lines):
1 68 144 M 23
2 78 202 M 34
3 62 99 F 37
4 61 101 F 45
Depending on exactly where you've put the file, the following code will read the data and print it out.
FILENAME EXAMPLE1 'long-file-name';
DATA LISTINP;
INFILE EXAMPLE1;
INPUT ID HEIGHT WEIGHT GENDER $ AGE;
PROC PRINT DATA=LISTINP;
TITLE 'Example 1 from an external file';
RUN;
- Note the following features of this code.
- The FILENAME assigns a fileref or nickname to the long file name that is used by the system.
The FILENAME statement defining the nickname must appear before the nickname is used.
- The INFILE statement in the Data step essentially says, "the next time an INPUT statement wants to read data, use the data in the file nicknamed EXAMPLE1."
The INFILE statement must appear before the INPUT statement that uses it.
- The INPUT statement, which does the actual work of reading the data, is the same as before.
- There is no DATALINES or CARDS statement.