SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Reading Variable-Length Records
Reading Variable-Length Fields


The LENGTH= Option

Each record in the file referenced by the fileref Phonedat contains a value for an employee ID number, name, and phone extension.  The first and third fields (ID and PhoneExt) are always the same length in every record. However, the second field (Name) varies in length from record to record.

Raw Data File Phonedat
1---+----10---+----20
1802JOHNSON2123
1803BARKER2142
1804EDMUNDSON2325
1805RIVERS2543
1806MASON2646
1807JACKSON2049
1808LEVY2856
1809THOMAS2222
1810WARREN2003
1811VANDENBERGH2110
1812YEAGER2145
1813SMITH2025
1814HINES2243
1815TAYLOR2746


As you write the DATA step to read these values, remember that you must accommodate the varying length of Name in each individual record.

     data perm.phones; 
        infile phondat . . .;
But first, you need to determine the total length of each record. This is easily accomplished by using the LENGTH= option.


General form, INFILE statement with LENGTH= option:
INFILE file-specification LENGTH=variable;

where variable defines a numeric variable whose value is set to the length of the current record.

Note: 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.


Here, the length of each record is assigned to the variable reclen. As each record is read, the value of reclen changes.

     data perm.phones; 
        infile phondat length=reclen;
        input . . .;

Raw Data File Phonedat

1---+----10---+----20
1802JOHNSON2123
1803BARKER2142
1804EDMUNDSON2325
1805RIVERS2543
1806MASON2646
1807JACKSON2049
1808LEVY2856
1809THOMAS2222

>
>
>
>
>
>
>
>

reclen= 15
reclen= 14
reclen= 17
reclen= 14
reclen= 13
reclen= 15
reclen= 12
reclen= 14



back||next


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

Terms of Use & Legal Information | Privacy Statement