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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

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
  • 4 replies
  • 930 views
  • 3 likes
  • 4 in conversation