Help using Base SAS procedures

Reverse Substr

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 145
Accepted Solution

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

Thanks in advance


Accepted Solutions
Solution
‎07-08-2015 10:02 AM
Frequent Contributor
Posts: 130

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.

View solution in original post


All Replies
Frequent Contributor
Posts: 130

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!

Frequent Contributor
Posts: 145

Re: Reverse Substr

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

Solution
‎07-08-2015 10:02 AM
Frequent Contributor
Posts: 130

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.

Occasional Contributor
Posts: 19

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

Contributor
Posts: 65

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

Occasional Contributor
Posts: 19

Re: Reverse Substr

Use the substring function

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

Contributor
Posts: 65

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

Occasional Contributor
Posts: 10

Re: Reverse Substr

you can use reverse function

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

Contributor
Posts: 65

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

Senior User
Posts: 1

Re: Reverse Substr

thanks that worked perfectly!
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 1607 views
  • 5 likes
  • 6 in conversation