BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Brian3
Obsidian | Level 7

Hi,

 

I have a SAS program. Using the file mod and put statements I can append lines to the end of this program. But say I want to write "hello" to line 1 or line 30 or any specific line of the code. How would I go about doing that?

 

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@Brian3 wrote:

Hi,

 

I have a SAS program. Using the file mod and put statements I can append lines to the end of this program. But say I want to write "hello" to line 1 or line 30 or any specific line of the code. How would I go about doing that?

 

Thanks.


Your description is a little muddled.  Are you talking about running a data step to modify an existing text file?  What does this file contain? How is it structured?

 

To modify a file make a COPY of the file.

data _null_;
  infile 'original file';
  file 'new file';
  input;
  if _n_=1 then put 'hello';
 else put _infile_;
run;

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

@Brian3 wrote:

Hi,

 

I have a SAS program. Using the file mod and put statements I can append lines to the end of this program. But say I want to write "hello" to line 1 or line 30 or any specific line of the code. How would I go about doing that?

 

Thanks.


Your description is a little muddled.  Are you talking about running a data step to modify an existing text file?  What does this file contain? How is it structured?

 

To modify a file make a COPY of the file.

data _null_;
  infile 'original file';
  file 'new file';
  input;
  if _n_=1 then put 'hello';
 else put _infile_;
run;
Brian3
Obsidian | Level 7

 

Thanks for replying,

 

It's any program (say it contains a list of marco variable definitons). Its a .sas file if that's what you mean by text.

 

Yes, what I have at the moment is

 

data _null_;

file "original" mod;

put "hello";

run;

 

but I want to be able to write the "hello" at any specific line of the code.

Kurt_Bremser
Super User

The option MOD in a FILE statement causes new lines to be appended. @Tom has provided the solution, as you cannot open a file simultaneously for input and output. 

RichardDeVen
Barite | Level 11

You can use an ordered hash to maintain the lines read in from a file.  Modify the hash and write the information back out to the same file.

 

Example:

data _null_;
  %textfile_to_hash(settings,lines);

  rc = lines.remove(key: 1);
  lines.replace(key: 5, data: 'parameter5 = 555;');
  lines.replace(key:10, data: 'parameter10 = 4096');
  lines.replace(key:12, data: 'parameter12 = 121212');

  %hash_to_textfile(lines,settings);
run;

Full program with macros

Spoiler
* Create a sample text file;
filename settings 'parameters.sas';

data _null_;
  file settings;
  do row = 1 to 10;
    put 'parameter' row ' = ' row ';';
  end;
run;

data _null_;
  infile settings;
  input; put _infile_;
run;

%macro textfile_to_hash(fileref,hash,log=0);
  length index 8 line $256;

  declare hash &hash.(ordered:'A');
  &hash..defineKey('index');
  &hash..defineData('line');
  &hash..defineDone();
  call missing (index, line);

  fid = fopen ("&fileref");
  msg = sysmsg();

  if fid ne 0 then do;
    do index = 1 by 1 while (fread(fid)=0);
      if fget(fid,line,256)=0 then 
        &hash..add(key:index, data:line);

      %if &log %then
        %str(put index 2. ': ' line;  * log the file data;);
    end;
    fid = fclose(fid);
  end;
  else
    put msg;
%mend;

%macro hash_to_textfile(hash,fileref);
  fid = fopen("&fileref","O");
  msg = sysmsg();

  if fid ne 0 then do;
    declare hiter iter("&hash");
    do while (iter.next()=0);
      rc1 = fput(fid,line);
      msg = sysmsg();
      if rc1 = 0 then rc2 = fwrite(fid);
      if rc1 or rc2 then putlog msg;
    end;
  end;
%mend;

data _null_;
  %textfile_to_hash(settings,lines);

  rc = lines.remove(key: 1);
  lines.replace(key: 5, data: 'parameter5 = 555;');
  lines.replace(key:10, data: 'parameter10 = 4096');
  lines.replace(key:12, data: 'parameter12 = 121212');

  %hash_to_textfile(lines,settings);
run;

data _null_;
  infile settings;
  input; put _infile_;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1447 views
  • 2 likes
  • 4 in conversation