- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Good point! Got a new video covering that. I'll pin it too.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Great! Thanks for Sharing
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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
--------------------------