Search code examples
sas

How to identify the last row of content using SAS file function?


I am using file I/O function to read the content of files. Is there a function or some quick way to identify the end of one file?

Test program is:

data _null_;
  set sashelp.class;
  file "class.txt";
  put _all_;
run;

data _null_;
  set sashelp.iris;
  file "iris.txt";
  put _all_;
run;

data have;
  path="class.txt";  output;
  path="iris.txt";   output;
run;

data want;
  set have;

  length content $1024.;
  filref=filename('_dummy_',path);
  filrc=fopen('_dummy_');
  if filrc then do;
    do while(fread(filrc)=0);
      rc=fget(filrc,content,1024);
      output;
    end;
    filrc=fclose(filrc);
  end;
run;

I want to derive a flag variable right in the data want step. This variable use 1 or 0 to flag out this row is the last row of a file or not. That is to say, this variable must have the same value with last.path in a new step:

data want1;
  set want;
  by path notsorted;
  flag=last.path;
run;

Note: The want dataset can be very big, that why I don't want a new data step.


Solution

  • Just read the files normally.

    data want;
      set have;
      fname=path;
      infile dummy filevar=fname end=eof truncover;
      do record=1 by 1 while (not eof);
        input content $char1024.;
        last_path=eof;
        output;
      end;
    run;