I have
x=abcdefg
y= SUBSTRN(x,-1,3);
It returns me abc. but I want efg.
Thank you!
@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.
@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.
@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=
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.