Amadeus cookies policy - you'll see this message only once.

Amadeus use cookies on this website. They help us to know a little bit about you and how you use our website, which improves the browsing experience and marketing - both for you and for others. They are stored locally on your computer or mobile device. To accept cookies, continue browsing as normal. Or, go to the privacy policy for more information.

Reading Backwards!

The SUBSTR function provides an easy way of extracting a sub-string from a longer string by specifying the character position to begin extracting from and a number of characters to extract. For example the following function call will return the value SUBSTR:

SUBSTR("The SUBSTR function is very useful", 5, 6)

The position (second) and number of characters to extract (third) arguments of the SUBSTR function must be positive values, but what if we wanted to specify the position to start extracting the substring from relative to the end of the string rather than the beginning? A practical example of this could be to extract the file extension from a filename, for example "Example File v.1.1.PDF".

One way of achieving this is to first reverse the string such that the first character becomes the last and so on. The REVERSE function will do exactly this for us, taking a character variable or quoted string as it's only argument, e.g.:

REVERSE("Example File v.1.1.PDF") = "FDP.1.1.v eliF elpmaxE"

We can now easily use the SUBSTR function to read everything up to the first period in the reversed string, and applying the reverse function again to this sub-string will give us the file extension.

The following datastep achieves this for us and writes the result to the variable fileType (an intermediate step of reversing the original string is included for clarity and illustration purposes but in practice this could be nested with the other function calls):

data reverseExample; 
  filename = "Example File v.1.1.PDF";
  rfile = reverse(filename);
  fileType = reverse(substr(rfile,1,index(rfile,'.') - 1));
run;