BookmarkSubscribeRSS Feed
BeverlyBrown
Community Manager

The most-viewed SAS Communities Library article is this one: How to concatenate values in SAS. The word "concatenate" isn't part of my everyday vernacular, but I bet it's in yours. Google's doing a fine job of helping folks find the article, but thought I'd create this post and pin it in a conspicuous place. You're welcome! Hit the orange button to learn about SAS software's most common CAT* functions.

Concatenate Now

Learn from the Experts! Check out the huge catalog of free sessions in the Ask the Expert webinar series.
5 REPLIES 5
ChrisNZ
Tourmaline | Level 20

Thank for this @BeverlyBrown .

I guess another good topic to pin would be "Converting a numeric variable into character".

This matter comes back rather often as well, including yesterday

BeverlyBrown
Community Manager

Good point! Got a new video covering that. I'll pin it too.

Learn from the Experts! Check out the huge catalog of free sessions in the Ask the Expert webinar series.
manikandanms
Fluorite | Level 6

Great! Thanks for Sharing

sagarwal1
Fluorite | Level 6

I will consider using the CATX method provided by SAS. It automatically takes care of trailing space but assigns 200 bytes of space to the new variable. For that, users can use the LENGTH method to use a certain space.

Syntax -- CATX(' delimiter ', first_var, second_var, ....and more).

Another method --> This is a little bit long but gives the same result using TRIM and ' || '.

Syntax -- TRIM(first_var) || ' delimiter ' || TRIM(second_var) || ' delimiter ' || ......and more.

mkeintz
PROC Star

@sagarwal1 wrote:

I will consider using the CATX method provided by SAS. It automatically takes care of trailing space but assigns 200 bytes of space to the new variable. For that, users can use the LENGTH method to use a certain space.

Syntax -- CATX(' delimiter ', first_var, second_var, ....and more).

Another method --> This is a little bit long but gives the same result using TRIM and ' || '.

Syntax -- TRIM(first_var) || ' delimiter ' || TRIM(second_var) || ' delimiter ' || ......and more.


Not only can you use the LENGTH statement (prior to the CATX call) to override the default length of 200, you can also do something like this:

data want;
  set have;
  if _n_=1 then newvar=oldvar1 || oldvar2 || oldvar3;
  newvar=catx('!',oldvar1,oldvar2,oldvar3);
run;

 

The "if _n_=1 then newvar=..." statement obligates the SAS compiler to calculate a length for newvar, by summing the lengths of oldvar1, oldvar2, and oldvar3.  This could be useful if you are going to apply CATX to a changing list of variables (possibly specified in a macro).  No need for you to determine a safe length by checking changing variable lists.

 

 

Edited addition.  To be precise about this technique, you have to generate a length that also accounts for the delimiter used by catx.  Something like: 

 

data want;
  set have;
  if 0 then newvar=oldvar1 || '!' || oldvar2 || '!' || || '!' oldvar3;
  newvar=catx('!',oldvar1,oldvar2,oldvar3);
run;

BTW, using "if 0" means the statement is never actually executed, but the SAS compiler still has to do its job of making provision for newvar.  

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 6412 views
  • 15 likes
  • 5 in conversation