Chapter Contents

Previous

Next
SAS Companion for the OS/2 Environment

Accessing External Files with SAS Statements

This section presents examples of using the FILE, INFILE, and %INCLUDE statements to access external files. The following examples are simple. For more complex examples of using these statements under OS/2, 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)

The following is a simple example that uses the FILE statement. This example reads the data in the SAS data set MYLIB.TEST and writes only those scores that are 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. Consider this when you are reading the data later. For more information about named output, see the description of the PUT statement in SAS Language Reference: Dictionary.  [cautionend]

The FILE statement also accepts several options. These options enable you, among other things, 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.

Note:   The default record length that is used by the FILE statement is 256 characters. If the data that you are saving have records longer than this, 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.  [cautionend]


Using the INFILE Statement

The INFILE statement is used to specify the source of data that are read by the INPUT statement in a SAS DATA step. The source can be either a text file. The INFILE statement is always used in conjunction with an INPUT statement, which defines the location, order, and type of data being read.

The following 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 that is named SURVEY:

filename mydata "c:\mysasdir\survey.dat";
data survey;
   infile mydata;
   input fruit $ taste looks;
run;

Of course, you can use a quoted OS/2 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, among other things, 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.

Note:   The default record length that is used by the INFILE statement is 256 characters. If the data that you are reading have records that are longer than this, 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.  [cautionend]


Using the %INCLUDE Statement

When you submit a %INCLUDE statement, it reads an entire file into the current SAS program that you are running and submits that file to the SAS System 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 by 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, among other things, 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 Using the %INCLUDE Statement.

Note:   The default record length that is used by the %INCLUDE statement is 256 characters. If the program that you are reading has records longer than this, 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 FILE.  [cautionend]


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 Language Reference: Dictionary[arrow]


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.