SUPPORT / SAMPLES & SAS NOTES
 

Support

Usage Note 24514: What is the difference in using the SAS function CATX to concatenate text versus using the TRIM and LEFT functions with the concatenation operator (||)?

DetailsAboutRate It

The results of concatenating text are equivalent with both methods. However, the CATX function requires less code and processes the concatenation faster than the combination method, which uses multiple calls.

The following examples show the difference in coding for both methods. Notice that in these examples,

  • 'Nina' is the user-supplied value for FIRSTNAME.
  • 'Werner' is the user-supplied value for LASTNAME.
  • 'Nina Werner' is the resulting concatenated string represented by MEMNAME.

Example 1: Concatenating First and Last Names by Using the Combination Method

  memname=trim(left(firstname) || ' ' || left(lastname);

Example 2: Concatenating First and Last Names by Using the CATX Function

  memname=catx(' ', firstname, lastname);

For environments that support variable lists, you can use the OF syntax in the CATX function to condense variable lists that you are concatenating (Example 4 below). Note: In the following examples, separator specifies the character string of your choice (comma, blank, and so on) that will be used to separate the concatenated strings.

Example 3: Concatenating a Variable List by Using the Combination Method

    x=trim(left(x1)) || separator || trim(left(x2)) ||separator ||
    trim(left(x3)) || separator || trim(left(x4));

Example 4: Concatenating a Variable List by Using the CATX Function

      x=catx(separator, of x1-x4);

For detailed information, see "CATX Function" in SAS Language Reference: Dictionary under Base SAS in SAS OnlineDoc 9.1.3.

page divider

About the Author
This tip was suggested by Nina L. Werner. Nina is a SAS programmer with more years experience than she wants to say.



Operating System and Release Information

Product FamilyProductSystemSAS Release
ReportedFixed*
SAS SystemBase SASAlln/a
* For software releases that are not yet generally available, the Fixed Release is the software release in which the problem is planned to be fixed.