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.
Couldn't attend SAS Innovate in person? We gotchu. Access on-demand content now!
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.
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!
Great! Thanks for Sharing
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.
@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.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.