1

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!

andrey_sz
  • 751
  • 1
  • 13
  • 29
stallingOne
  • 3,633
  • 3
  • 41
  • 63

3 Answers3

3

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;
Jetzler
  • 787
  • 3
  • 11
1

You can add attrib or format statement to data-step:

data want;
   attrib 
      aaaaa format=$200.;
   aaaaa='[';
   aaaaa=cats(aaaaa,'hello');
   aaaaa=cats(aaaaa,']');
   put aaaaa;
run;
andrey_sz
  • 751
  • 1
  • 13
  • 29
0

As per my comments, here's an alternative way using call cats.

data want;
length aaaaa $300;
call cats(aaaaa,'[');
call cats(aaaaa,'Hello');
call cats(aaaaa,']');
run;
Longfish
  • 7,582
  • 13
  • 19