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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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