SAS® and R

Best of Both Worlds

All those CATs

with one comment

The title is a bit misleading since we are not talking about the furry animals here. This post is about the conCATenation functions in SAS, which are useful, but CAN get hairy at times.

The word concatenate comes directly from Latin concatenare, which in turn is formed from “con-,” meaning “with” or “together,” and “catena,” meaning “chain.” And the CATx functions, in short, “chain” words or numbers together to make a new string. The different CATx functions dictate how you want to chain them together.

There are five concatenate functions in SAS (CAT, CATS, CATT, CATX, CATQ).  The main difference among these functions involves the handling of leading/trailing blanks as well as separator characters between the concatenated items. People from SAS old school might still prefer traditional concatenation operator (||), but the new functions just take far less work to accomplish the same task. The following output gives an overview of the difference between the CATx functions. (click the graph to enlarge)

As shown below, we have four strings (brackets on both sides to show the leading/trailing blanks), and you can see the resulting strings using the different CATx functions (period at the end to show where the string ends). One advantage of using the CATx functions is the items to be concatenated may be character or numeric. And, if you include numeric values, they are treated as if they were actually character values and no numeric-to-character conversion messages are printed to the SAS log.

  All Those CATx, MEOW!
String 1 has no leading/trailing blank            [ALL]
String 2 has three trailing blanks                [THOSE   ]
String 3 has two leading blanks                   [  CATS]
String 4 has three leading and two trailing blanks[   MEOW  ]
- CAT acts like '||' with minor difference.¹
cat(String1,String2,String3,String4) shows:       ALLTHOSE     CATS   MEOW  .
- CATS Removes trailing and leading blanks.
cats(String1,String2,String3,String4):            ALLTHOSECATSMEOW.
cats(12,34,56,78)                                 12345678.
- CATT only trims trailing blanks.
catt(String1,String2,String3,String4)             ALLTHOSE  CATS   MEOW.
- CATX trims both leading and trailing blanks, and inserts separator character.
catx(' ',String1,String2,String3,String4)         ALL THOSE CATS MEOW.
catx(',',String1,String2,String3,String4)         ALL,THOSE,CATS,MEOW.
catx('-',908,782,6562)                            908-782-6562.
- CATQ joins strings together as defined by the modifier.²
catq(' ',String1,String2,String3,String4)         ALL "THOSE   " "  CATS" "   MEOW  ".
catq('a',String1,String2,String3,String4)         "ALL" "THOSE   " "  CATS" "   MEOW  ".
catq('s',String1,String2,String3,String4)         ALL THOSE CATS MEOW.
catq('as',String1,String2,String3,String4)        "ALL" "THOSE" "CATS" "MEOW".
catq('asd','~~', String1,String2,String3,String4) "ALL"~~"THOSE"~~"CATS"~~"MEOW".
¹ The default length of the result when you use the || operator is the sum of
  the lengths of the strings being concatenated, the default length of the
  result when you use the CAT function is 200.
² Check SAS Language Reference for details on all the available modifiers.
Remember to define string length before calling CATx,
otherwise the default length of the resulting string is 200.

For a complete list of CATQ modifiers, go to CATQ function language reference.

Please note that it is always a good practice to initialize your character variable by specifying the length of the resulting variable directly under the data statement, and making sure to set the length of created variables long enough to accommodate the longest string created by concatenation. If not, the resulting string will be truncated, and you will see an error message in the log.


Written by sasandr

May 22, 2012 at 10:55 am

Posted in SAS

Tagged with

One Response

Subscribe to comments with RSS.

  1. Best of both worlds, indeed! And not just R KELLY and JAY Z. Thanks for the post.


    March 16, 2014 at 1:15 am

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: