SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Reading Variable-Length Records
Reading Variable-Length Fields


Notice that the w value that is specified in the informat is the maximum length of Name and not the actual number of columns read.

     data perm.phones; 
        infile phondat length=reclen;
        input ID 4. @;
        namelen=reclen-8;
        input Name $varying10. namelen 
              PhoneExt;
When the DATA step executes for the first time, the record length is 15 so the value of namelen is 15 - 8, or 7. Because the value of namelen is 7, only 7 columns are read.

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

Program Data Vector

But suppose there is a later record where the value of namelen exceeds the w value specified in the $VARYINGw. informat. For example, in this record the value of namelen is 11. However, the maximum length of the variable Name is 10, so only 10 columns are read and the last character is truncated.

The missing character, H, is then read as part of PhoneExt, which is a numeric variable. Therefore, the value of of PhoneExt is invalid and set to missing in the Program Data Vector.

    |    11    |
1---+----10---V----20
1811VANDENBERGH2110
1812YEAGER2145
1813SMITH2025
1814HINES2243

Program Data Vector

As you can see, it's important to specify a w value that is large enough to accommodate the longest value.

     data perm.phones; 
        infile phondat length=reclen;
        input ID 4. @;
        namelen=reclen-8;
        input Name $varying12. namelen 
              PhoneExt;

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



back||next


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

Terms of Use & Legal Information | Privacy Statement