<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Writing to specific lines of a program in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Writing-to-specific-lines-of-a-program/m-p/701787#M214923</link>
    <description>&lt;P&gt;You can use an ordered hash to maintain the lines read in from a file.&amp;nbsp; Modify the hash and write the information back out to the same file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;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;&lt;/PRE&gt;
&lt;P&gt;Full program with macros&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;* 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 &amp;amp;hash.(ordered:'A');
  &amp;amp;hash..defineKey('index');
  &amp;amp;hash..defineData('line');
  &amp;amp;hash..defineDone();
  call missing (index, line);

  fid = fopen ("&amp;amp;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 
        &amp;amp;hash..add(key:index, data:line);

      %if &amp;amp;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("&amp;amp;fileref","O");
  msg = sysmsg();

  if fid ne 0 then do;
    declare hiter iter("&amp;amp;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;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;</description>
    <pubDate>Thu, 26 Nov 2020 09:48:23 GMT</pubDate>
    <dc:creator>RichardDeVen</dc:creator>
    <dc:date>2020-11-26T09:48:23Z</dc:date>
    <item>
      <title>Writing to specific lines of a program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Writing-to-specific-lines-of-a-program/m-p/701543#M214834</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Nov 2020 14:54:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Writing-to-specific-lines-of-a-program/m-p/701543#M214834</guid>
      <dc:creator>Brian3</dc:creator>
      <dc:date>2020-11-25T14:54:07Z</dc:date>
    </item>
    <item>
      <title>Re: Writing to specific lines of a program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Writing-to-specific-lines-of-a-program/m-p/701545#M214835</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/274283"&gt;@Brian3&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Your description is a little muddled.&amp;nbsp; Are you talking about running a data step to modify an existing text file?&amp;nbsp; What does this file contain? How is it structured?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To modify a file make a COPY of the file.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  infile 'original file';
  file 'new file';
  input;
  if _n_=1 then put 'hello';
 else put _infile_;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 Nov 2020 14:58:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Writing-to-specific-lines-of-a-program/m-p/701545#M214835</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-11-25T14:58:54Z</dc:date>
    </item>
    <item>
      <title>Re: Writing to specific lines of a program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Writing-to-specific-lines-of-a-program/m-p/701549#M214837</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for replying,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, what I have at the moment is&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data _null_;&lt;/P&gt;
&lt;P&gt;file "original" mod;&lt;/P&gt;
&lt;P&gt;put "hello";&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but I want to be able to write the "hello" at any specific line of the code.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Nov 2020 15:06:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Writing-to-specific-lines-of-a-program/m-p/701549#M214837</guid>
      <dc:creator>Brian3</dc:creator>
      <dc:date>2020-11-25T15:06:41Z</dc:date>
    </item>
    <item>
      <title>Re: Writing to specific lines of a program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Writing-to-specific-lines-of-a-program/m-p/701573#M214846</link>
      <description>&lt;P&gt;The option MOD in a FILE statement causes new lines to be appended. &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;has provided the solution, as you cannot open a file simultaneously for input and output.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Nov 2020 16:30:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Writing-to-specific-lines-of-a-program/m-p/701573#M214846</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-25T16:30:26Z</dc:date>
    </item>
    <item>
      <title>Re: Writing to specific lines of a program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Writing-to-specific-lines-of-a-program/m-p/701787#M214923</link>
      <description>&lt;P&gt;You can use an ordered hash to maintain the lines read in from a file.&amp;nbsp; Modify the hash and write the information back out to the same file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;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;&lt;/PRE&gt;
&lt;P&gt;Full program with macros&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;* 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 &amp;amp;hash.(ordered:'A');
  &amp;amp;hash..defineKey('index');
  &amp;amp;hash..defineData('line');
  &amp;amp;hash..defineDone();
  call missing (index, line);

  fid = fopen ("&amp;amp;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 
        &amp;amp;hash..add(key:index, data:line);

      %if &amp;amp;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("&amp;amp;fileref","O");
  msg = sysmsg();

  if fid ne 0 then do;
    declare hiter iter("&amp;amp;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;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;</description>
      <pubDate>Thu, 26 Nov 2020 09:48:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Writing-to-specific-lines-of-a-program/m-p/701787#M214923</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-11-26T09:48:23Z</dc:date>
    </item>
  </channel>
</rss>

