You’re not alone if you’ve ever struggled with concatenating strings in SAS. Traditional methods, such as the “||” operator, can be cumbersome and prone to errors, making them less than ideal for many string manipulation tasks. However, a simple yet powerful solution can help you concatenate strings in SAS with ease: the CAT function.
The CAT function is a versatile tool that allows you to combine multiple strings into a single string, with a wide range of customization options. It is easy to use and can greatly simplify and streamline your string concatenation tasks in SAS. In this post, we’ll take a closer look at the CAT function and how it can be used to concatenate strings effectively in SAS.
Using || Operator to concatenate strings
One traditional method for concatenating strings in SAS is to use the “||” operator. This operator allows you to combine two or more strings by placing the operator between the strings you want to concatenate. For example:
data concatenated_strings;
string1 = 'Hello';
string2 = 'World';
concatenated_string = string1 || string2;
run;
In this example, the resulting value of concatenated_string = "HelloWorld".
Note: Whenever we use the concatenation operator || or !!
, the length of the resultant string is the sum of the lengths of all the individual strings we are adding or concatenating.
The “||” operator is a traditional method for concatenating strings in SAS, but it can present several difficulties and limitations. One issue is that using the “||” operator can result in lengthy and confusing lines of code, especially when concatenating many strings or when more advanced string manipulation is needed. This can make the code difficult to read and maintain.
Another problem with using the “||” operator is that it can be prone to errors. It can be easy to forget to add a delimiter between concatenated strings, resulting in unexpected output. Additionally, the “||” operator may not offer sufficient control over the formatting of the resulting concatenated string, such as adding spaces or other separators between strings.
Overall, while the “||” operator can be a useful tool for simple string concatenation tasks, it may not be the most efficient or reliable method for more complex string manipulation in SAS.
Concatenate strings in SAS using the CAT function
The CAT function concatenates character variables like the concatenation operator (||). It does not remove leading or trailing blanks and returns a concatenated character string.
Syntax:
CAT( item [, ...item])
Example:
data Concatenated_Strings;
var1=" Hello ";
var2=" World ";
combined=cat("*",var1, var2,"*");
run;
In this example, the CAT function concatenates the COL1, COL2 and COL3 listed in the parameters:
The two columns are concatenated into one.
Note that there are leading and trailing spaces in all the var1 and var2 variables.
The spaces are also captured in the concatenated variable:
The CATT Function
The CATT function is similar to the CAT function. However, it removes the trailing spaces before concatenating the variables.
Syntax:
CATT( item [, ...item])
Example:
data Concatenated_Strings;
var1=" Hello ";
var2=" World ";
combined=catt("*",var1, var2,"*");
run;
The CATT function concatenates the variables with the trailing spaces removed:
The CATS Function
The CATS function removes both the leading and trailing spaces before concatenating the variables.
Syntax:
CATT( item [, ...item])
Example:
data Concatenated_Strings;
var1=" Hello ";
var2=" World ";
combined=catt("*",var1, var2,"*");
run;
The leading and trailing spaces are removed from the var1, and var2 variables before the variables are concatenated.
There is no space between the two character values in the concatenated variable.
CATQ Function in SAS:
It concatenates two or more characters or numeric strings by adding a delimiter and quotation mark to the string that contains the delimiter. CATQ function is similar to the CAT function, except it also adds quotation marks.
Syntax
CATQ(modifiers <, delimiter>, item-1 <, …, item-n>)
The modifier modifies the action of the CATQ function. Blanks are ignored. You can use the following characters as modifiers:
Modifier | Description |
---|---|
1 or ‘ | adds single quotation marks to a string. |
2 or “ | uses double quotation marks when CATQ adds quotation marks to a string. |
a or A | adds quotation marks to all of the item arguments. |
b or B | Add quotation marks to item arguments with leading or trailing blanks that the S or T modifiers have not removed. |
c or C | uses a comma as a delimiter. |
d or D | indicates that you have specified the delimiter argument. |
h or H | uses a horizontal tab as the delimiter. |
m or M | M argument inserts a delimiter for every item argument after the first. If you do not use the M modifier, CATQ does not insert delimiters for item arguments that have a length of 0 after processing that is specified by other modifiers. The M modifier can cause delimiters to appear at the beginning or end of the result and can cause multiple consecutive delimiters to appear in the result. |
n or N | With the N argument, you can convert item arguments to name literals when the value does not conform to the usual syntactic conventions for a SAS name. |
q or Q | adds quotation marks to item arguments that already contain quotation marks. |
s or S | strips leading and trailing blanks from subsequently processed arguments: |
t or T | trims trailing blanks from subsequently processed arguments: |
data Concatenated_Strings;
var1=" Hello ";
var2=" World ";
var3="Hello";
combined1=catq('1',var1, var2);output;
combined2=catq('2',var1, var2);output;
combined3=catq('A',var1, var2);output;
combined4=catq('C',var1, var2);output;
combined5=catq('ASTD','-',var1, var2,var3);output;
run;
If you do not use the C, D, or H modifiers, CATQ uses a blank as a delimiter.
If you do not specify a quotation mark in the modifier or the 1 or 2 modifiers, CATQ decides for each item argument which type of quotation mark to use, if quotation marks are required. The following rules apply:
- CATQ uses single quotation marks for strings that contain an ampersand (&) or percent (%) sign, or that contain more double quotation marks than single quotation marks.
- CATQ uses double quotation marks for all other strings.
The CATX Function
The CATX function not only trims the beginning and end spaces but also adds a separator between the character values when joining the variables together.
Syntax
CATX(delimiter, item-1 <, ... item-n>)
Example:
data Concatenated_Strings;
var1=" Hello ";
var2=" World ";
combined=catt("*",var1, var2,"*");
run;
Important points
For all concatenate functions: –
- The Default LENGTH of the returned variable from any CAT function would be 200 bytes if the Length is not previously specified to the assigned variable of the CAT function.
- The returned values from CAT, CATS, CATT, and CAT are usually equivalent to the resultant values of the concatenation operator (with a certain combination like Trim, Left, Strip) except in length.
- CAT, CATS, CATT, and CATX functions are faster than using TRIM and LEFT functions.
- In CATQ, if we do not use C, D, or H as modifiers, then CATQ would use blank as a delimiter
Conclusion
Now that you’ve learned about the benefits and capabilities of the Catx function for concatenating strings in SAS, we encourage you to try it out in your code. Whether a beginner or an experienced SAS user, the CATX function is a valuable tool that can simplify and streamline your string concatenation tasks.
To start with the Catx function, refer to the syntax and examples provided in this post.
Experiment with different combinations of strings and options to see how the CATX function works. You may also want to try using the Catx function in different scenarios, such as merging data values or creating custom labels.
As you gain more experience with the CATX function, you’ll find it a powerful and flexible tool for concatenating strings in SAS. We hope that you find the CATX function as useful and efficient as we do and that it becomes a go-to function in your SAS code.