BookmarkSubscribeRSS Feed
TimH
Quartz | Level 8

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.

4 REPLIES 4
Amir
PROC Star

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.

Amir
PROC Star

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.

Amir
PROC Star

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.

TimH
Quartz | Level 8

Thanks, your last example works

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1838 views
  • 0 likes
  • 2 in conversation