BookmarkSubscribeRSS Feed

How to concatenate values in SAS

Started ‎09-10-2018 by
Modified ‎08-04-2022 by
Views 501,967

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.

 

SAS instructor @AndyRavenna  offers this quick tutorial if you want to watch and learn. Otherwise, just scroll past and we'll get started with the CAT techniques.

 

 

Concatenate simple trimmed values with the CATS function

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;

Example output:

  

Alfred, Age:14
Alice, Age:13
Barbara, Age:13
Carol, Age:14


Create a list of delimited values with CATX function

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;

Example output:

 

Alfred,14,112.5,69
Alice,13,84,56.5
Barbara,13,98,65.3
Carol,14,102.5,62.8
Henry,14,102.5,63.5
James,12,83,57.3

 

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.

 

Shorthand methods to specify variable lists

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;

 

See SAS Variable Lists for more information.

 

Concatenation operators in SAS

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.)  

 

See also

 

SAS documentation

 
Comments
You can use a CAT function to compare a numeric variable to a character variable or constant. For example, given that zip is numeric ... if cat(zip) eq ‘12201’;

This is as good as it can get

@bedzekim , be careful with using cat(zip) eq '00201 ! This will not work for strings with leading zeros. Run this code:

data _null_;
   zip=00201;
   if cat(zip) eq '00201' then put 'Equal';
   else put 'Not equal';
run;

and you will see in the SAS log:

Not equal

This is because leading zeros are dropped from numeric value.

 

Version history
Last update:
‎08-04-2022 09:46 AM
Updated by:

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Labels
Article Tags