BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Aexor
Lapis Lazuli | Level 10

 

I need to write to code to change the value of X

X=’1234567890’ , write code to get ‘1234567098’ 

 

I have TRANWRD function here like below

Y = TRANWRD(X,'980',890') though I got the output but this is not the optimal approach for big values.

 

Please help with any other options. Will be much appreciated.

 

Thanks!!

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@Aexor wrote:
Yeah. Will be more careful next time. The code is as below actually:

Y = TRANWRD(X,'890,'098')

I just wanted to know if we have any others functions or utility for this kind of problem.

suppose If X is having length as 500 and I have to replace last three char to "abc" value . wanted a dynamic approach for this. thats all !

Thanks


Now we are getting somewhere. "LAST 3 characters" can be found from a string using something like:

substr(var,length(var)-2);

 

So now, what is the "last 3 characters" supposed to be replaced with?

If those "last 3 characters" can ever be found in more than one portion of the string you do not want to use TRANWRD as that will replace all the occurences. So you may want to extract the first n characters and append the desired 3 characters to the result such as in this example:

data junk;
  x='1234567890';
  y=cats(substr(x,1,length(x)-3),'ABC');
run;

using a much different "replacement" string of letters instead of digits to make it more obvious what has been changed.

 

 

View solution in original post

7 REPLIES 7
ballardw
Super User

You might provide some of those "big" values and one or more rules for what needs to be done with your values.

One example is just that, an example. There is no way we can move from that to general solution without a better description of the process.

 

Something like "switch the value in the 8th position with that in the 10th position" is a rule and can be programmed.

PaigeMiller
Diamond | Level 26

@Aexor wrote:

 

I need to write to code to change the value of X

X=’1234567890’ , write code to get ‘1234567098’ 

 

I have TRANWRD function here like below

Y = TRANWRD(X,'980',890') though I got the output but this is not the optimal approach for big values.

 


Please explain this further. Why do you say it is not optimal?

 

Also, please write code examples that match the words used. Your one SAS statement changes '980' to '890' but your text doesn't have any such change, it changes '890' to '098'

--
Paige Miller
Aexor
Lapis Lazuli | Level 10
Yeah. Will be more careful next time. The code is as below actually:

Y = TRANWRD(X,'890,'098')

I just wanted to know if we have any others functions or utility for this kind of problem.

suppose If X is having length as 500 and I have to replace last three char to "abc" value . wanted a dynamic approach for this. thats all !

Thanks

PaigeMiller
Diamond | Level 26

@Aexor wrote:
Yeah. Will be more careful next time. The code is as below actually:

Y = TRANWRD(X,'890,'098')

I just wanted to know if we have any others functions or utility for this kind of problem.

suppose If X is having length as 500 and I have to replace last three char to "abc" value . wanted a dynamic approach for this. thats all !


This seems like a different problem than the original. You want always to replace the last three characters with 'abc' at the end of a very long string? Or does the replacement depend on what is there now? Or are the replacement characters sometimes 'abc' and sometimes something else depending on some rule? Can you state the entire problem clearly?

--
Paige Miller
Aexor
Lapis Lazuli | Level 10
Sure.

I have below queries : -
1. I want to replace last 3 character of a long string with a value ( having length 3)
2. I want to replace 3 character (starting from position 15 to next three) of a long string with a value ( having length 3)

PaigeMiller
Diamond | Level 26

@Aexor wrote:
Sure.

I have below queries : -
1. I want to replace last 3 character of a long string with a value ( having length 3)
2. I want to replace 3 character (starting from position 15 to next three) of a long string with a value ( having length 3)


Then the code from @ballardw using the CATS() and SUBSTR() function should work.

--
Paige Miller
ballardw
Super User

@Aexor wrote:
Yeah. Will be more careful next time. The code is as below actually:

Y = TRANWRD(X,'890,'098')

I just wanted to know if we have any others functions or utility for this kind of problem.

suppose If X is having length as 500 and I have to replace last three char to "abc" value . wanted a dynamic approach for this. thats all !

Thanks


Now we are getting somewhere. "LAST 3 characters" can be found from a string using something like:

substr(var,length(var)-2);

 

So now, what is the "last 3 characters" supposed to be replaced with?

If those "last 3 characters" can ever be found in more than one portion of the string you do not want to use TRANWRD as that will replace all the occurences. So you may want to extract the first n characters and append the desired 3 characters to the result such as in this example:

data junk;
  x='1234567890';
  y=cats(substr(x,1,length(x)-3),'ABC');
run;

using a much different "replacement" string of letters instead of digits to make it more obvious what has been changed.

 

 

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