Select Page

Jonathan-Sellar-SunAs a relatively new SAS coder I have been lucky enough to have been brought up with the newer versions of SAS functions that have come along since 9.1. A number of my colleagues, who have far more experience than I, are often unaware or have developed macros or work arounds where there have been failings in SAS. I’d like to present some functions that can make your life much easier.


This first one is quite common and is used to remove leading and trailing spaces from a string.

The original technique was (and I have even seen it still produce from Enterprise Guide 5.1 tasks):

trimmedstring = trim(left(paddedstring));

it can now be done simply by using:

trimmedstring = strip(paddedstring);

Strictly speaking these two aren’t quite equivalent as the trim/left technique will return a string with a single space in it if the original string had no characters in it, as compared to strip which would return an empty string (SAS null). The newer trimn function also returns an empty string in such cases.

ANY and NOT functions

This is a family of functions that either find the first instance of a specific character type or find the first instance of anything other than the specific character type within a string. This family is similar to the INDEX/FIND family of functions but work with sets of characters rather than specific strings.

The character types are defined as:

ALNUM                               any number, or upper or lower case letter
ALPHA                                any upper or lower case letter
DIGIT                                   any number
PUNCT                                 any punctuation character
SPACE                                  any white space character
UPPER                                 any upper case character
LOWER                                any lower case character

The syntax for the function is:

[ANY|NOT][ ALNUM| ALPHA|DIGIT|PUNCT|SPACE|UPPER|LOWER](<string>[,<start position>])

<string> is either a quoted string or a variable name and <start position> is the relative position within the string that you want to start looking from.

Note that if <start position> is negative this changes the default search behaviour from left to right to right to left.

Note that spaces are included in the count of characters.

A few examples:

stringvar = “David’s 12 bacon sandwiches”
ANYDIGIT(stringvar) returns 9
ANYPUNCT (stringvar) returns 6
NOTALPHA(stringvar) returns 6
ANYDIGIT(stringvar, -1) returns 18

CAT functions

The basic CAT function takes two or more strings and joins them together in the order they are specified, including any trailing or leading blanks. This set of functions adds additional options to make the CAT functions even more useful.

CATS removes both trailing and leading blanks from all strings before concatenating them. This is the same as CAT(TRIM(LEFT(string1), (TRIM(LEFT(string2)).

CATT removes only the trailing blanks from all strings before concatenating them.

CATX removes both trailing and leading blanks from all strings and inserts a specified delimiter between each string as it concatenates them.

CATQ has numerous modifiers to adapted it’s use, but primarily it wraps strings in quotes before concatenating.

Some examples:

string1 = "   apple"
string2 = "    pear    "

CAT(string1, string2) returns  "   apple    pear    "

CATS(string1, string2) returns "applepear"

CATX(" and ", string1, string2) returns "apple and pear"

CATQ("1asd", ",", string1, string2) returns "'apple','pear'"


There are plenty of other modified and updated functions now available in SAS especially since 9.2.

Check out the SAS’s support documentation here:

You never know what you might find!

Happy coding,


%d bloggers like this: