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
* 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;
... View more