Relpacing every 5th character with X

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 89
Accepted Solution

Relpacing every 5th character with X

Hello,

can anyone help

I have to replace every 5th character with X

Data test;

input desc $ 25.;

datalines;

THE CAT SIT ON THE MAT

;

run;

i want the output as every 5th character should be replace with X

space is also considered

DESC

THE XAT SXT ONXTHE XAT


Accepted Solutions
Solution
‎12-15-2014 01:25 AM
Super User
Posts: 9,294

Re: Relpacing every 5th character with X

In the data step do:

i = 5;

do until (i > length(desc));

  substr(desc,i,1) = 'X';

  i = i + 5;

end;

drop i;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Solution
‎12-15-2014 01:25 AM
Super User
Posts: 9,294

Re: Relpacing every 5th character with X

In the data step do:

i = 5;

do until (i > length(desc));

  substr(desc,i,1) = 'X';

  i = i + 5;

end;

drop i;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Respected Advisor
Posts: 4,462

Re: Relpacing every 5th character with X

One way to go:

Data test;

  input desc $ 25.;

  want=prxchange('s/(.{4})./\1X/oi',-1,desc);

  datalines;

THE CAT SIT ON THE MAT

;

run;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 187 views
  • 2 likes
  • 3 in conversation