Hello @JLF5806! The syntax of the both the SUBSTR and SUBSTRN functions is:
SUBSTR (string, start <,length>)
In both cases, the function reads the string left to right, starting at the position specified by start and returning a string of the specified length. The two functions differ in how they handle zero or negative start and length values.
Your second example of SUBSTR("ABCDE", 1, 2) specifies a starting position of 1 and a length of 2, so the function starts at the letter "A" and returns two characters, resulting in the string "AB". Similarly, your first example of SUBSTRN("ABCDE", -1, 4) specifies a starting position of -1 and a length of 4. When the starting position is negative or zero, SUBSTRN returns a string that starts at the first position (here the letter "A") and adjusts the length of the string returned to account for the start value. If the starting position is 0, SAS subtracts 1 off the length. If the starting position is -1, SAS subtracts 2 off the length, etc. For a starting position of -1 and length of 4, think of it, for example, that you count -1, 0, 1, 2, but SAS only returns the characters starting at position 1, resulting in the string "AB". Check out Example 1- Manipulating Strings with the SUBSTRN Function- in the SUBSTRN documentation for another example.
... View more