Date: Thu, 27 Jul 2006 08:07:26 +0100
Reply-To: Guido T <cymraegerict@GMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Guido T <cymraegerict@GMAIL.COM>
Subject: Re: Concatenating character values?
In-Reply-To: <1153865478.073979.267910@75g2000cwc.googlegroups.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
On 25/07/06, Eric B <emblabac@hotmail.com> wrote:
> Its actually pretty easy. Simply
> new_var = a || "/" || b || "/" || c;
> Use the COMPRESS function if you have extra blanks lurking around.
> Hope that helps.
> Eric B
>
Hi Eric,
The problem with using
new_var = a || "/" || b || "/" || c;
Is that if the defined length of the component character variables is
greater or equal to the length of NEW_VAR then the resulting string
will just be the conents of A.
For example ...
5107 data test;
5108 length new_var a b c $200;
5109 a = 'a';
5110 b = 'b';
5111 c = 'c';
5112 new_var = a||'/ '||b||'/ '||c;
5113 put new_var=;
5114 cmp_var = compbl(a||'/ '||b||'/ '||c);
5115 put cmp_var=;
5116 run;
new_var=a
cmp_var=a / b / c
The defined length is used in concatenating the strings, so the first
'/' is added to the end of character variable A which contains 'a'
followed by 199 spaces (sort of).
So the '/' is beyond the end of the string.
When you use COMPRESS or the COMPBL functions, the work string is a
long as it needs to be before the spaces are removed. So in this case
the 602 character long work string is compressed before bing returned
to CMP_VAR.
The version 8 solution would be ...
new_var = compress(a||'/ '||b||'/ '||c);
or
new_var = trim(a)||'/'||trim(b)||'/'||trim(c);
In version 9 the solution would be to use the CATX function ...
cmp_var = catx('/', a, b, c);
The CATX function removes the leading and trailing spaces from the
strings and then concatenates the string with the delimiter between
them.
A much neater solution.
Regards
++ Guido