SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Reading Variable-Length Records
Lesson Summary

This page contains

I. Text Summary

To go to the page where a task, programming feature, or concept was presented, select a link.


Record Formats
A record format specifies the characteristics of the organization of records in a file. Some operating systems have two types of record formats:
  • fixed-length records
  • variable-length records.

External files that have fixed-length records have an end-of-record marker after a predetermined number of columns. A typical record length is 80 columns. External files that have variable-length records have an end-of-record marker after the last field. When records contain fields of varying widths or a varying number of fields, variable-length records can use space more efficiently.

Variable-Length Records That Have a Varying Field Width
The length of a record may vary because the length of a specific field varies from record to record. To read a file that has a variable-length field, a DATA step should include statements to
  1. determine the total length of each record by using the LENGTH= option
  2. hold the first record by using a single trailing @ line-hold specifier
  3. create a length-variable for the variable-length field
  4. use the $VARYINGw. informat to read the values for the variable-length field.

Variable-Length Records That Have a Varying Number of Fields
Records in an external file may contain a varying number of fields. The first three lines of the DATA step that are used to read a file that contains a varying number of fields are similar to those that are used to read a file that contains a variable-length field. You need to
  1. determine the total length of each record by using the LENGTH= option
  2. hold the first record by using a single trailing @ line-hold specifier
  3. create a length variable for the variable-length field

    The DATA step should also include statements to
  4. read the repeating blocks of data by using an iterative DO loop.

The LENGTH= Option
A LENGTH= option in the INFILE statement creates a numeric variable whose value is the length of the current record. Although the LENGTH= option is specified in the INFILE statement, the variable is not assigned a value until an INPUT statement is executed, and the SAS System determines the length of the record that is being read.

The $VARYINGw. Informat
The $VARYINGw. informat is a special SAS informat that enables you to read a character value that has a different length from record to record. Before you can specify the $VARYINGw. informat, you must define the value of the length-variable.

II. Syntax

To go to the page where a statement or option was presented, select a link.

Variable-Length Records That Have a Varying Field Width

LIBNAME libref  'SAS-data-library';
FILENAME fileref 'filename';
DATA SAS-data-set;
        INFILE raw-data-file LENGTH=total-length-variable;
        INPUT variable @;
                     variable-length-variable=total-length-variable - value;
        INPUT variable
                   $VARYINGw. variable-length-variable variable(s);
RUN;

Variable-Length Records That Have a Varying Number of Fields

LIBNAME libref  'SAS-data-library';
FILENAME fileref 'filename';
DATA SAS-data-set;
        INFILE raw-data-file LENGTH=total-length-variable;
        INPUT variable @;
        DO index-variable specification;
              INPUT variable(s) @;
              OUTPUT;
      END;
RUN;


III. Sample Programs

Variable-Length Records That Have a Varying Field Width

     libname perm 'c:\records\phone';
     filename phonedat 'c:\records\phone\new.dat';
     data perm.phones;
        infile phonedat length=reclen;
        input ID 4. @;        
        namelen=reclen-8;   
        input Name $varying10. namelen
              PhoneExt;
     run;


Variable-Length Records That Have a Varying Number of Fields

     libname perm 'c:\records\patient';
     filename bpdat 'c:\records\patient\bp.dat';
     
     data perm.health; 
        infile bpdata length=reclen;
        input ID 4. @;
        do index=6 to reclen by 15;
           input Date : date7. BP $ @;
           output;
        end;
     run;


IV. Points to Remember
  • Although the LENGTH= option is specified in the INFILE statement, the variable is not assigned a value until an INPUT statement is executed and the SAS System determines the length of the record being read.

  • You must define the value of the length-variable before you specify the $VARYINGw. informat.

  • When using the $VARYINGw. informat, remember to specify a w value that is large enough to accommodate the longest value.



back||next

 

Copyright © 2002 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Terms of Use & Legal Information | Privacy Statement