I'm in a RHEL environment (migrating from 6.9 to 8.1) running SAS 9.4m7. I've been struggling with a specific application of the FWRITE() function, attempting to overwrite an existing record of a file. Here's a simplified example: %macro overlay ; %local rc ; %let rc = %sysfunc(filename(homesas,/home/sas/testfwrite.txt)) ; %let fid1=%sysfunc(fopen(homesas, O, 0, P)) ; /* create header record */ %let rc=%sysfunc(fput(&fid1, %sysfunc(putc(%str(The Three Stooges), $, 41)))) ; %let rc=%sysfunc(fwrite(&fid1)) ; /* The Original Act */ %let rc=%sysfunc(fput(&fid1, %sysfunc(putc(%str(Shemp Larry and Moe), $, 19)))) ; %let rc=%sysfunc(fwrite(&fid1)) ; /* In 1932, Shemp left and Curly joins */ %let rc=%sysfunc(fput(&fid1, %sysfunc(putc(%str(Curly), $, 5)))) ; %let rc=%sysfunc(fwrite(&fid1,+)) ; /* Shemp returned to replace Curly in 1946 */ %let rc=%sysfunc(fput(&fid1, %sysfunc(putc(%str(Shemp), $, 5)))) ; %let rc=%sysfunc(fwrite(&fid1, +)) ; %let rc=%sysfunc(fclose(&fid1)) ; %mend overlay ; %overlay ; When I execute a standard cat command on the file, I get: > cat /home/sas/testfwrite.txt The Three Stooges Shemp Larry and Moe But when I run cat -e to show hidden characters: > cat -e /home/sas/testfwrite.txt The Three Stooges$ Shemp Larry and Moe^MCurly^MShemp$ The expected output (after processing overlays) where "Shemp" at the beginning was replaced with "Curly" and then "Curly" was replaced with "Shemp", should look like this: The Three Stooges Shemp Larry and Moe I've tried different file opening modes: Standard mode: %let fid1=%sysfunc(fopen(homesas, O, 0, P)) ; Binary mode: %let fid1=%sysfunc(fopen(homesas, O, 0, B)) ; No record structure: %let fid1=%sysfunc(fopen(homesas, O, 0, P RECFM=N)) ; None of these approaches resulted in the overlays being processed as expected. The carriage returns (^M) are being written to the file, but they're not causing the text to be overwritten when the file is read. The question is: How can I effectively overlay a line of a file using the FWRITE() function so that when the file is read later, the overlays are processed correctly?
... View more