Accessing External Files with SAS Statements

Overview of Accessing External Files with SAS Statements

This section presents simple examples of using the FILE, INFILE, and %INCLUDE statements to access external files. For more complex examples of using these statements under Windows, see Advanced External I/O Techniques .

Using the FILE Statement

The FILE statement enables you to direct lines that are written by a PUT statement to an external file. (footnote 1)
Here is an example using the FILE statement. This example reads the data in the SAS data set MYLIB.TEST and writes only those scores greater than 95 to the external file C:\MYDIR\TEST.DAT:
filename test "c:\mydir\test.dat";
libname mylib "c:\mydata";
data _null_;
   set mylib.test;
   file test;
   if score ge 95 then
      put score;
run;
The previous example illustrates writing the value of only one variable of each observation to an external file. The following example uses the _ALL_ option in the PUT statement to copy all variables in the current observation to the external file if the variable REGION contains the value west.
libname us "c:\mydata";
data west;
   set us.pop;
   file "c:\sas\pop.dat";
   where region="west";
   put _all_;
run;
This technique of writing out entire observations is particularly useful if you need to write variable values in a SAS data set to an external file so that you can use your data with another application that cannot read data in a SAS data set format.
Note: This example uses the _ALL_ keyword in the PUT statement. This code generates named output, which means that the variable name, an equal sign (=), and the variable value are all written to the file. For more information about named output, see the description of the PUT statement in SAS Statements: Reference.
The FILE statement also accepts several options. These options enable you to control the record format and length. Some of these options are illustrated in Advanced External I/O Techniques . For the complete syntax of the FILE statement, see FILE Statement: Windows .
The default record length that is used by the FILE statement is 32767 characters. If the data that you are saving contains records that are longer than 32767 characters, you must use the FILENAME statement to define a fileref and either use the LRECL= option in the FILENAME statement to specify the correct logical record length or specify the LRECL= option in the FILE statement. For details about the LRECL= option, see LRECL= in FILE Statement: Windows .
You can also specify a different value, instead of the default 32767, for LRECL= in an OPTIONS statement or in your configuration file. This value stays in effect during the entire session. If you want to specify a different LRECL= value for a specific step, then you must specify the value in a FILENAME, FILE, or INFILE statement.

Using the INFILE Statement

Use the INFILE statement to specify the source of data that is read by the INPUT statement in a SAS DATA step. The INFILE statement is always used in conjunction with an INPUT statement, which defines the location and type of data being read.
Here is a simple example of the INFILE statement. This DATA step reads the specified data from the external file and creates a SAS data set named SURVEY:
filename mydata "c:\mysasdir\survey.dat";
data survey;
   infile mydata;
   input fruit $ taste looks;
run;
You can use a quoted Windows filename instead of a fileref:
data survey;
   infile "c:\mysasdir\survey.dat";
   input fruit $ taste looks;
run;
The INFILE statement also accepts other options. These options enable you to control the record format and length. Some of these options are illustrated in Advanced External I/O Techniques . For the complete syntax of the INFILE statement, see INFILE Statement: Windows .
The default record length that is used by the INFILE statement is 32767 characters. If the data that you are reading has records longer 32767 characters, you must use the FILENAME statement to define a fileref and either use the LRECL= option in the FILENAME statement to specify the correct logical record length or specify the LRECL= option in the INFILE statement. For details about the LRECL= option, see LRECL= in INFILE Statement: Windows .
You can also specify a different value, instead of the default 32767, for LRECL= in an OPTIONS statement or in your configuration file. This value stays in effect during the entire session. If you want to specify a different LRECL= value for a specific step, then you would need to specify the value in a FILENAME, FILE, or INFILE statement.

Using the %INCLUDE Statement

When you submit an %INCLUDE statement, it reads an entire file into the current SAS program that you are running and submits that file to SAS immediately. A single SAS program can have as many individual %INCLUDE statements as necessary, and you can nest up to ten levels of %INCLUDE statements. Using the %INCLUDE statement makes it easier for you to write modular SAS programs.
Here is an example that submits the statements that are stored in C:\SAS\MYJOBS\PROGRAM1.SAS using the %INCLUDE statement and member-name syntax:
filename job "c:\sas\myjobs";
%include job(program1);
The %INCLUDE statement also accepts several options. These options enable you to control the record format and length. Some of these options are illustrated in Advanced External I/O Techniques . For the complete syntax of the %INCLUDE statement, see %INCLUDE Statement: Windows .
The default record length used by the %INCLUDE statement is 32767 characters. If the program that you are reading has records longer than 32767 characters, you must use the FILENAME statement to define a fileref and either use the LRECL= option in the FILENAME statement to specify the correct logical record length or specify the LRECL= option in the %INCLUDE statement. For details about the LRECL= option, see LRECL= in %INCLUDE Statement: Windows .
You can also specify a different value, instead of the default 32767, for LRECL= in an OPTIONS statement or in your configuration file. This value stays in effect during the entire session. If you want to specify a different LRECL= value for a specific step, then you would need to specify the value in a FILENAME, FILE, or INFILE statement.
FOOTNOTE 1:You can also use the FILE statement to direct PUT statement output to the SAS log or to the same destination as procedure output. For more information, see SAS Statements: Reference.[return]