BookmarkSubscribeRSS Feed
GeorgeSAS
Lapis Lazuli | Level 10

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!

9 REPLIES 9
Reeza
Super User

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?

GeorgeSAS
Lapis Lazuli | Level 10
Windows,how to do that?
Reeza
Super User

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.

art297
Opal | Level 21

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

 

Reeza
Super User

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

art297
Opal | Level 21

@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

 

Reeza
Super User

@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.

GeorgeSAS
Lapis Lazuli | Level 10
I agree with both of you!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 2838 views
  • 4 likes
  • 4 in conversation