Relpacing every 5th character with X

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 87
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: 7,465

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

View solution in original post


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

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
Respected Advisor
Posts: 4,138

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
  • 175 views
  • 2 likes
  • 3 in conversation