In SAS we don't have use the RIGHT function in the same way. SAS's RIGHT function moves the characters in the string to the end of the string and the trailing blanks to the start of it i.e. right aligns the string. That's not what we want here.
We want the SUBSTRN function.
SUBSTRN takes three input variables:
the text string,
the starting position,
the number of characters from that point to extract.
Instead we have to calculate the length of each string (excluding trailing blanks) and work back from there. In the case of our "LONDON GB" example, we have 9 characters (including our space in the middle). To get the length of this string in SAS, use the LENGTH function e.g. = LENGTH (myString). This will return an integer for the number of characters in the string.
To get the last two characters we take 1 away from the LENGTH of the string i.e. start at the position one before the last to get the last two characters.
Our SAS code would look like this:
= substrn(LOCATION, length(LOCATION)-1, 2)
That is, use the SUBSTRN function on the string in variable LOCATION. Start at position LENGTH-1 and retrieve two characters from there.
I've suggested using SUBSTRN in this case as it allows us a little more flexibility. It can return null values without giving an error.Also, if the string is smaller than the number of characters needing to be returned, it can return less than the requested number of characters.
In fact, if you suspect that your data may have cases where it is smaller in length than the required number of characters, try using the MAX function in there as well: