How to Concatenate Strings in SAS (With Examples)


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;

Additional Resources

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

How to Normalize Data in SAS
How to Rename Variables in SAS
How to Remove Duplicates in SAS
How to Replace Missing Values with Zero in SAS

Leave a Reply

Your email address will not be published.