## Relpacing every 5th character with X

Solved
Frequent Contributor
Posts: 89

# 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: 10,270

## 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

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

## 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
Posts: 4,736

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