Concatenate Strings in SAS
In SAS you can concatenate strings in a variety of ways. This article answers the question how to concatenate strings in SAS with the CAT, CATT, CATS and CATX function. We explain the syntax and the differences between the functions with examples.
The CAT, CATT and CATS have all the same syntax. Each function has two obligatory arguments and n optional arguments.
CAT(item1, item2 <, item3, item4, …, item n>)
CATT(item1, item2 <, item3, item4, …, item n>)
CATS(item1, item2 <, item3, item4, …, item n>)
The items 1 to n are the strings you want to concatenate. The CATX function has an extra, obligatory argument, namely a separator argument. With this argument, you can separate the input items in the output string.
CATX(separator, item1, item2 <,item3, item4, …, item n>)
Differences & Examples
To better explain the differences between the 4 functions we need the data set below. The data set consists of 3 character columns which will be concatenated. It’s important to realize that the first row contains leading and trailing blanks. More specifically, string1 has both leading and trailing blanks, string2 has leading blanks, and string3 has leading and trailing blanks. In the second row, the strings contain only trailing blanks.
data work.ds; infile datalines dlm=',' ; informat string1 $ CHAR15. string2 $ CHAR15. string3 $ CHAR15.; input string1 string2 string3; datalines; A STRING , SECOND STRING, THIRD STRING A STRING,SECOND STRING,THIRD STRING ; run;
The CAT function
In SAS you can use the CAT function to simply concatenate one or more strings. It is equivalent to the concatenation operator ‘||’. This function concatenates the inputs regardless of leading or trailing blanks. See the example below.
data work.cat; set work.ds; cat = cat(string1, string2, string3); keep cat; run;
Note in the image above how the CAT function ignored any leading or trailing blanks.
The CATT function
Compared to the CAT function, the CATT function does take into account the trailing blanks and removes them during the concatenation. Note the second T in the name of the function, which stands for Trailing.
data work.catt; set work.ds; catt = catt(string1, string2, string3); keep catt; run;
Observe how all trailing blanks were removed during concatenation. In the second row, the string only contains the blanks of within each concatenated string. However, the first row still contains blanks because of the leading blanks.
The CATS function
Compared to the CAT and CATT functions, the CATS function removes both leading and trailing blanks while concatenating its input arguments. The S in the function name stands for Strip.
data work.cats; set work.ds; cats = cats(string1, string2, string3); keep cats; run;
The image above shows the result of the concatenation with the CATS function. Both leading and trailing blanks are removed and the first and second row are the same.
The CATX function
Using the CATS function might result in a string difficult to read. To solve this problem you can use the CATX function. The CATX function removes both leading and trailing blanks and allows the user to define a separator that separates the input arguments. In the examples below we use a ‘blank’ as a separator and a ‘/’.
data work.catx1; set work.ds; catx = catx(' ',string1, string2, string3); keep catx; run;
data work.catx2; set work.ds; catx = catx('/',string1, string2, string3); keep catx; run;
So, to summarize, this article explains how to concatenate strings in SAS with the CAT, CATT, CATS, and CATX functions. The conclusions are:
- CAT: Concatenates strings regardless of leading and trailing blanks.
- CATT: Concatenates strings removing trailing blanks.
- CATS: Concatenates strings removing leading and trailing blanks.
- CATX: Concatenates strings removing leading and trailing blanks allowing the user to define a separator that separates the strings.