How can I concatenate strings in SAS, and what are some examples of how to do so? 2

How can I concatenate strings in SAS, and what are some examples of how to do so?

Concatenating strings in SAS refers to combining two or more strings together to form a single string. This can be useful when working with large datasets and needing to create new variables or labels that are a combination of existing strings. To concatenate strings in SAS, the CAT, CATS, and CATX functions can be used. The CAT function combines strings without adding any spaces, while the CATS function adds a single space between each string. The CATX function allows for a specified delimiter to be inserted between the strings. For example, using the CAT function, the strings “Hello” and “World” can be concatenated to form “HelloWorld”. Using the CATS function, the same strings would result in “Hello World”. The CATX function could be used to add a comma between the strings, resulting in “Hello, World”. Overall, concatenating strings in SAS can help simplify data manipulation and analysis.

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 printdata=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 printdata=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 printdata=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 printdata=my_data2;

Additional Resources

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

Cite this article

stats writer (2024). How can I concatenate strings in SAS, and what are some examples of how to do so?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-concatenate-strings-in-sas-and-what-are-some-examples-of-how-to-do-so/

stats writer. "How can I concatenate strings in SAS, and what are some examples of how to do so?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-concatenate-strings-in-sas-and-what-are-some-examples-of-how-to-do-so/.

stats writer. "How can I concatenate strings in SAS, and what are some examples of how to do so?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-concatenate-strings-in-sas-and-what-are-some-examples-of-how-to-do-so/.

stats writer (2024) 'How can I concatenate strings in SAS, and what are some examples of how to do so?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-concatenate-strings-in-sas-and-what-are-some-examples-of-how-to-do-so/.

[1] stats writer, "How can I concatenate strings in SAS, and what are some examples of how to do so?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.

stats writer. How can I concatenate strings in SAS, and what are some examples of how to do so?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top