Reversing the contents of a mainframe file

Reply
Occasional Contributor kdm
Occasional Contributor
Posts: 11

Reversing the contents of a mainframe file

How do I reverse the contents of a variable byte mainframe file using SAS. 

The contents of the file are

tom

john

tim

kim

sam

After reversing the file looks like this

sam

kim

tim

john

tom

Super Contributor
Posts: 358

Re: Reversing the contents of a mainframe file

Hi:

You just build a counter for the file when you read the records:

if _n_ = 1 then counter = 0;

counter + 1;

then sort the data by descending counter.

Occasional Contributor kdm
Occasional Contributor
Posts: 11

Re: Reversing the contents of a mainframe file

The suggestion looks good. But my intention is not to read the entire record. I just use INFILE to write out the record in the same data step where the read happens. I have the following coded. It throws an error saying that infile cannot find the input file.

 

DATA ANBTREC;

  INFILE ANMISFL;

  INPUT RECTYPE $CHAR2.; /* RECORD TYPE CODE */

  IF _N_ = 1 THEN COUNTER = 0;

  COUNTER + 1;

  OUTPUT ANBTREC;

  PROC SORT DATA=ANBTREC;

   BY COUNTER;

RUN;

DATA _NULL_;

  SET ANBTREC;

  FILE FILEOUT;

  PUT _INFILE_;

RUN;

Super User
Posts: 5,071

Re: Reversing the contents of a mainframe file

_INFILE_ no longer exists once PROC SORT begins.  But one-step solutions will require holding all the data in memory.  What are the dimensions (how wide is each line, how many lines)?

Occasional Contributor kdm
Occasional Contributor
Posts: 11

Re: Reversing the contents of a mainframe file

Its a variable byte file with a length of 4045.

Number of lines can be 500 thousand to 1 million lines

Super User
Posts: 5,071

Re: Reversing the contents of a mainframe file

You'll probably need about 5G of memory for this to work.  There are potential holes in the logic that I won't have time to test (see below).

data _null_;

infile source end=done;

array lines {1000000} $4045 _temporary_;

array linelen {1000000} _temporary_;

do until (done);
   recno + 1;

   input;

   linelen{recno}=lengthn(_infile_);

   lines{recno}=_infile_;

end;

* All lines now read into memory.  Write them back out;

file result;

do _n_=recno to 1 by -1;

   put lines{recno};

end;

run;

As is, this will write out the full 4,045 characters for each line.  I wish I had time to see how to control that to write out variable length lines, but I don't think I'll get to that today.  Also, it is possible that _infile_ gets set incorrectly with this code.  You may need to switch to:

input @;

linelen=..

lines=..

input;

But at least this is a starting point for getting what you're looking for.

Good luck.

Respected Advisor
Posts: 3,124

Re: Reversing the contents of a mainframe file

Is this what you want?

data have;

input name $4.;

cards;

tom

john

tim

kim

sam

;

data want;

  do n=nobs by -1 to 1;

  set have nobs=nobs point=n;

output;

  end;

stop;

run;

proc print;run;

Haikuo

Ask a Question
Discussion stats
  • 6 replies
  • 271 views
  • 0 likes
  • 4 in conversation