4 Functions to Concatenate Strings Using SAS

4 Functions to Concatenate Strings Using SAS

By : -

4 Functions to Concatenate Strings Using SAS

The concatenation is about putting pieces of text one after the other in a variable. On the one hand, you can use the double bars  || or the exclamation points !!. On the other hand, you can use ‘cat’ functions. Here is a quick overview of these syntaxes.

Whatever the method used, you will need either character variables or text into quotes to perform a contamination. You will have to convert values to numeric values using a put function first, if you want to have a chance to concatenate values.

To illustrate this topic, we are gonna use three variables: x will have leading blanks, y will have both leading and trailing blanks and z would have trailing blanks.

x = ' Country';
y = '  UK  ';
z = '2008 ';

I’m giving you here several ways to concatenate the strings. They all give the same result.

1. The traditional double bars

The double bars || are equivalent to the double exclamation points !!.

a1 = x ||  y || z ;
a1 = ' Country' || '  UK  ' || '2008';
a1 = ' Country' || y || z;
a1 = ' Country UK 2008';

This syntax can also be used in call execute statements.

2. cats, cats, cat and catt Functions

Among those four functions I mainly use cats and cats.

  • cats removes both leading and trailing blanks from each string before being joined
  • catx also removes both leading and trailing blanks ; in addition, a delimiter to be added between the strings to join is specified in the first function parameter.

Keep all blanks: the cat function does not remove any blank. It gives the same results as double bars.

a2 = cat(x,y,z) ;
a2 = cat('Country','  UK  ','2008');
a2 = cat('Country',y,z);
a2 = 'Country  UK  2008’;
a2 = ' Country' || '  UK  ' || '2008 ';

No leading and trailing blanks: the cats function contains as many parameters as strings to join. It is a mix of cat and strip function.

a3 = cats(x,y,z) ;
a3 = cats(' Country','  UK  ','2008 ');
a3 = cats(' Country',y,z);
a3 = 'CountryUK2008';
a3 = strip(' Country') || strip('  UK  ') || strip('2008 ');

Add a delimiter: the catx function removes both leading and trailing blanks before joining any string ; in addition, a delimiter between each string is specified in the first parameter of the function.

a4 = catx('-',x,y,z) ;
a4 = catx('-','Country','  UK  ','2008');
a4 = catx('-','Country',y,z);
a4 = 'Country-UK-2008′;
a4 = strip(' Country') || '-' ||  strip('  UK  ') || '-' || strip('2008 ');

Remove training blanks only: the catt function removes training blanks from strings to be joined. It is a mix of the trim function which moves trailing blanks and of the cat function to concatenate strings.

a5 = catt(x,y,z) ;
a5 = catt(' Country','  UK  ','2008');
a5 = catt(' Country',y,z);
a5 = ' Country  UK2008';
a5 = trim(' Country') || trim('  UK  ') || trim('2008 ');

Don’t forget to specify the length of your new character variable to avoid unexpected truncations.

Leave a Reply

Your email address will not be published.

twenty − six =