How to concatenate strings in SAS?

In SAS, strings can be concatenated using the concatenation operator (||) or the CATX function. The concatenation operator allows you to combine two or more strings in a SAS data step to create a new string. The CATX function allows you to concatenate strings and add delimiters or other values to create the final string. Both of these techniques are useful for combining strings together in SAS.


You can use the following methods to quickly concatenate strings in SAS.

Method 1: Concatenate Strings with Space in Between

new_variable = CAT(var1, var2);

Method 2: Concatenate Strings with No Space in Between

new_variable = CATS(var1, var2);

Method 3: Concatenate Strings with Custom Delimiter

new_variable = CATX("-", var1, var2);

The following examples show how to use each method with the following dataset in SAS:

/*create dataset*/
data my_data1;
    input firstName $ lastName $ points;
    datalines;
Austin Smith 15
Brad Stevens 31
Chad Miller 22
Dave Michaelson 19
Eric Schmidt 29
Frank Wright 20
Greg Gunner 40
Harold Anderson 35
;
run;

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

Example 1: Concatenate Strings with Space in Between

The following code shows how to create a new column called fullName that concatenates the firstName and lastName columns using a blank space as a delimiter:

/*create new dataset with concatenated strings*/
data my_data2;
	set my_data1;
	fullName = CAT(firstName, lastName);
run;

/*view new dataset*/
proc print data=my_data2;

concatenate strings in SAS

Example 2: Concatenate Strings with No Space in Between

The following code shows how to create a new column called fullName that concatenates the firstName and lastName columns using no space as a delimiter:

/*create new dataset with concatenated strings*/
data my_data2;
	set my_data1;
	fullName = CATS(firstName, lastName);
run;

/*view new dataset*/
proc print data=my_data2;

Example 3: Concatenate Strings with Custom Delimiter

The following code shows how to create a new column called fullName that concatenates the firstName and lastName columns using a dash as a delimiter:

/*create new dataset with concatenated strings*/
data my_data2;
	set my_data1;
	fullName = CATX("-", firstName, lastName);
run;

/*view new dataset*/
proc print data=my_data2;

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

x