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!!
@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.
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.
@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'
@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?
@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.
@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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.