BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rakeshvvv
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
dcruik
Lapis Lazuli | Level 10

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.

View solution in original post

10 REPLIES 10
dcruik
Lapis Lazuli | Level 10

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!

rakeshvvv
Quartz | Level 8

it does help..can you let me know what does -2 mean in the code....

dcruik
Lapis Lazuli | Level 10

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.

viveklanka
Fluorite | Level 6

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

MadhuKorni
Quartz | Level 8

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

viveklanka
Fluorite | Level 6

Use the substring function

var2=substr(var1,length(var1)-2,3);

MadhuKorni
Quartz | Level 8

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.);

saidi
Fluorite | Level 6

you can use reverse function

newvar=substr(reverse(strip(oldvar)),1,3);

MadhuKorni
Quartz | Level 8

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

desrcam
Calcite | Level 5
thanks that worked perfectly!

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!

What is Bayesian Analysis?

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.

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
  • 10 replies
  • 65019 views
  • 11 likes
  • 6 in conversation