SAS: Concatenate Strings with CAT, CATT, CATS & CATX

Deepanshu Bhalla Add Comment

This tutorial explains five different ways to quickly concatenate strings (character values) in SAS.

Concatenate Strings using Concatenation Operator (||)
combined_variable = variable1 || variable2
CAT Function: Concatenate Strings
combined_variable = CAT(variable1, variable2)
CATT Function: Concatenate Strings with No Trailing Space
combined_variable = CATT(variable1, variable2)
CATS Function: Concatenate Strings with No Space
combined_variable = CATS(variable1, variable2)
CATX Function: Concatenate Strings with a Delimiter and No Space
combined_variable = CATX(delimiter, variable1, variable2)
Sample Dataset

Let's create a sample dataset for demonstration purpose. This dataset will be used in the examples below.

data have;
length first_name $9.;
input first_name $ last_name $ sales;
cards;
Joe Dan 25
Jason Roy 22
Deeps Arora 31
Deepanshu Bhalla 27
;
run;
Method 1: Concatenate Strings using Concatenation Operator (||)

In SAS, the double vertical bar (||) joins strings. It's the oldest method in SAS to combine strings.

data want;
set have;
name = first_name ||  last_name;
run;

The double vertical bar (||) has a limitation of adding unnecessary spaces when concatenating strings. Refer to the image below. The variable first_name has a length of 9 characters so it adds spaces in all the strings where length is less than 9. It doesn't add a space against name 'Deepanshu' because it has a length of 9 characters. Hence this method is not recommended.

Concatenation Operator in SAS

To fix the unnecessary spaces between the text, we can use the STRIP function to remove leading and trailing spaces before concatenating. Here we are adding one space between the first and last name to create a full name.

data want;
set have;
name = strip(first_name) || " " || strip(last_name);
run;

proc print data=want;
run;
STRIP when concatenating in SAS
Method 2: Concatenate Strings using CAT Function

The CAT function combines strings without removing any spaces at the beginning or end. Please note that if any of the variable is numeric, it will be converted to a character string, and any leading or trailing spaces will be removed.

data want;
set have;
name = cat(first_name, last_name);
run;

The CAT Function also has a limitation of having spaces at the end which makes spacing between two strings inconsistent when concatenating the strings.

CAT Function in SAS
Method 3: Concatenate Strings using CATT Function

The CATT function combines strings and removes trailing spaces when concatenating. The extra "T" in "CATT" refers to TRIM function in SAS which removes the trailing blanks.

data want;
set have;
name = catt(first_name, last_name);
run;
CATT Function in SAS
Method 4: Concatenate Strings using CATS Function

The CATS function combines strings and removes both leading and trailing spaces when concatenating. The "S" in "CATS" refers to the STRIP function which removes both leading and trailing blanks.

data want;
set have;
name = cats(first_name, last_name);
run;
CATT Function in SAS
Method 5: Concatenate Strings using CATX Function

The CATX function combines strings and separates them with a delimiter. The CATX function also removes leading and trailing spaces when concatenating.

You can specify any delimiter you want in the first argument of CATX function. In the code below, we are showing two different examples wherein we are using one space and "-" as delimiters.

data want;
set have;
name  = catx(" ", first_name, last_name);
name2 = catx("-", first_name, last_name);
run;

proc print data=want;
run;
CATX Function in SAS
Compare Methods of Concatenation in SAS

In the table below, we have a comparison of five different methods of concatenation in SAS. It is recommended to use the CATX function.

Methods Removes spaces (Character) Removes spaces (Numeric) Delimiter
|| No No No
CAT Function No Yes No
CATT Function Trailing Yes No
CATS Function Leading and Trailing Yes No
CATX Function Leading and Trailing Yes Yes
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 0 Response to "SAS: Concatenate Strings with CAT, CATT, CATS & CATX"
Next → ← Prev