Hi,
Can someone help in the extracting last three characters of a string...irrespective of the length of the string i should extract last 3 three into new variable......I am attaching the sample number
32-015-10101
32-015-11201
32-015-11501
32-015-11502
32-015-11503
32-015-11504
output should like this.....
101
201
501
502
503
504
Thanks in advance
It just means to take the entire length of the variable, and then go back 2 spots from the end of the length. Then you take the next 3 values in the string.
Not sure if you want the last 3 in character or numeric, but this should pull out the last 3 regardless of the length of the variable:
newvar=substr(oldvar,length(oldvar)-2,3);
Hope that helps!
it does help..can you let me know what does -2 mean in the code....
It just means to take the entire length of the variable, and then go back 2 spots from the end of the length. Then you take the next 3 values in the string.
It means that
length(var)-2 this means from the length of the variable you are going 2 places prior.
for ex: 32-015-10101
length (var)= 12
length(var)-1=11
length(var)-2=10 from 10th position you want to take three characters, which will give you the last 3 chars that is why its -2
if the lenght of the var is 10 then we have to take the characters from 8,9 and 10th position.
substr(var, starting position, no of characters)
to get the starting position we will do length(var) - 2
if you want the last 5 characters then the same will be length(var)-4
Use the substring function
var2=substr(var1,length(var1)-2,3);
if you want the result as character then
reqvar = substr(var,length(var)-2,3);
if you want the result as numeric then
reqvar = input( substr(var,length(var)-2,3),best3.);
you can use reverse function
newvar=substr(reverse(strip(oldvar)),1,3);
If you use reverse function then the entire string will be reversed.
if you take the first 3 characters from the result then we will get the result which is reverse to the actual req output
the result will be like
101
102
105
.
.
.
but he wants the result like
101
201
501
.
.
.
So again we have to apply reverse for the final output to get the desired output
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!
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.