DATA Step, Macro, Functions and more

rc=-20009 WARNING: Truncated record. rc=20026 ERROR: Undetermined I/O failure.

Reply
Occasional Contributor
Posts: 13

rc=-20009 WARNING: Truncated record. rc=20026 ERROR: Undetermined I/O failure.

Greetings.

 

This post is actually a solution to a problem. I can't find any appropriate information about this issue, so it may be helpful for someone in the future.

 

I was doing a file reading stuff using FDB and caught the issue which I couldn't find a way to solve. But in the end, I figured out that the issue was with FILENAME function, specifically with the length of a variable which I was passing to the second argument - filename. 

 

Affected SAS version and systems:

 

  1. SAS 9.2 Unix HP UX. &SYSLONG4=9.02.02M3P04132010, &SYSSCP=HP IPF.
    Error reference rc=-20009 WARNING: Truncated record.
  2. SAS 9.2 Linux. &SYSLONG4=9.02.02M3P04132010, &SYSSCP=LIN X64.
    Error reference rc=20026 ERROR: Undetermined I/O failure.

My task was to open a file using FOPEN, then read the file till the end of file (EOF) and then assign the pointer to the beginning of the file (basically, reopen the file) using FREWIND function and read it again. Since I was working on Unix, I assumed the maximum possible path coming from an external source has 4096 length (which is considered as the programmatic limit for Unix/Linux). And that was a huge mistake and made me spend around 1 day of debugging. So, the file reading was ok, but at the end of the file I was getting the return code -20009 or 20026 (depending on the system, see affected sas version list) instead of expected rc=-1 WARNING: End of file. And the problem was that FREWIND doesn't work. Later I identified that the issue was with the variable length where I stored the path, so when I declared a file reference using FILENAME function I did that in a next way:

 

length fileref $8 filepath $4096;
fileref = "fref1";
filepath = "&file";
rc = filename(fileref, filepath);

And everything was ok, no issues in return code, no errors or warnings. But as I said issues were when I reached the end of a file. As I identified afterward you must use a variable with the length no-longer than 1025 (1026 doesn't work). Also, I couldn't find any notes in SAS Documentation about this.

 

Interesting note, that on SAS 9.4 Linux everything works fine, and you can even use a variable with the length more than 4096.

 

The code to reproduce the issue (change the file parameter to an arbitrary textfile in your system)

 

%macro _frewind(l, file=/etc/passwd);
    %macro __report(msg=%str(Unseccessfully));
        if rc=0 then do;
            put 'DBG: Successfully';
        end;
        else do;
            msg = sysmsg();
            put "ERR" "OR: &msg: rc=" rc 'message=' msg;
        end;
    %mend;
    
    data debug_&l;
        length v $32767;

        length fileref $8 filepath $&l;

        fileref = "fref1";
        filepath = "&file";
        put 'DBG: Trying to assign a fileref';
        rc = filename(fileref, filepath);
        %__report;
        put 'DBG: Trying to open the file';
        fileid = fopen(fileref);
        %__report;

        put 'DBG: READ file with path 1025 length';

        length v $32767;
        
        counter = 0;
        do while (rc ne -1 and counter < 10);
           /* Read a record. */
           rc=fread(fileid);
           select(rc);
               when(0) do;
                   put 'DBG: Observation was read successfully';
                   rc = fget(fileid, v, 32767);
               end;
               when(-1) do;
                   put 'DBG: End of file reached';
               end;
               otherwise do;
                   %__report(msg=Error upon attempt to read the file)
                   counter = counter + 1;
               end;
           end;
           output;
        end;

        put 'DBG: Trying to reposition the pointer at the beginning of the file';
        if rc= -1  then rc=frewind(fileid);
        %__report;
        put 'DBG: Read the first record';
        rc=fread(fileid);
        %__report;
        rc=fget(fileid,v,32767);
        %__report;
        put 'DBG: Trying to close the file';
        rc=fclose(fileid);
        %__report;
        put 'DBG: Trying to deassign the fileref';
        rc=filename(fileref,'');
        %__report;
    run;    
%mend _frewind;

/* 1025 length works */
%_frewind(1025)
/*! 1026 length does not work */
%_frewind(1026)

Kind regards,
Igor

 

 

 

PROC Star
Posts: 2,319

Re: rc=-20009 WARNING: Truncated record. rc=20026 ERROR: Undetermined I/O failure.

You could contact tech support so that they create a Usage Note and check that all is fine on all 9.4 plaforms.

Super User
Super User
Posts: 7,938

Re: rc=-20009 WARNING: Truncated record. rc=20026 ERROR: Undetermined I/O failure.

I cannot get it to fail using SAS 9.4, even if I use a variable defined longer than $4096.

SAS version 9.2 is over 7 years old.  http://blogs.sas.com/content/iml/2013/08/02/how-old-is-your-version-of-sas-release-dates-for-sas-sof...

Perhaps it is time to switch to a more recent version of SAS?

Ask a Question
Discussion stats
  • 2 replies
  • 186 views
  • 0 likes
  • 3 in conversation