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

I have 

x=abcdefg

y= SUBSTRN(x,-1,3);

 

It returns me abc. but I want efg.

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@GingerJJ wrote:

I have 

x=abcdefg

y= SUBSTRN(x,-1,3);

 

It returns me abc. but I want efg.

 

Thank you!


The real question is do you want the last three characters, regardless of length, or do you want the substring that starts in the 5th position to the end of the value, or some other rule?

 

The above returns 'abc' because you told it to start in a position before the string and get the following 3 characters.

From the documentation : If the position that you specify is non-positive, the result is truncated at the beginning, so that the first character of the result is the first character of the string.

Position is the second parameter, or where to start

 

Assuming you want the last 3 characters try

y= SUBSTRN(x,length(x)-2);

Why minus 2 you may ask. The function want a starting position in the second position. If the length of string is 7, then you want positions 5, 6 and 7 (assuming you want three characters). So to get 5 you want 7-2.

 

View solution in original post

3 REPLIES 3
ballardw
Super User

@GingerJJ wrote:

I have 

x=abcdefg

y= SUBSTRN(x,-1,3);

 

It returns me abc. but I want efg.

 

Thank you!


The real question is do you want the last three characters, regardless of length, or do you want the substring that starts in the 5th position to the end of the value, or some other rule?

 

The above returns 'abc' because you told it to start in a position before the string and get the following 3 characters.

From the documentation : If the position that you specify is non-positive, the result is truncated at the beginning, so that the first character of the result is the first character of the string.

Position is the second parameter, or where to start

 

Assuming you want the last 3 characters try

y= SUBSTRN(x,length(x)-2);

Why minus 2 you may ask. The function want a starting position in the second position. If the length of string is 7, then you want positions 5, 6 and 7 (assuming you want three characters). So to get 5 you want 7-2.

 

data_null__
Jade | Level 19

@GingerJJ wrote:

I have 

x=abcdefg

y= SUBSTRN(x,-1,3);

 

It returns me abc. but I want efg.

 

Thank you!


-1,3 does not return abc

53         data _null_;
54            x='abcdefg';
55            do i = -1 to 8;
56               y=SUBSTRN(x,i,3);
57               put i 2. +1 y=$hex10. +1 y=;
58               end;
59            run;

-1 y=6120202020  y=a
 0 y=6162202020  y=ab
 1 y=6162632020  y=abc
 2 y=6263642020  y=bcd
 3 y=6364652020  y=cde
 4 y=6465662020  y=def
 5 y=6566672020  y=efg
 6 y=6667202020  y=fg
 7 y=6720202020  y=g
 8 y=2020202020  y= 

 

Tom
Super User Tom
Super User

Explanation:

If you start in column -1 and want 3 characters then the string you want ends at column 1.

If you start in column  S and want L characters then the string ends at column (S+L-1).

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 949 views
  • 5 likes
  • 4 in conversation