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.
@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 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;
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.
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.
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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.