Quartz | Level 8

## Reverse Substr

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

1 ACCEPTED SOLUTION

Accepted Solutions
Lapis Lazuli | Level 10

## Re: Reverse Substr

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.

10 REPLIES 10
Lapis Lazuli | Level 10

## Re: Reverse Substr

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!

Quartz | Level 8

## Re: Reverse Substr

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

Lapis Lazuli | Level 10

## Re: Reverse Substr

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.

Fluorite | Level 6

## Re: Reverse Substr

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

Quartz | Level 8

## Re: Reverse Substr

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

Fluorite | Level 6

## Re: Reverse Substr

Use the substring function

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

Quartz | Level 8

## Re: Reverse Substr

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

Fluorite | Level 6

## Re: Reverse Substr

you can use reverse function

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

Quartz | Level 8

## Re: Reverse Substr

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

Calcite | Level 5

## Re: Reverse Substr

thanks that worked perfectly!
Discussion stats
• 10 replies
• 64595 views
• 11 likes
• 6 in conversation