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

Couldn't attend SAS Innovate in person? We gotchu. Access on-demand content now!

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.

Couldn't attend SAS Innovate in person? We gotchu. Access on-demand content now!

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

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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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