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.

Concatenating Strings

The old-fashioned way of concatenating strings uses the operator “||”. This is still available, and still perfectly respectable. When using it, it is important to be aware of the lengths of the variables involved.

1. Concatenating Strings

The log shows:

Since variable A was declared with a length of 3, its value is “A  “ (with two trailing spaces), and similarly with variable C. No length was declared for variable B, so its value is simply ‘B’ and its length is 1.

The first time variable CONCAT is set, its value is “A  BC  “, including the two trailing blanks from variable C. This string has a length of 7, so that becomes the length of variable CONCAT, for the entire data step.

The second time variable CONCAT is set, the STRIP function is used to remove any leading and/or trailing blanks from some of the variables, and some explicit blanks are introduced. The value intended here is ‘A B C  !’, with two blanks before the “!”, but this value has a length of 8, and variable CONCAT only has a length of 7. So the value is truncated to 7 characters, and the “!” is lost.

The SAS language supports a number of concatenation functions that make matters easier. Each of them can handle any number of strings.

2. Concatenating Strings

The log shows:

The CAT function has simply concatenated the three strings, without modifying them. In common with all the other concatenation functions, it has then removed leading blanks from the result.

The CATS function (concatenate with “strip”) strips leading and trailing blanks from all the values before concatenating them.

The CATT function (concatenate with “trim”) strips trailing blanks from all the values before concatenating them.

The CATX function is similar to CATS, but has an extra parameter at the beginning. This is a delimiter to be inserted between the other strings, after they have been stripped. In this example the delimiter is 5 characters long.

The CATQ function (concatenate with quotes) is similar to CATX, but quotes the individual values before concatenating them. It has a lot of options e.g. to specify whether single or double quotes are to be used.

Notice that, although no length was set for the CONCAT variable, the longer values that came later in the step were not truncated. Where the length has not been set, the CAT functions will set a length of 200. This is another significant advantage over using the “||” operator.