BookmarkSubscribeRSS Feed
Loko
Barite | Level 11

Dear all,

How can prxchange function ne used to substitute variable a in table have A with 1 , B with 2, C with 3, D with 4.

I have been only able to group ([ABCD]) the searched characters , but not able to determine the substitute pattern as described above.

I know I can do it by using transtrn function but I am interested in using pearl functions.

data have;
a='2A345';output;
a='2B134';output;
a='3C124';output;
a='4D123';output;
run;

data want;
set have;
b=prxchange('s/[ABCD]/1234/i',1,a);
run;

10x

4 REPLIES 4
Ksharp
Super User

No. You can't do that with prxchange() .use translate() instead.

data have;
a='2A345';output;
a='2B134';output;
a='3C124';output;
a='4D123';output;
run;
data want;
set have;
b=translate(a,'1234','ABCD');
run;

Xia Keshan

Loko
Barite | Level 11

ok, 10x.

FriedEgg
SAS Employee

If you really wanted to accomplish this with regular expressions (say your real data is much more complex than this data) then an approach to use could be similar to the following:

data have;

a='2A345';output;

a='2B134';output;

a='3C124';output;

a='4D123';output;

run;

data want;

array r[4] $1 _temporary_ ('1' '2' '3' '4');

prxid = prxparse('/(A)|(B)|(C)|(D)/io');

do until(done);

set have end=done;

pos = prxmatch(prxid, a);

if pos then do;

idx = prxparen(prxid);

substr(a, pos, 1) = r[idx];

end;

output;

end;

run;

art297
Opal | Level 21

And, if you only need to change the 2nd character and its always an uppercase letter, then you could use:

data want;

  set have;

  substr(a,2,1)=rank(substr(a,2,1))-64;

run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1561 views
  • 3 likes
  • 4 in conversation