Search code examples
sasdatastep

SAS How to concatenate strings several times in the same variable during a data step


A very basic question.

How come I cannot change a variable in a datastep like this?

data want;
   aaaaa='[';
   aaaaa=cats(aaaaa,'hello');
   aaaaa=cats(aaaaa,']');
   put aaaaa;
run;

aaaaa will be equal to '[' , I expect '[hello]'

I need to define aaaaa in multiple steps because I need to construct aaaaa with do loops (with unpredictable length), I can't define aaaaa in just one step.

Thanks!


Solution

  • When first calling aaaaa SAS will assign a length to the variable. In this case length aaaaa $1 as you only assign one character. SAS data types are not dynamic. Start your datastep by assigning your variable a fixed length that covers the maximal expected length e.g.

    data want;
        length aaaaa $300;
        aaaaa='[';
        aaaaa=cats(aaaaa,'hello');
        aaaaa=cats(aaaaa,']');
        put aaaaa;
    run;