DATA Step, Macro, Functions and more

Appending to an external file with a "trailer record"

Reply
Contributor
Posts: 21

Appending to an external file with a "trailer record"

I need to append data to an external file which has a "trailer record".   That means that what I want to "append" really needs to be inserted just before that record. 
It's not HTML - but imagine if you needed to update an HTML file by inserting records prior to the </BODY> tag.  It's a similar concept.

What I've come up with so far is kludgy:

1. Using DATA_NULL_,  INFILE referencing the original, and FILE referencing a temporary file,  copy all of the records except the trailer to a temporary file.

2. Using DATA_NULL_ and FILE referencing the temporary file with MOD, write the new records and then a trailer record
3. Using DATA _NULL_, INFILE referencing the temporary file and FILE referencing the original, copy all of the (updated) records from temporary to original

There's got to be a better way?  And hopefully without risk of losing the records (although I will build in a backup)

Environment note:  this is a text-type file within a Unix directory on a z/OS system.

Thanks for any help.

Super Contributor
Posts: 282

Re: Appending to an external file with a "trailer record"

Hi,

What about:

1. Read original file into a SAS data set

2. Use set statement with end= option to write the data to the original file inserting the new records when the end= variable is 1, followed by the trailer.

Regards,

Amir.

Super Contributor
Posts: 282

Re: Appending to an external file with a "trailer record"

Hi,

Actually, you should be able to just do it in one data step by reading the external file with the end= option writing out to the same external file. When the end= variable is 1 write the extra records followed by the trailer.

Regards,

Amir.

Super Contributor
Posts: 282

Re: Appending to an external file with a "trailer record"

Hi,

An untested template example of my last suggestion:

data _null_;

  file 'your-file-name';

  infile 'your-file-name' end=lastrec;

  input;

  if lastrec then

  do;

    put 'new-rec-1';

    put 'new-rec-2';

    put 'new-rec-3';

  end;

  put _infile_;

run;

Regards,

Amir.

Contributor
Posts: 21

Re: Appending to an external file with a "trailer record"

Thanks, your last example works

Ask a Question
Discussion stats
  • 4 replies
  • 239 views
  • 0 likes
  • 2 in conversation