DATA Step, Macro, Functions and more

update an exist text file

Reply
Highlighted
Regular Contributor
Posts: 229

update an exist text file

[ Edited ]

Hello everyone,

 

I can use data _null_; file filename to update/overwrite an txt file

 

also I can use mod option  to append to text file.here is the code:

 

filename old "c:\temp\test\old";
data _null_;
file old;
put 'this is row 1';
put 'this is row 2';
put 'this is row 3';
put 'this is row 4';
put 'this is row 5';
put 'this is row 6';
put 'this is row 7';
put 'this is row 8';
put 'this is row 9';
put 'this is row 10';
run;
 
data a;
new="I want row number 6 to be updated";
run;

data _null_;
set a;
file old mod ;
put new;
run;

the result of old is:

 

this is row 1
this is row 2
this is row 3
this is row 4
this is row 5
this is row 6
this is row 7
this is row 8
this is row 9
this is row 10
I want row number 6 to be updated

 

 

 

 

My question is I don't want string new append on the old file, I want the string new update the 6th row of old file.

 

the result should like this:

 

this is row 1
this is row 2
this is row 3
this is row 4
this is row 5
I want row number 6 to be updated
this is row 7
this is row 8
this is row 9
this is row 10

 

 

Please help!

 

Thanks!

Super User
Posts: 18,580

Re: update an exist text file

What's your OS? It's best, IMO, to use either Unix or Windows line commands to do this rather than to use SAS. 

And do you have XCMD enabled?

Regular Contributor
Posts: 229

Re: update an exist text file

Windows,how to do that?
Super User
Posts: 18,580

Re: update an exist text file

Search "Powershell replace line item in text file" for starters. I had this issue and posted this question earlier this year, so if you can search my questions you should see a similar answer on here.

PROC Star
Posts: 7,416

Re: update an exist text file

You could do it with just using SAS. e.g.:

 

filename old "c:\temp\test\old.txt";
data _null_;
  file old;
  put 'this is row 1';
  put 'this is row 2';
  put 'this is row 3';
  put 'this is row 4';
  put 'this is row 5';
  put 'this is row 6';
  put 'this is row 7';
  put 'this is row 8';
  put 'this is row 9';
  put 'this is row 10';
run;
 
data a;
  new="I want row number 6 to be updated";
run;

data have;
  infile old dsd truncover;
  input text $100.;
run;

data _null_;
  set have;
  if _n_ eq 1 then set a;
  if _n_ eq 6 then text=new;
  file old;
  put text;
run;

Art, CEO, AnalystFinder.com

 

Super User
Posts: 18,580

Re: update an exist text file

@art297 that requires you to process the entire file, the command line tools are more powerful, but it is a different language...

PROC Star
Posts: 7,416

Re: update an exist text file

@Reeza: I agree! But, for a small file requiring such a minor one-time change, I think the learning curve would cost more than the processing time required by the method I suggested.

 

Art, CEO, AnalystFinder.com

 

Super User
Posts: 18,580

Re: update an exist text file


art297 wrote:

@Reeza: I agree! But, for a small file requiring such a minor one-time change, I think the learning curve would cost more than the processing time required by the method I suggested.

 

Art, CEO, AnalystFinder.com

 


100% agree, but there's also the logic if you never take the time to learn it, it will never be the faster method. Up to the OP to make that decision really at the end of the day. I wish I'd spent more time learning command line tools at the beginning of my career, they're the fast for manipulating text files and really powerful.

Regular Contributor
Posts: 229

Re: update an exist text file

I agree with both of you!
PROC Star
Posts: 1,630

Re: update an exist text file

Ask a Question
Discussion stats
  • 9 replies
  • 150 views
  • 4 likes
  • 4 in conversation