New Contributor
Posts: 3

# using SUBSTR function extracts last 3 values

Hello All,

The below program using substr extracts last 3 values from obs, can anyone please help me to undersatnad the logic using substr function

input trt \$;
cards;
treat123
treat21
treat1
treat1
trea2
lbdate
;run;

data abc;
set temp;
a=reverse(substr(left(reverse(trt)),1,3));
b=substr(trt,length(trt)-2);
run;

Thanks

Super User
Posts: 6,778

## Re: using SUBSTR function extracts last 3 values

Which of these do you already understand?

• What does the SUBSTR function do?
• What are the three parameters for SUBSTR?
• What does the LENGTH function do?
• What does the REVERSE function do?
New Contributor
Posts: 3

## Re: using SUBSTR function extracts last 3 values

output of the program
123
t21
at1
at1
ea2
ate

b=substr(trt,length(trt)-2);
length of the variabke is 8 and 8-2 =6,
so for the first observation from the sixth position we get the values using substr function
but from the second observation onwards the obs values decreasing but still substr function extracting last 3 values

So am missing some logic here, may be am not thinking correct way

Super User
Posts: 6,778

## Re: using SUBSTR function extracts last 3 values

For that piece of the puzzle ...

The second parameter within SUBSTR is a number that indicates which character to begin with.

When the third parameter is omitted (as is the case here), SUBSTR takes all the remaining characters.

So this code says measure the length of the incoming string.  Substract 2 from that.  That becomes the starting position.  This means that there will be 3 characters available (as long as the length of the original string is at least 3 characters).  Better code would be:

if length(trt) >= 3 then b = substr(trt, length(trt)-2);

That protects against calculating a negative starting position, something that SUBSTR would complain about.

Super User
Posts: 9,599

## Re: using SUBSTR function extracts last 3 values

Good time to read the manual methinks, substr is clearly described there.

If you just want the numbers at the end (and they don't appear anywhere else in the string) compress is simpler:

```data want;
text="abcd123";
num=compress(text," ","kd");
run;```
Super User
Posts: 13,554

## Re: using SUBSTR function extracts last 3 values

tinayong wrote:
output of the program
123
t21
at1
at1
ea2
ate

b=substr(trt,length(trt)-2);
length of the variabke is 8 and 8-2 =6,
so for the first observation from the sixth position we get the values using substr function
but from the second observation onwards the obs values decreasing but still substr function extracting last 3 values

So am missing some logic here, may be am not thinking correct way

Perhaps you phrased your question incorrectly. If you are wanting the numbers at the end and not the "last 3 values" then @RW9 has your solution with compress function

Super User
Posts: 9,599

## Re: using SUBSTR function extracts last 3 values

Well one method is:

`data want;  trt="treat123";  want=substr(trt,lengthn(trt)-2);run;`

Discussion stats
• 6 replies
• 157 views
• 0 likes
• 4 in conversation