@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.
... View more