Yi Zang
Yi Zang

Reputation: 31

concatenate strings in SAS

used || to concatenate two strings and overwrite one of the variable. But overwrite does not happen sometime.

Here is my 1st code:

data aa;
length a b $3;
a = 'FA';
b = '1';
a = a || b;
run;

Value of a is still 'FA'. But if we replace the concatenate with: $ b = a || b; then b will have the value 'FA1'.

Can anyone explain why this happens

Upvotes: 1

Views: 7602

Answers (1)

Richard
Richard

Reputation: 27498

SAS character variables store the trailing spaces of a value to fill the variable length. Values combined with the concatenation operator || are not modified in any way, thus the effective operation (value-wise) with the variables is:

a = "FA " || "1  "

So FA<space>1<space><space> is getting stuffed into $3 and truncating to FA<space>

Old school code would be

a = trim(left(a))||left(b);

Review the CAT* family of functions for concatenating values with trimming, stripping and delimiting. For example:

a = cats(a,b);

Upvotes: 3

Related Questions