Chapter Contents

Previous

Next
SAS Component Language: Reference

Reading Values from External Files

Before an SCL program can use the information in an external file, the program must read the file's records. For example, an application may need to use external file records to do the following:

In order to read record values from the external file, your SCL program must first copy a record from the file into the FDB. Then it must copy the contents of the FDB into the SDV to make the values available to your SCL program. A value can be either part of a record or an entire record. Unless you must read values separately for some reason, reading an entire record as a single value is the easier technique to use. To complete the process of reading values from an open file, follow these steps:

  1. A record must be copied from the file to the FDB. Use the FREAD function to copy the values to the FDB, starting at the file's first record and reading each record sequentially.

  2. Each value in the record must be copied from the FDB to the SDV, where the value can be used by the application. Use the FGET function to copy the contents of the FDB (interpreted as values) to the SDV.


Order of Reading Records

Many types of external files can be read only sequentially, from the first record to the last. However, when a file supports random access, you can use SCL functions to change that sequence and either reread a particular record or start at the first record and reread the entire file. For more information, see Changing the Sequence of Reading Records.


Reading Record Values into the SDV

When the FGET function reads values from the FDB into the SDV, it makes the contents of the FDB available to the SCL program. You can control how this step processes the contents of the FDB by reading the FDB contents either as one single value or as a series of separate values. Reading the contents of the FDB as a single value can simplify your program. To do this, you can design a single control or field in the window to display the entire contents of the record. If you need to read record values into separate window variables, you can read the FDB contents as a single value into a program variable in the SDV. Then, you can use SAS character functions (like SCAN or SUBSTR) to assign parts of that program variable to window variables. The following example finds the length of a record and then reads the entire record as a single value into the window variable ROW.

length=finfo(fileid,'lrecl');
reclen=inputn(length,'best.');
rc=fget(fileid,row,reclen);
If ROW is a nonwindow variable instead of a window variable, then values that are read from the FDB are in the SDV, but they are not displayed in the window until they are assigned to a window variable.

Note:   The code in the preceding example is host specific. See the SAS documentation for your operating environment for more information.  [cautionend]

You determine whether the contents are treated as one value or as a series of values. There is a column pointer in the FDB that is set to 1 when the contents are read. By default, the FGET function copies the value from the current position of the column pointer to the next separator character. The default separator character is one blank. Therefore, the default action of the FGET function is to copy the value from the current position of the column pointer to the next blank. (To designate a different character as the separator character, use the FSEP function).

After each FGET function, the column pointer is positioned one column past the last character that was read. When the FDB contains no more values, the FGET function returns -1 to signal that it has reached the end of the FDB.


Reading Records as Separate Values

Reading the contents of the FDB as a series of separate values brings up a different set of considerations. Your applications must process a specified number of file values and display them in window variables of a particular size. Also, in order to read separate values, you need to know more about the external files that your application will process. You need to know how many values a record contains, and it is helpful if you know the starting columns for the values or the characters that separate the values. Finally, you need to define separate controls or fields to display the values.

When you read the FDB contents as separate values, you can locate these values by positioning the FDB column pointer at the column where the value begins or by specifying the character that separates these values. By default, the separator character for file records is a blank.


Identifying a Value's Starting Column

When you know the numbers of the columns where the values start, you can use the FPOS function to move the "read" pointer to the column where the next value begins. When the FPOS and FGET functions are used together, the FPOS function sets the starting column for the FGET function, which reads the FDB contents up to the next separator character unless a length is specified.

The following example shows how to read separate values when you know the numbers of the columns where the values start. This example reads record values into the variables NAME, HEIGHT, and WEIGHT by using the FPOS function to specify the position of the "read" pointer.

rc=fget(fileid,name,20);
rc=fpos(fileid,21);
rc=fget(fileid,height);
rc=fpos(fileid,28);
rc=fget(fileid,weight);


Chapter Contents

Previous

Next

Top of Page

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