To concatenate strings in a SAS data step, the easiest way is to use the SAS catx() function.

data k;
        var1 = "This";
        var2 = "is";
        var3 = "a";
        var4 = "string.";
        concatenated = catx(" ",var1, var2, var3, var4);
        put concatenated=;
run;

/* Output */
concatenated=This is a string.

When working with character variables and strings in SAS, many times we need to combine and concatenate strings to create new concatenated variables.

There are a number of ways to concatenate strings in SAS. The easiest is with the SAS catx() function.

The catx() function in SAS takes in an argument for the delimiter you want to use, as well as arguments for the variables you want to concatenate.

catx() remove leading and trailing blanks from the variables and then concatenates them.

Below is a simple example of how to use catx() to concatenate strings in a SAS data step.

data k;
        var1 = "This";
        var2 = "is";
        var3 = "a";
        var4 = "string.";
        concatenated = catx(" ",var1, var2, var3, var4);
        put concatenated=;
run;

/* Output */
concatenated=This is a string.

The rest of this post will show you other ways you can concatenate variables in SAS.

Basic Concatenation of Character Variables in a SAS Data Step

You can also concatenate variables in a SAS data step without the use of a SAS function. You can do basic concatenation in SAS with ‘||’.

Just like in other languages, where typically the concatenate operator is “+”, use ‘||’ to concatenate strings in a SAS data step.

Let’s say we have the following data in SAS.

data k;
        input word $ 5. word2 $ 8.;
    datalines;
this  is
some  data
for   an
easy  example
;
run;

We can concatenate the two variables with a delimiter in the following SAS code using simple concatenation as shown below.

data data_new;
    set k;
    basic_concat = word || "+" || word2;
run;

/* Output: */
  word    word2   basic_concat
1 this	     is	       this+is
2 some	   data	     some+data
3  for	     an	        for+an
4 easy	example	  easy+example

Using cat() to Concatenate Strings in SAS

There are a number of useful functions in SAS which allow us to concatenate character variables. While catx() is probably the best option in most cases, there are a few others which could be useful depending on what you want to accomplish.

The cat() function concatenates the given arguments, but does not remove leading or trailing spaces and does not have an option for a delimiter.

cat() is basically the same as the simple concatenation showed in the last section.

Below is an example of how to use cat() to concatenate strings in a SAS data step.

data k;
        var1 = "  This   ";
        var2 = "is   ";
        var3 = " a    ";
        var4 = "    string.";
        cat = cat(var1, var2, var3, var4);
        put cat=;
run;

/* Output */
cat=This   is    a        string.

Using catt() to Concatenate Strings in SAS Data Steps

Another function you can use to concatenate two strings together in a SAS data step is the catt() function.

The catt() function concatenates the given arguments and removes trailing blanks, but does not remove leading spaces and also does not have an option for a delimiter.

catt() differs from cat() in that it removes the trailing blanks from a variable. Other than that, it is performs basic concatenation.

Below is an example of how to use catt() to concatenate strings in a SAS data step.

data k;
        var1 = "  This   ";
        var2 = "is   ";
        var3 = " a    ";
        var4 = "    string.";
        catt = catt(var1, var2, var3, var4);
        put catt=;
run;

/* Output */
catt=Thisis a    string.

Using cats() to Concatenate String Variables in SAS

Finally, we have the cats() function which concatenates the given arguments, removes the trailing and leading blanks, but does not have an option for a delimiter.

Again, we have a function which will concatenate variables, but if you need to use a delimiter, cats() won’t work.

Below is an example of how to use cats() to concatenate multiple strings in a SAS data step.

data k;
        var1 = "  This   ";
        var2 = "is   ";
        var3 = " a    ";
        var4 = "    string.";
        cats = catss(var1, var2, var3, var4);
        put cats=;
run;

/* Output */
cats=Thisisastring.

Hopefully this article has been useful for you to learn how to concatenate string and character variables in SAS.

Categorized in:

SAS,

Last Update: March 13, 2024