![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
Prior to SAS 9.0, in order to concatenate text strings in SAS, a combination of the TRIM and LEFT functions and the concatenation operator (||) had to be used. A separator, if desired, would have to be added using a quoted string.
Beginning in SAS 9.0, the CAT family of functions was introduced. These functions simplify concatenating text strings by automatically using certain combinations of these functions and the concatenation operator. The new functions are listed below.
CAT: concatenates character strings without removing leading or trailing blanks CATS: concatenates character strings and removes leading and trailing blanks CATT: concatenates characters strings and removes trailing blanks CATX: concatenates character strings, removes leading and trailing blanks, and inserts separators
The table below shows a comparison of each CAT function to using the concatenation operator.
Function Equivalent Code CAT(OF X1-X4) X1||X2||X3||X4 CATS(OF X1-X4) TRIM(LEFT(X1))||TRIM(LEFT(X2))||TRIM(LEFT(X3))|| TRIM(LEFT(X4)) CATT(OF X1-X4) TRIM(X1)||TRIM(X2)||TRIM(X3)||TRIM(X4) CATX(SP, OF X1-X4) TRIM(LEFT(X1))||SP||TRIM(LEFT(X2))||SP|| TRIM(LEFT(X3))||SP||TRIM(LEFT(X4))
There are several differences in the CAT family of functions compared to the concatenation operator for concatenating character strings. If a variable has not been previously assigned a length, the variable created with the CAT function assigns a length of 200. Using the concatenation operator, the length assigned is the sum of the lengths of the values being concatenated. Another difference is in how missing values are treated. When using the CATX function with a separator, missing values are ignored. When using the concatenation operator and defining a separator, if any variable has a missing value, multiple separators appear consecutively.
Sample code is included on the Full Code tab.
For more information about concatenation in SAS, see the following SAS tutorial video:
Product Family | Product | System | Product Release | SAS Release | ||
Reported | Fixed* | Reported | Fixed* | |||
SAS System | Base SAS | z/OS | 9.21 | 9.2 TS2M3 | ||
Microsoft® Windows® for 64-Bit Itanium-based Systems | 9.21 | 9.2 TS2M3 | ||||
Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9.21 | 9.2 TS2M3 | ||||
Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9.21 | 9.2 TS2M3 | ||||
Microsoft Windows XP 64-bit Edition | 9.21 | 9.2 TS2M3 | ||||
Microsoft® Windows® for x64 | 9.21 | 9.2 TS2M3 | ||||
Microsoft Windows Server 2003 Datacenter Edition | 9.21 | 9.2 TS2M3 | ||||
Microsoft Windows Server 2003 Enterprise Edition | 9.21 | 9.2 TS2M3 | ||||
Microsoft Windows Server 2003 Standard Edition | 9.21 | 9.2 TS2M3 | ||||
Microsoft Windows Server 2003 for x64 | 9.21 | 9.2 TS2M3 | ||||
Microsoft Windows Server 2008 | 9.21 | 9.2 TS2M3 | ||||
Microsoft Windows Server 2008 for x64 | 9.21 | 9.2 TS2M3 | ||||
Microsoft Windows XP Professional | 9.21 | 9.2 TS2M3 | ||||
Windows 7 Enterprise 32 bit | 9.21 | 9.2 TS2M3 | ||||
Windows 7 Enterprise x64 | 9.21 | 9.2 TS2M3 | ||||
Windows 7 Home Premium 32 bit | 9.21 | 9.2 TS2M3 | ||||
Windows 7 Home Premium x64 | 9.21 | 9.2 TS2M3 | ||||
Windows 7 Professional 32 bit | 9.21 | 9.2 TS2M3 | ||||
Windows 7 Professional x64 | 9.21 | 9.2 TS2M3 | ||||
Windows 7 Ultimate 32 bit | 9.21 | 9.2 TS2M3 | ||||
Windows 7 Ultimate x64 | 9.21 | 9.2 TS2M3 | ||||
Windows Vista | 9.21 | 9.2 TS2M3 | ||||
Windows Vista for x64 | 9.21 | 9.2 TS2M3 | ||||
64-bit Enabled AIX | 9.21 | 9.2 TS2M3 | ||||
64-bit Enabled HP-UX | 9.21 | 9.2 TS2M3 | ||||
64-bit Enabled Solaris | 9.21 | 9.2 TS2M3 | ||||
HP-UX IPF | 9.21 | 9.2 TS2M3 | ||||
Linux | 9.21 | 9.2 TS2M3 | ||||
Linux for x64 | 9.21 | 9.2 TS2M3 | ||||
OpenVMS on HP Integrity | 9.21 | 9.2 TS2M3 | ||||
Solaris for x64 | 9.21 | 9.2 TS2M3 |
/* Demonstrates each function */
data test;
input (x1-x4) ($);
x5=' 5';
length new1 $40 new2-new4 $10 ;
new1=cat(of x1-x5);
new2=cats(of x1-x5);
new3=catt(x1,x2,x3,x4,x5);
new4=catx(',', of x1-x5);
keep new:;
datalines;
1 2 3 4
5 6 . 8
;
run;
proc print;
run;
/* Demonstrates the difference in how missing values are handled */
data test;
x1='1';
x2=' ';
x3='6';
newx=catx(',', of x1-x3);
newx1=trim(left(x1))||','||trim(left(x2))||','||trim(left(x3));
put newx=;
put newx1=;
run;
/* output one */ new1 new2 new3 new4 1 2 3 4 5 12345 1234 5 1,2,3,4,5 5 6 8 5 5685 568 5 5,6,8,5/* output two */ newx=1,6 newx1=1, ,6
Type: | Usage Note |
Priority: |
Date Modified: | 2020-10-06 08:34:44 |
Date Created: | 2012-05-25 13:14:15 |