We often need to combine one or more values in SAS for reporting or to create new data fields. SAS supports a series of functions for this task, referred to collectively as the CAT* functions. These functions allow you to combine SAS variables and literal values in different ways. You can also use the (older) method of the SAS concatenation operator (||) to combine values. This article describes the most commonly used functions -- see the documentation references at the bottom of the article for more.
The CATS function combines values by first removing any leading or trailing blank spaces, then returns the concatenated string. For example:
data report(keep=report); set sashelp.class; report = cats(name, ', Age:',age); run;
Use CATX function to create a character string that combines multiple values and separates them with a delimiter of one or more characters. Like the CATS function, CATX trims leading and trailing blanks before combining the values. For example:
data csv (keep=extract); set sashelp.class; extract = catx(',',name,age,weight,height); run;
When using CATX or CATS, pay attention to the length of the returned value. If you assign the result to a variable whose length has not yet been set, the default length of the variable is 200 bytes. When using the result as an intermediate value in SQL or SAS macro processing, the length can vary. See Length of the Returned Variable for guidance.
The CAT* functions support the OF syntax to specify variable lists. For example, this program creates a comma-separated list of all variable values in the CLASS data set. Note the double hyphen that specifies the range.
data all (keep=full); set sashelp.class; full = catx(',',of name--weight); run;
This program creates a list of just the character values separated by the | symbol. Note the character keyword between the hyphens in the range specifier. (Use numeric for just the numeric values.)
data char (keep=text); set sashelp.class; text = catx('|',of name-character-weight); run;
Like most programming languages, SAS also has a concatenation operator that allows for simple inline combinations of values. The operator is the double vertical bar: ||. Example use:
data report(keep=report); set sashelp.class; report = name || ', Age: ' || age; run;
This concatenation operator is convenient, but doesn't provide the same flexibility as the CAT* functions to control how white space is trimmed. But as this operator has been supported in SAS for a very long time, you might see it used in older SAS programs.
Note: The CAT* functions and concatenation operator can combine character and numeric variables into a single (larger) character value. The process implicitly converts a numeric value into a character and doesn't generate a SAS note or warning, unlike some of the other conversion methods. (For example, when you assign a numeric value to a character variable.)
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.