SAS: A Complete Guide to CAT, CATT, CATS & CATX


You can use the CAT, CATT, CATS & CATX functions to concatenate string variables together in SAS.

Here is the difference between the various functions:

The CAT function concatenates string variables without removing any blanks.

The CATT function removes trailing spaces from strings and then concatenates string variables.

The CATS function removes both leading and trailing spaces from strings and then concatenates string variables.

The CATX function removes both leading and trailing spaces from strings and then concatenates string variables with a custom delimiter.

The following example shows how to use each function in practice.

Example: How to Use CAT, CATT, CATS & CATX in SAS

Suppose we have the following dataset in SAS that contains three string variables:

/*create dataset*/
data my_data;
input player $ team $ conf $;
datalines;
Andy Mavs West
Bob Lakers West
Chad Nuggets West
Doug Celtics East  
Eddy Nets East  
;
run;

/*view dataset*/
proc print data=my_data;

Now suppose that we would like to use the CAT, CATT, CATS & CATX functions to concatenate these three string variables into a single variable.

We can use the following code to do so:

/*create new dataset that concatenates columns*/
data new_data;
set my_data;
cat = cat(player, team, conf);
catt = catt(player, team, conf);
cats = cats(player, team, conf);
catx = catx('-', player, team, conf);
run;

/*view dataset*/
proc print data=new_data;

SAS CAT CATT CATS and CATX functions

Notice that the various concatenate functions have all concatenated the three string variables in slightly different ways.

The CAT function concatenated the three strings and left the spaces in between them.

The CATT function removed all trailing spaces from each strings and then concatenated them.

The CATS function removed both leading and trailing spaces from each string and then concatenated them.

The CATX function removed both leading and trailing spaces from each string and then concatenated them using a dash ( ) as a delimiter.

When working with these concatenation functions on your own data, feel free to use whichever one is most suitable for your situation.

Additional Resources

The following tutorials explain how to perform other common tasks in SAS:

How to Remove Special Characters from Strings in SAS
How to Extract Numbers from String in SAS
How to Convert Strings to Uppercase, Lowercase & Proper Case in SAS

Leave a Reply

Your email address will not be published. Required fields are marked *