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
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
ok, 10x.
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;
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.