Why can't SAS string concat include this variable?

This problem is driving me crazy. In SAS, when I want to concatenate a string, the variable to be assigned to the result cannot be used in the input.

DATA test1;
    LENGTH x $20;
    x = "a";
    x = x || "b";
RUN;

      

Result: x = "a";

DATA test2;
    LENGTH x $20;
    y = "a";
    x = y || "b";
RUN;

      

Result: x = "ab";

DATA test3;
    LENGTH x $20;
    x = "a";
    y = x;
    x = y || "b";
RUN;

      

Result: x = "a";

The last one is so strange. x doesn't even participate in concat directly.

This makes no sense. Since 1) you can do other operations this way eg. transtrn, substr. 2) SAS does not give any warning messages.

Why?

+3


source to share


2 answers


This is because the length of X is initially set to 20, so it has 19 trailing spaces. If you add b, there is no room for it due to trailing whitespace. Either truncate the x before the operator cat, or use catt. You can use lengthc to look at the length of a character variable.



DATA test1;
    LENGTH x $20;
    x = "a";
    len=lengthc(x);
    x = trim(x) || "b";
   *x = catt(x, b);
RUN;

proc print data=test1;
run;

      

+5


source


You can also use substr () on the left side of the equation. Something like:

substr(x,10,1) = 'a';

      



set the 10th car to "a". Then loop over each character in x (where 10).

0


source







All Articles