BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
yashpande
Obsidian | Level 7

Is there a way I can use  multiple prxchange in one go. i want to replace a with d, b with k, c with l, d with a , 

Here is  my input 

data a;

input_string="abcdjhj";

input_num=12;

input_string2= "robin";

run;

 

 

data b;

set a;

changed_string=prxchange('s/a/d/',-1,input_string);

changed_string=prxchange('s/b/k/',-1,input_string);

changed_string=prxchange('s/c/l/',-1,input_string);

changed_string=prxchange('s/d/a/',-1,input_string);

run;

 

as per  logic i want changed_string value to be "ddklajhj" but this is not producing correct output and my table b has 16 such character variables i want to apply the same logic for those variables also. Can someone help me with the same

 

Thanks in advance. Any help is appreciated

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

First a question, in your real data are you changing single letters or longer strings?

 

Second this "want changed_string value to be "ddklajhj" " does not match the description " replace a with d, b with k, c with l, d with a"  If we are starting with "abcdjhj" which is 7 characters we don't get "ddklajhj" which is 8 characters.

 

Assuming your are replacing single characters and that you have a typo in the changed string above:

 data example;
    input_string="abcdjhj";
    newstring = translate(input_string,"dkla","abcd");
 run;

TRANSLATE function is only single character replacements which is why I asked. If you have a double-byte character set the function would be KTRANSLATE.

View solution in original post

3 REPLIES 3
ballardw
Super User

First a question, in your real data are you changing single letters or longer strings?

 

Second this "want changed_string value to be "ddklajhj" " does not match the description " replace a with d, b with k, c with l, d with a"  If we are starting with "abcdjhj" which is 7 characters we don't get "ddklajhj" which is 8 characters.

 

Assuming your are replacing single characters and that you have a typo in the changed string above:

 data example;
    input_string="abcdjhj";
    newstring = translate(input_string,"dkla","abcd");
 run;

TRANSLATE function is only single character replacements which is why I asked. If you have a double-byte character set the function would be KTRANSLATE.

carl_miles
Fluorite | Level 6
/* a with d, b with k, c with l, d with a , */ data b; set a; input_string = tranwrd(tranwrd(tranwrd(tranwrd(input_string, 'a', 'd'), 'b', 'k'), 'c', 'l'), 'd', 'a'); input_string2 = tranwrd(tranwrd(tranwrd(tranwrd(input_string2, 'a', 'd'), 'b', 'k'), 'c', 'l'), 'd', 'a'); run;
Tom
Super User Tom
Super User

What value would you expect changed_string to have if you ran:

 

changed_string='1';
changed_string='2';
changed_string='3';

Do you see what your problem is?

 

 

Or let's do it with numbers:

 

x=1;
y=x+2;
y=x+3;
y=x+4;

How should you modify those four statements so that y ends up with 10?

Spoiler
x=1;
y=x+2;
y=y+3;
y=y+4;

Can you apply the same thing to your assignment statements?

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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